Asp.net图像质量调整 [英] Asp.net image resizing quality

查看:207
本文介绍了Asp.net图像质量调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code,我用它来调整和保存是用户上传的文件。
问题是,当我rezise一个480像素宽的形象失去大量的质量和KB的规模还在pretty大了。

例如,当我调整到480像素相同的图像手动使用像画图软件,质量还是与原来一样好(从什么我的眼睛可以告诉)和KB大小是小了很多比使用 GetThumbNailImage 方法调整。

MDN说的如果你从具有嵌入的缩略图图像请求大量的缩略图(例如,300×300),有可能是缩略图质量的明显损失。这可能会更好通过调用的DrawImage 方法来缩放主图像(而不是缩放嵌入缩略图)。的,但是这似乎是Windows窗体,我需要一个Web应用程序。

什么code,我应该用它来做到这一点呢?

 的System.IO.Stream ST = FileUploadPost.PostedFile.InputStream;MYIMAGE = System.Drawing.Image.FromStream(ST);
拇指= myImage.GetThumbnailImage(newWidth,newHeight,空,System.IntPtr.Zero);thumb.Save(mypath中);


解决方案

下面是code已经为我工作。您可以设置新的位图分辨率:

 使用System.Drawing中;位图IMG =(位图)Bitmap.FromStream(FileUploadPost.PostedFile.InputStream);
位图newImg =新位图(了maxWidth,了maxHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newImg.SetResolution(72,72);
图形newGraphic = Graphics.FromImage(newImg);
newGraphic.Clear(Color.Transparent);
newGraphic.Smoothi​​ngMode = System.Drawing.Drawing2D.Smoothi​​ngMode.AntiAlias​​;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(IMG,0,0,了maxWidth,了maxHeight);
System.Drawing.Imaging.ImageFormat格式=默认(System.Drawing.Imaging.ImageFormat);
字符串EXT = Path.GetExtension(FileUploadPost.PostedFile.FileName);
开关(ext.ToLower())
{
    案例符.gif:
        格式= System.Drawing.Imaging.ImageFormat.Gif;
        打破;
    案巴纽:
        格式= System.Drawing.Imaging.ImageFormat.Png;
        打破;
    默认:
        格式= System.Drawing.Imaging.ImageFormat.Jpeg;
        打破;
}
newImg.Save(mypath中,格式);

您可以把它包在一个void函数在全球类:

 公共静态无效UploadImage(HttpPostedFileBase文件,诠释了maxWidth,诠释了maxHeight)
{
  //粘贴上述所有code在这里和文件替换FileUploadPost.PostedFile
}

然后你可以从任何地方在项目中调用它:

  ClassName.UploadImage(FileUploadPost.PostedFile,300,300);

I have this code that I use to resize and save a file that is posted by the user. The issue is that when I rezise to a 480px width the image looses lots of quality and the size in kb is still pretty big.

For instance, when I resize the same image to 480px "by hand" using a software like Paint, the quality is still as good as the original (from what my eye can tell) and the size in kb is a lot smaller than resizing using the GetThumbNailImage method.

Mdn says "If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method.", but that seems to be for Windows forms and I need for a web app.

What code should I use to do this then?

System.IO.Stream st = FileUploadPost.PostedFile.InputStream;

myImage = System.Drawing.Image.FromStream(st);
thumb = myImage.GetThumbnailImage(newWidth, newHeight, null, System.IntPtr.Zero);

thumb.Save(myPath);

解决方案

Here is code that has worked for me. You can set the new bitmap resolution:

using System.Drawing;

Bitmap img = (Bitmap)Bitmap.FromStream(FileUploadPost.PostedFile.InputStream);
Bitmap newImg = new Bitmap(maxWidth, maxHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newImg.SetResolution(72, 72);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.Clear(Color.Transparent);
newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(img, 0, 0, maxWidth, maxHeight);
System.Drawing.Imaging.ImageFormat format = default(System.Drawing.Imaging.ImageFormat);
string ext = Path.GetExtension(FileUploadPost.PostedFile.FileName);
switch (ext.ToLower())
{
    case ".gif":
        format = System.Drawing.Imaging.ImageFormat.Gif;
        break;
    case ".png":
        format = System.Drawing.Imaging.ImageFormat.Png;
        break;
    default:
        format = System.Drawing.Imaging.ImageFormat.Jpeg;
        break;
}
newImg.Save(myPath, format);

You can wrap it in a void function on a global class:

public static void UploadImage(HttpPostedFileBase file, int maxWidth, int maxHeight)
{
  //paste all the above code in here and replace FileUploadPost.PostedFile with file
}

Then you can call it from anywhere in your project:

ClassName.UploadImage(FileUploadPost.PostedFile, 300, 300);

这篇关于Asp.net图像质量调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆