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;
}
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;
}