简体   繁体   English

即时更改图像质量

[英]Change image quality on the fly

I have write this code to do crop and resize the image on the fly . 我已经编写了这段代码来即时裁剪和调整图像大小。 I send the processed image to the browser like <img src="imagehandler.aspx?img=1.jpg"> : 我将处理后的图像发送到浏览器,例如<img src="imagehandler.aspx?img=1.jpg">

imagehandler.aspx: imagehandler.aspx:

<%@ Page Language="C#"%>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
    System.Drawing.Image oldImage, newImage,cloned,tempImage;
    void Page_Load(Object sender, EventArgs e) {
    string strFileName = Convert.ToString(Request.QueryString["img"]);
    oldImage = System.Drawing.Image.FromFile(Server.MapPath(strFileName));
    rect= new Rectangle(0,50,100,100);  
    cloned = new Bitmap(oldImage ).Clone(rect, tempImage.PixelFormat);
    newImage = new Bitmap(cloned);
    cloned.Dispose();   

    Response.ContentType = "image/jpeg";
    newImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    oldImage.Dispose();
    newImage.Dispose();
    oldImage = null;
    newImage = null;
    }
</script>

Now I want to add quality control to the output image and I found this Q/A This answer suggests a method which saves the image to the disk. 现在,我想对输出图像添加质量控制,并且找到了此Q / A。这个答案提出了一种将图像保存到磁盘的方法。 I have tried to fit it to my purpose. 我已尝试使其适合我的目的。 Currently I can only save it on the disk and the method is void. 目前,我只能将其保存在磁盘上,并且该方法无效。 I don't know how to pass the output to my own codes before streaming the result to the browser: 在将结果流式传输到浏览器之前,我不知道如何将输出传递到我自己的代码:

private void VaryQualityLevel(bmp1)
{
    ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
    System.Drawing.Imaging.Encoder myEncoder= System.Drawing.Imaging.Encoder.Quality;
    EncoderParameters myEncoderParameters = new EncoderParameters(1);
    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jgpEncoder,myEncoderParameters);
}

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

You can save your bitmap directly to MemoryStream and do whatever you want with it. 您可以将位图直接保存到MemoryStream并使用它进行任何操作。 Your encoder will be applied to image inside this stream. 您的编码器将应用于此流中的图像。 Instead of passing file path as first parameter of Save method just pass instance of MemoryStream. 不用传递文件路径作为Save方法的第一个参数,只需传递MemoryStream的实例即可。 If I remember correctly there's also a way to pass this stream directly as a response to browser. 如果我没记错的话,还有一种方法可以直接将此流作为对浏览器的响应。

using(var ms = new MemoryStream()) 
{
    bmp1.Save(ms, jgpEncoder, myEncoderParameters);
    var bmp2 = new BitMap(ms);
    //do whatever you want with this image
}

Keep in mind to use using statment or dispose method for sfream to avoid memory leak. 记住要使用声明或处置方法来避免内存泄漏。

More details here: https://docs.microsoft.com/en-us/dotnet/api/system.drawing.bitmap?view=netframework-4.7.2 此处有更多详细信息: https : //docs.microsoft.com/zh-cn/dotnet/api/system.drawing.bitmap?view=netframework-4.7.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM