如何在asp.net中以相同的清晰度减小图像大小
问题描述:
亲爱的先生,
如何以相同的清晰度减少asp.net中的图像尺寸
我使用文件上传控件上传图片。
大小示例:
我的图像尺寸是=> 1 MB到3 MB。
它将减少到100 KB以下。
和
它会显示原始尺寸的相同清晰度。
请告诉我如何制作代码缩小图像大小???
通过
Dear Sir,
How to reduce image size in asp.net with same clarity
I am uploading image using file upload control.
its size Example:
My Image Size is => 1 MB to 3MB.
it will reduce below 100 KB size.
and
it will display same clarity what image original size is.
Please tell me how to make a code for reduce image size???
By
答
使用以下功能缩小图像尺寸。它将重新生成具有定义高度和宽度的图像。
Use following function to reduce image size. It will regenerate image with defined height and width.
public static void ResizeImage(string image, string Okey, string key, int width, int height, string newimagename)
{
System.Drawing.Image oImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image));
System.Drawing.Image oThumbNail = new System.Drawing.Bitmap(width, height);//, System.Drawing.Imaging.PixelFormat.Format24bppRgb
System.Drawing.Graphics oGraphic = System.Drawing.Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//set smoothing mode to high quality
oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//set the interpolation mode
oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//set the offset mode
oGraphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle(0, 0, width, height);
oGraphic.DrawImage(oImg, oRectangle);
if (newimagename == "")
{
if (image.Substring(image.LastIndexOf(".")) != ".png")
oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image), System.Drawing.Imaging.ImageFormat.Jpeg);
else
oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image), System.Drawing.Imaging.ImageFormat.Png);
}
else
{
if (newimagename.Substring(newimagename.LastIndexOf(".")) != ".png")
oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + newimagename), System.Drawing.Imaging.ImageFormat.Jpeg);
else
oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + newimagename), System.Drawing.Imaging.ImageFormat.Png);
}
oImg.Dispose();
}
Hi
使用以下代码减小尺寸而不会失去清晰度
Hi
Use following code to reduce size without loss of clarity
Image img;
img.GetThumbnailImage(400,400,null,IntPtr.Zero)
一种减小尺寸的简单方法图片或生成缩略图
查看此图
通过使用C#保持纵横比来创建缩略图图像
A very simple way to decrease size of an image or generating a thumbnail
check this out
Creating Thumbnail Image by keeping Aspect Ratio using C#