473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Dynamic Image Resize and Data Type issues

Converting data types

I'm trying to do some image manipulation. This code project article
(http://www.codeproject.com/csharp/imageresize.asp) has a great method
I want to modefy. Here it is:

static Image ScaleByPercent(Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent/100);

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight ),
GraphicsUnit.Pixel);

grPhoto.Dispose();
return bmPhoto;
}

However, I have some business rules for the source image size.
- height or width cannot be less than 100px, display error in
<asp:label> to user
- height or width cannot exceed 1000px, display error in <asp:label> to
user

The dest image has rules too:
- if source image has height > width, set dest height = 100px,
proportionally set the dest width
- if source image has width > height, set dest width = 100px,
proporionally set the dest height
- if source height = source width, set dest height and width both to
100px

In the case of destHeight:
- if sourceWidth > sourceHeight;
- destWidth = 100px;
- destHeight = ((destWidth / sourceWidth) * 100)

My issues are:
1) I am having data type issues with getting destHeight or destWidth in
this manner.
2) I don't know the proper way to setup the business rules in this
method. If statement? Switch statment?

Here is my method so far:

static System.Drawing.Image ScaleByPercent(System.Drawing.Image
imgPhoto)
{
//float nPercent = ((float)percent / 100);
int percent;

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int destWidth = 0; //(int)(sourceWidth * nPercent);
int destHeight = 0; //(int)(sourceHeight * nPercent);
string theMessage = "";
if (sourceHeight > 100 || sourceWidth > 100)
{
if (sourceHeight < 1000 || sourceWidth < 1000)
{
if (sourceHeight > sourceWidth)
{
destHeight = 100;
//what do I do here ??
percent = (float)((destHeight / sourceHeight) *
100);
destWidth = (int)(sourceWidth * percent);
}
else if (sourceWidth > sourceHeight)
{
destWidth = 100;
//what do I do here ??
percent = (float)((destWidth / sourceWidth) * 100);
destHeight = (int)(sourceHeight * percent);
}
else if (sourceWidth == sourceHeight)
{
destHeight = 100;
destWidth = 100;
}
else
{
theMessage = "Image was both greater than 100 and
less than 1000, something else is wrong";

}

theMessage = "Image must be less than 1000 pixels";

}

theMessage = "Image must be more than 100 pixels";

}

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();
return bmPhoto;

}

Jan 26 '06 #1
2 1737
Well, I'm starting to answer my own question. For the data types, I
did the following to test things out:

int a = 101;
int b = 999;

int z = 100;
double c = (a / b)*z;
lbl.Text = c.ToString();
double d = ((Convert.ToDouble(a)) / Convert.ToDouble(b))*
Convert.ToDouble(z);
lbl2.Text = d.ToString();

int y = Convert.ToInt32(d);
lbl3.Text = y.ToString();

d = Math.Round(d);
lbl4.Text = d.ToString();

Now I know how to get my destWidth or destHeight int's.

What I still would like to know is how I should setup all of my
business rules. If statements or a switch?

Jan 26 '06 #2
I'll answer my own question even more. For some reason I thought Case
statements could handle expressions when they do not. Expressions like
(x > y). So I have to use the If statements.

I guess all is good. It's a matter of playing around with the code now.

webonomic wrote:
Well, I'm starting to answer my own question. For the data types, I
did the following to test things out:

int a = 101;
int b = 999;

int z = 100;
double c = (a / b)*z;
lbl.Text = c.ToString();
double d = ((Convert.ToDouble(a)) / Convert.ToDouble(b))*
Convert.ToDouble(z);
lbl2.Text = d.ToString();

int y = Convert.ToInt32(d);
lbl3.Text = y.ToString();

d = Math.Round(d);
lbl4.Text = d.ToString();

Now I know how to get my destWidth or destHeight int's.

What I still would like to know is how I should setup all of my
business rules. If statements or a switch?


Jan 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
1
by: Blasting Cap | last post by:
I would like to try to see if an image uploaded to a SQL database is over a specific size, and if so, to resize it when it is displayed. The following is in the Page_load event. Dim strImageID...
2
by: Poppa Pimp | last post by:
ImageResizer.php Image Resizer PLEASE HELP The URL of the page this is on in my site is http://poppa-pimps-wallpapers.com//ImageResizer.php You can click on browse and get image,but...
9
by: dli07 | last post by:
Hello, I'm trying to convert a piece of code that creates a dynamic vertical resizing bar in a table from internet explorer to firefox. It's based on a post from...
7
by: xx75vulcan | last post by:
Hi, I've got a PHP Upload Form that works great, unless that is, the image your uploading has been modified through a photo editing software. Example: if I upload the image straight from a...
1
by: bharathv6 | last post by:
i need to do is modify the image in memory like resizing the image in memory etc ... with out saving it disk as i have to return back the image with out saving it disk PIL supports the use of...
8
by: JJ | last post by:
Whilst I am resizing images I am losing quality. This is only happening in small amounts, but if you repeatedly put the same image through the following code, the image quality slowly degrades. Can...
3
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi! This discussion may help other programmers get a better idea of how to save uploaded images through a website. Why? Well currently, I save 3 versions of every uploaded image on my own...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.