Dynamic Image Resize and Data Type issues

W

webonomic

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.HorizontalResolution,
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.HorizontalResolution,
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;

}
 
W

webonomic

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?
 
W

webonomic

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top