Click here to Skip to main content
15,886,835 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How croping is done for image processing in C#.net? give the code!
Posted
Updated 18-Oct-10 4:46am
v2
Comments
HimanshuJoshi 18-Oct-10 10:07am    
What have you tried? Any effort? Nobody is going to provide entire code to you.
Marc A. Brown 18-Oct-10 10:09am    
You'd think sooner or later people would learn not to simply demand code. Make an effort to do the task and then come back with questions about the things that don't work!
Abhinav S 18-Oct-10 10:45am    
This is a global website - please dont demand people to "give the code:.

This[^] may help you.
 
Share this answer
 
public static void CropImageFile(string ImageFrom, string ImageTo, int targetW, int targetH)
{
Image imgPhoto = Image.FromFile(ImageFrom);
int targetX = (imgPhoto.Width - targetW) / 2;
int targetY = (imgPhoto.Height - targetH) / 2;

Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.DrawImage
(
imgPhoto,
new Rectangle(0, 0, targetW, targetH),
targetX,
targetY,
targetW,
targetH,
GraphicsUnit.Pixel
);
// Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked.
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)100);

ImageCodecInfo ici = GetEncoderInfo("image/jpeg");

imgPhoto.Dispose();
grPhoto.Dispose();

bmPhoto.Save(ImageTo, ici, ep);
bmPhoto.Dispose();
}

private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900