Monday, May 10, 2010
Saturday, May 8, 2010
Friday, May 7, 2010
Wednesday, April 28, 2010
Sunday, April 25, 2010
Thursday, January 7, 2010
Crop Image C#
protected static System.Drawing.Image ImageCrop(
System.Drawing.Image imgPhoto, int Width, int Height, int dWidth, int dHeight, int bWidth)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int destWidth = dWidth;
int destHeight = dHeight;
int borderWidth = bWidth;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
//If there is a border then center image and resize bitmap to add in
if (borderWidth != 0)
{
destX = borderWidth / 2;
destY = borderWidth / 2;
destHeight = destHeight + borderWidth;
destWidth = destWidth + borderWidth;
}
//Draw the bitmap canvas - this is the final size of the created image
System.Drawing.Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//set the resize filter
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//do the resizing from imgPhoto as source, then the destination size and position, then cropping rect
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, dWidth, dHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
//done with resize, release resources
grPhoto.Dispose();
//return resized image to caller
return bmPhoto;
}
System.Drawing.Image imgPhoto, int Width, int Height, int dWidth, int dHeight, int bWidth)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int destWidth = dWidth;
int destHeight = dHeight;
int borderWidth = bWidth;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
//If there is a border then center image and resize bitmap to add in
if (borderWidth != 0)
{
destX = borderWidth / 2;
destY = borderWidth / 2;
destHeight = destHeight + borderWidth;
destWidth = destWidth + borderWidth;
}
//Draw the bitmap canvas - this is the final size of the created image
System.Drawing.Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//set the resize filter
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//do the resizing from imgPhoto as source, then the destination size and position, then cropping rect
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, dWidth, dHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
//done with resize, release resources
grPhoto.Dispose();
//return resized image to caller
return bmPhoto;
}
Subscribe to:
Comments (Atom)



