Task: Dynamically Re-size Image with maintain its ration using Asp.net and C# code.
Description: Many times we need to store different size images for a specific images as used in e-commerce website. for that we re-size the image and save multiple images as one small, medium and a full size image. But If we have issue regarding space and having good performance server then we can use dynamically re-sizing of image just save single big image and then when need to use re-size it at run time as you want.
For achieving this task I write a code which response the re-size image. I use it for jpg and png images not for gif.
Call the page where you code for image merging :
Dynamic Code for image generation and merging (dynamicimage.aspx/dynamicimage.aspx.cs)
// can set imageurl, max-width and max-height using query string...
// below code is my old one for calculating height and width with maintain image ratio.
Description: Many times we need to store different size images for a specific images as used in e-commerce website. for that we re-size the image and save multiple images as one small, medium and a full size image. But If we have issue regarding space and having good performance server then we can use dynamically re-sizing of image just save single big image and then when need to use re-size it at run time as you want.
For achieving this task I write a code which response the re-size image. I use it for jpg and png images not for gif.
Call the page where you code for image merging :
Image1.ImageUrl = "dynamicimage.aspx";
Dynamic Code for image generation and merging (dynamicimage.aspx/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);
}
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;
}
No comments:
Post a Comment