【问题标题】:Rendering smallest possible image size with MVC3 vs Webforms Library使用 MVC3 与 Webforms 库渲染尽可能小的图像尺寸
【发布时间】:2011-07-21 13:45:42
【问题描述】:

我正在将网络表单应用程序迁移到 MVC3。具有讽刺意味的是,除了一件事之外,一切都是很酷的豆子——图像是由处理程序提供的,特别是Microsoft Generated Image Handler。它工作得非常好——平均一张 450kb 的照片以大约 20kb 的速度输出。

实际的photo on disk 大小为 417kb,因此我得到了很大的缩减。

转到 MVC3 我想删除处理程序并使用控制器操作。但是,在渲染图像时,我似乎无法实现相同类型的文件大小减小。我浏览了源代码并获取了他们image transform code 的精确副本,但我只达到了 230~kb,这仍然比 ms 处理程序输出的 16kb 大得多。

你可以see an example of both the controller and the handler here

我浏览了处理程序源代码,看不到任何进一步压缩图像的内容。如果您检查这两个图像,您会发现差异 - 处理程序渲染的图像不太清晰,看起来更有颗粒感,但我仍然认为满足我的需求。

有人可以在这里给我任何指示吗?是否以某种方式使用了输出压缩?还是我忽略了一些非常明显的东西?

下面的代码在我的家庭控制器中用于渲染图像,并且是处理程序使用的Image Transform class 中的 FitImage 方法的精确副本...

public ActionResult MvcImage()
    {
        var file = Server.MapPath("~/Content/test.jpg");
        var img = System.Drawing.Image.FromFile(file);
        var sizedImg = MsScale(img);

        var newFile = Server.MapPath("~/App_Data/test.jpg");
        if (System.IO.File.Exists(newFile))
        {
            System.IO.File.Delete(newFile);
        }
        sizedImg.Save(newFile);
        return File(newFile, "image/jpeg");
    }

private Image MsScale(Image img)
    {
        var scaled_height = 267;
        var scaled_width = 400;
        int resizeWidth = 400;
        int resizeHeight = 267;
        if (img.Height == 0)
        {
            resizeWidth = img.Width;
            resizeHeight = scaled_height;
        }
        else if (img.Width == 0)
        {
            resizeWidth = scaled_width;
            resizeHeight = img.Height;
        }
        else
        {
            if (((float)img.Width / (float)img.Width < img.Height / (float)img.Height))
            {
                resizeWidth = img.Width;
                resizeHeight = scaled_height;
            }
            else
            {
                resizeWidth = scaled_width;
                resizeHeight = img.Height;
            }
        }

        Bitmap newimage = new Bitmap(resizeWidth, resizeHeight);
        Graphics gra = Graphics.FromImage(newimage);
        SetupGraphics(gra);
        gra.DrawImage(img, 0, 0, resizeWidth, resizeHeight);
        return newimage;
    }

    private void SetupGraphics(Graphics graphics)
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighSpeed;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighSpeed;
    }

【问题讨论】:

    标签: asp.net-mvc-3 httphandler image-scaling


    【解决方案1】:

    如果您没有在编码器上设置质量,则默认使用 100。由于 JPEG 等图像格式的工作方式,使用 100 将永远无法很好地减小尺寸。我有一个 VB.net 代码示例,说明如何设置您应该能够适应的质量参数。

    80L 这里是质量设置。 80 仍然可以为您提供相当高质量的图像,但在 100 的尺寸上会大幅减小。

                Dim graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newImage)
                graphic.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
                graphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
                graphic.PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality
                graphic.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
                graphic.DrawImage(sourceImage, 0, 0, width, height)
    
                ' now encode and send the new image
                ' This is the important part
    
                Dim info() As Drawing.Imaging.ImageCodecInfo = Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
                Dim encoderParameters As New Drawing.Imaging.EncoderParameters(1)
    
                encoderParameters.Param(0) = New Drawing.Imaging.EncoderParameter(Drawing.Imaging.Encoder.Quality, 80L)
                ms = New System.IO.MemoryStream
                newImage.Save(ms, info(1), encoderParameters)
    

    当您在设置编码器参数后保存或以其他方式写入图像时,它将使用设置为质量 8​​0 的 JPEG 编码器(在本例中)输出它。这将为您节省所需的尺寸。

    【讨论】:

      【解决方案2】:

      我相信它也默认为 PNG 格式,尽管 Tridus 的解决方案也解决了这个问题。

      但是,我强烈建议using this MVC-friendly library instead,因为它避免了all the image resizing pitfalls 并且不会泄漏内存。它非常轻量级、免费且完全受支持。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-01
        • 2021-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多