November 14, 2014 04:54 by
Peter
Now, I am going to tell you how to Re-size Image along with maintain the ration employing ASP.NET 4.5.2 and C# code. Sometimes we must store totally different size images to get a certain images as applied to e-commerce web page. for that we both re-size the image and save multiple images collectively small, medium along with a full size image. Other then When we've got issue relating to space having very fine performance server then we are able to use dynamically re-sizing of image merely save only huge image then when want to utilize re-size it in run time when you need.
For achieving this process I generate a code that response the re-size image. I utilize it for jpg and png images not for gif. Call the page in which code for image merging :
Image1.ImageUrl = "dynamicimage.aspx";
And this is the Code for image generation and merging (dynamicimage.aspx or dynamicimage.aspx.cs) using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
protected void Page_Load(object sender, EventArgs e)
{
string testimage = Server.MapPath(@"images/hemant_image.jpg");
getimage(testimage, 400, 400);
// can set imageurl, max-width and max-height using query string...
}
public void getimage(string ThumbnailPath, int maxwidth, int maxheight)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(ThumbnailPath);
Size ThumbNailSize = setimagesize(maxwidth, maxheight, image.Width, image.Height);
System.Drawing.Image ImgThnail = new Bitmap(ThumbNailSize.Width, ThumbNailSize.Height);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(ImgThnail);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.DrawImage(image, 0, 0, ThumbNailSize.Width, ThumbNailSize.Height);
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
string sExt = System.IO.Path.GetExtension(ThumbnailPath);
if (sExt.ToString() == ".png")
{
Response.ContentType = "image/png";
ImgThnail.Save(Response.OutputStream, image.RawFormat);
}
else
{
Response.ContentType = "image/jpeg";
ImgThnail.Save(Response.OutputStream, info[1], encoderParameters);
}
image.Dispose();
}
// below code is my old one for calculating height and width with maintain image ratio.
public Size setimagesize(int maxwidth, int maxheight, int OriginalWidth, int OriginalHeight)
{
Size NewSize = new Size();
int sNewWidth = OriginalWidth;
int sNewHeight = OriginalHeight;
int tempheight = 0;
int tempwidht = 0;
if (OriginalWidth >= OriginalHeight)
{
if (OriginalWidth >= maxwidth)
{
sNewWidth = maxwidth;
sNewHeight = OriginalHeight * maxwidth / OriginalWidth;
}
if (sNewHeight > maxheight)
{
tempheight = sNewHeight;
sNewHeight = maxheight;
sNewWidth = sNewWidth * maxheight / tempheight;
}
}
else
{
if (OriginalHeight >= maxheight)
{
sNewHeight = maxheight;
sNewWidth = OriginalWidth * maxheight / OriginalHeight; }
if (sNewWidth > maxwidth)
{
tempwidht = sNewWidth;
sNewWidth = maxwidth;
sNewHeight = sNewHeight * maxwidth / tempwidht;
}
}
NewSize = new Size(sNewWidth, sNewHeight);
return NewSize;
}