渲染尽可能小的图像尺寸与MVC3 VS Web表单库 [英] Rendering smallest possible image size with MVC3 vs Webforms Library

查看:120
本文介绍了渲染尽可能小的图像尺寸与MVC3 VS Web表单库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在移动web表单应用MVC3的过程。讽刺的是,一切都只是一件事酷豆 - 图像是从处理程序提供服务,特别是微软生成图像处理器。它的作品真的很好 - 在平均450KB的照片被输出以大约20KB

在磁盘上的实际照片在417KB重量在,所以我得到一个伟大的减少。

移动到MVC3我想放弃处理器,并使用一个控制器的动作。但是我似乎无法呈现图像时,实现了相同种类的文件大小减小。我通过源走去,把他们的图像的精确副本变换code 但我只获得230〜KB,这仍然比什么毫秒处理程序输出大很多 - 16KB

您可以看到控制器和这里处理程序两者的例子

我已经通过处理程序源$ C ​​$ C走去,什么都看不到那是COM pressing形象进一步。如果你研究这两种图像,你可以看到一个区别 - 处理器渲染图像不太清楚,更多的颗粒感看,但还是什么,我会考虑我的需求满意

谁能给我任何指针吗?输出COM pression莫名其妙地被使用?还是我忽视的东西很明显?

在code以下是我家的控制器用来渲染图像,并且是FitImage方法中的图像变换类中的处理程序使用...

 公众的ActionResult MvcImage()
    {
        var文件=使用Server.Mappath(〜/内容/ test.jpg放在);
        变种IMG = System.Drawing.Image.FromFile(文件);
        VAR sizedImg = MsScale(IMG);        VAR NEWFILE =使用Server.Mappath(〜/ App_Data文件/ test.jpg放在);
        如果(System.IO.File.Exists(NEWFILE))
        {
            System.IO.File.Delete(NEWFILE);
        }
        sizedImg.Save(NEWFILE);
        返回文件(NEWFILE,图像/ JPEG);
    }私人形象MsScale(图像IMG)
    {
        VAR scaled_height = 267;
        VAR scaled_width = 400;
        INT resizeWidth = 400;
        INT resizeHeight = 267;
        如果(img.Height == 0)
        {
            resizeWidth = img.Width;
            resizeHeight = scaled_height;
        }
        否则,如果(img.Width == 0)
        {
            resizeWidth = scaled_width;
            resizeHeight = img.Height;
        }
        其他
        {
            如果(((浮点)img.Width /(浮点)img.Width< img.Height /(浮点)img.Height))
            {
                resizeWidth = img.Width;
                resizeHeight = scaled_height;
            }
            其他
            {
                resizeWidth = scaled_width;
                resizeHeight = img.Height;
            }
        }        位图newimage =新位图(resizeWidth,resizeHeight);
        图形GRA = Graphics.FromImage(newimage);
        SetupGraphics(GRA);
        gra.DrawImage(IMG,0,0,resizeWidth,resizeHeight);
        返回newimage;
    }    私人无效SetupGraphics(图形图像)
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighSpeed​​;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.Smoothi​​ngMode = Smoothi​​ngMode.HighSpeed​​;
    }


解决方案

如果您没有设置在连接codeR质量,它使用默认的100。你永远不会得到使用100一个良好的尺寸减少因道路图像格式,如JPEG工作。我有一个如何设置,你应该能够适应的质量参数VB.net code的例子。

80L这里是质量设置。 80仍然为您提供了一个相当高品质的形象,但在大幅减少规模超过100。

 昏暗的图形作为System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newImage)
            graphic.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
            graphic.Smoothi​​ngMode = Drawing.Drawing2D.Smoothi​​ngMode.HighQuality
            graphic.PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality
            graphic.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
            graphic.DrawImage(sourceImage,0,0,宽度,高度)            恩,现在code和发送新形象
            这是重要的组成部分            昏暗信息()作为Drawing.Imaging.Image codecInfo = Drawing.Imaging.Image codecInfo.GetImageEn codeRS()
            昏暗带coderParameters作为新Drawing.Imaging.En coderParameters(1)            EN coderParameters.Param(0)=新Drawing.Imaging.En coderParameter(Drawing.Imaging.En coder.Quality,80L)
            MS =新System.IO.MemoryStream
            newImage.Save(MS,信息(1),EN coderParameters)

当您保存或设置EN codeR参数后,否则写的形象,它会输出它使用设置为质量80. JPEG EN codeR(在这种情况下),将让你的大小你正在寻找的节约。

I am in the process of moving a webforms app to MVC3. Ironically enough, everything is cool beans except one thing - images are served from a handler, specifically the Microsoft Generated Image Handler. It works really well - on average a 450kb photo gets output at roughly 20kb.

The actual photo on disk weighs in at 417kb, so i am getting a great reduction.

Moving over to MVC3 i would like to drop the handler and use a controller action. However i seem to be unable to achieve the same kind of file size reduction when rendering the image. I walked through the source and took an exact copy of their image transform code yet i am only achieving 230~kb, which is still a lot bigger than what the ms handler is outputting - 16kb.

You can see an example of both the controller and the handler here

I have walked through the handler source code and cannot see anything that is compressing the image further. If you examine both images you can see a difference - the handler rendered image is less clear, more grainy looking, but still what i would consider satisfactory for my needs.

Can anyone give me any pointers here? is output compression somehow being used? or am i overlooking something very obvious?

The code below is used in my home controller to render the image, and is an exact copy of the FitImage method in the Image Transform class that the handler uses ...

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;
    }

解决方案

If you don't set the quality on the encoder, it uses 100 by default. You'll never get a good size reduction by using 100 due to the way image formats like JPEG work. I've got a VB.net code example of how to set the quality parameter that you should be able to adapt.

80L here is the quality setting. 80 still gives you a fairly high quality image, but at DRASTIC size reduction over 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)

When you save or otherwise write the image after setting the encoder parameters, it'll output it using the JPEG encoder (in this case) set to quality 80. That will get you the size savings you're looking for.

这篇关于渲染尽可能小的图像尺寸与MVC3 VS Web表单库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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