smart developer’s blog

This is a C# resource library! Free how to’s and best practices…

Posts Tagged ‘image manipulation

Crop image in ASP.Net (C#)

with one comment

You can use the custom method below:

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; } [/sourcecode] If you combine this with the thumbnail auto-generation you can get a nice method that can generate a cropped thumbnail. 🙂

Advertisements

Written by smartdev

April 9, 2009 at 4:50 pm

Posted in .Net

Tagged with ,

Generate image thumbnails using ASP.NET(C#)

with 7 comments

You can use the GetThumbnailImage built-in method to generate a thumbnail using this method I created:

public static void CreateThumbnail(string ImageFrom, string ImageTo, int ImageHeight)
{
System.Drawing.Image i = System.Drawing.Image.FromFile(ImageFrom);

i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

System.Drawing.Image th = i.GetThumbnailImage
(
ImageHeight * i.Width / i.Height,
ImageHeight,
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero
);
i.Dispose();

EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)80);
ImageCodecInfo ici = GetEncoderInfo(“image/jpeg”);

th.Save(ImageTo, ici, ep);
th.Dispose();
return;
}

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; } public static bool ThumbnailCallback() { return true; } [/sourcecode] If the Image contains an embedded thumbnail image (all digital cameras attach a thumbnail to the big image), this method retrieves the embedded thumbnail and scales it to the requested size. But the problem is that in most of the cases we do not want this because we want to generate thumbnails bigger than 150-200 pixels as embwdded thumbnails have and this will lead in poor quality images for 400-500 pizels or more thumbnail sizes. The workaround is to use this piece of code: [sourcecode language="csharp"] i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); [/sourcecode] By fliping the image twice we get a new image identical with the original one but without the embedded thumbnail. Now the resulted thumbnail is in a very good quality. If you combine this with the cropped image auto-generation you can get a nice method that can generate a cropped thumbnail. 🙂

Written by smartdev

April 9, 2009 at 3:41 pm

Posted in .Net, Programming

Tagged with ,