【问题标题】:Resizing image in ASP.NET losing quality?在 ASP.NET 中调整图像大小会丢失质量?
【发布时间】:2011-04-18 07:48:36
【问题描述】:

我有一个调整图像大小的脚本,我没有编写这个脚本,也不知道任何 ASP.NET,但我被分配了修复它的问题的任务!

问题是,即使上传的文件已设置为正确的大小,调整大小生成的图像质量似乎也低于上传的质量。

上传图片 - http://climatechange.cbi.org.uk/uploaded/whatevhomep.jpg 调整大小的图像 - http://climatechange.cbi.org.uk/ext_pages/resizeimage.aspx?img=/uploaded/whatevhomep.jpg&w=500

调整大小的脚本如下,知道如何解决这个问题吗?

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class ext_pages_resizeImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Read in the image filename to create a thumbnail of
        String imageUrl = "";

        try
        {   imageUrl = Request.QueryString["img"].ToString();
        }catch(Exception imgEx){}

        //Read in the width and height
        int thumbWidth = 0;
        int imageOrigHeight = 0;
        int imageOrigWidth = 0;

        try
        {
            thumbWidth = Convert.ToInt32(Request.QueryString["w"].ToString());
        }catch(Exception imgHEx)
        {
            thumbWidth = Convert.ToInt32(Request.QueryString["amp;w"]);
        }


        if (!System.IO.File.Exists(Server.MapPath(imageUrl)))
            Response.End();

        System.IO.FileInfo fileDetails = new System.IO.FileInfo(Server.MapPath(imageUrl));

        System.Drawing.Image fullSizeImg; 
        fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl));

        imageOrigHeight = fullSizeImg.Height;
        imageOrigWidth = fullSizeImg.Width;

        Decimal sizeRatio = ((Decimal)imageOrigHeight / imageOrigWidth);
        int thumbHeight = Decimal.ToInt32(sizeRatio * thumbWidth);

        if (thumbHeight <= 0 || thumbWidth <= 0)
        {
            pageMisc oPageMisc = new pageMisc();

            oPageMisc.sendMail("tim.payne@epiphanysolutions.co.uk", "CBI CC Image Errors", "Width: " + thumbWidth.ToString() + ", Height: " + thumbHeight.ToString());
        }

        Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);

        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
        gr.DrawImage(fullSizeImg, rectDestination, 0, 0, imageOrigWidth, imageOrigHeight, GraphicsUnit.Pixel);

        switch (fileDetails.Extension.ToUpper())
        {
            case ".JPG":
                Response.ContentType = "image/jpeg";
                break;
            case ".GIF":
                Response.ContentType = "image/gif";
                break;
        }

        //Do we need to create a thumbnail?
        if (thumbHeight > 0 && thumbWidth > 0)
        {
            System.Drawing.Image.GetThumbnailImageAbort dummyCallBack;
            dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

            System.Drawing.Image thumbNailImg;
            thumbNailImg = fullSizeImg.GetThumbnailImage(thumbWidth, thumbHeight, dummyCallBack, IntPtr.Zero);

            switch (fileDetails.Extension.ToUpper())
            {
                case ".JPG":
                    bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
                    break;
                case ".GIF":
                    bmp.Save(Response.OutputStream, ImageFormat.Gif);
                    break;
            }

            //Clean up / Dispose...
            thumbNailImg.Dispose();
        }
        else
        {
            switch (fileDetails.Extension.ToUpper())
            {
                case ".JPG":
                    fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg);
                    break;
                case ".GIF":
                    fullSizeImg.Save(Response.OutputStream, ImageFormat.Gif);
                    break;
            }
        }

        //Clean up / Dispose...
        fullSizeImg.Dispose();
        bmp.Dispose();
    }

    public static bool ThumbnailCallback()
    {
        return false;
    }

}

更新显示错误消息

Compiler Error Message: CS0103: The name 'GetEncoder' does not exist in the current context

Source Error:


Line 109:                    var codecParams = new EncoderParameters(1);
Line 110:                    codecParams.Param[0] = ratio;
Line 111:                    fullSizeImg.Save(Response.OutputStream, GetEncoder(ImageFormat.Jpeg), codecParams);
Line 112:                    break; 
Line 113:                case ".GIF":

Line: 111 

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    保存为 JPEG 时,您需要指定更高的质量。将您的 JPEG 大小写替换为以下内容:

    case ".JPG": 
        Encoder quality = Encoder.Quality;
        var ratio = new EncoderParameter(quality, 80L);
        var codecParams = new EncoderParameters(1);
        codecParams.Param[0] = ratio;
        fullSizeImg.Save(Response.OutputStream, GetEncoder(ImageFormat.Jpeg), codecParams);
        break; 
    

    并添加此功能:

        private ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
                if (codec.FormatID == format.Guid)
                    return codec;
            return null;
        }
    

    GIF 可能需要类似的东西。

    【讨论】:

    • 感谢您的回复。请原谅我缺乏理解,但我应该在代码中的哪个位置插入以获得完整质量的图像?谢谢!
    • @probocop:查看我的编辑 - 用我的 JPG 替换 case 语句
    • @RedFilter - 嗯,我刚试过,它似乎抛出了一个错误,知道为什么会这样吗?我更换了 JPG 的外壳,并通过 FTP 将文件备份到服务器。
    • @RedFilter - 我设法得到了错误消息,我已经用它更新了我的原始帖子。非常感谢您的帮助!
    • @Probocop:抱歉,缺少功能。我已经添加进去了。
    【解决方案2】:

    尝试使用GetThumbnailImage函数:

    System.Drawing.Image.GetThumbnailImageAbort dummyCallBack
           = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
    System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(
                        nWidth, 
                        nHeight,
                        dummyCallBack, 
                        IntPtr.Zero);
    

    ThumnailCallback 函数:

        //this function is reqd for thumbnail creation
        protected bool ThumbnailCallback()
        {
            return false;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 2020-08-20
      • 2015-03-05
      • 1970-01-01
      • 2014-02-17
      • 2021-09-20
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多