ASP.NET将图像从PNG转换为jpg

时间:2011-02-23 15:15:49

标签: asp.net gdi+

我将图像从PNG转换为JPG时出现问题。从jpg到jpg是非常好的,但从PNG到jpg有一个像素化的问题。我已经在这段代码中实现了压缩方法,但它仍然无法生成高质量的图像。

任何想法的人?

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
public partial class _Default : System.Web.UI.Page
{

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

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "image/JPEG";//convert toJPEG for web usage
         // Get a bitmap.
        Bitmap bmp1 = new Bitmap(@"E:\websites\LogoMaster\LogoID4.png");
        ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
        // Create an Encoder object based on the GUID
        // for the Quality parameter category.
        System.Drawing.Imaging.Encoder myEncoder =System.Drawing.Imaging.Encoder.Quality;
        // Create an EncoderParameters object.
        // An EncoderParameters object has an array of EncoderParameter
        // objects. In this case, there is only one
        // EncoderParameter object in the array.
        EncoderParameters myEncoderParameters = new EncoderParameters(1);
        EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);//SET to the highest quality
        myEncoderParameters.Param[0] = myEncoderParameter;
       // bmp1.Save(@"E:\websites\LogoMaster\TestPhotoQualityHundredp.jpg", jgpEncoder, myEncoderParameters);
        bmp1.Save(Response.OutputStream, jgpEncoder, myEncoderParameters);
        bmp1.Dispose();

    } 
}

1 个答案:

答案 0 :(得分:0)

也许你需要重新绘制图像?我最近回答了一个关于生成缩略图的问题(asp.net version of timthumb php class),这使得图像变得干净整洁。

            Dim bmpThumb As New Bitmap(destWidth, destHeight)
            Dim g = Graphics.FromImage(bmpThumb)
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
            g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

            g.DrawImage(img, _
                        New Rectangle(0, 0, destWidth, destHeight), _
                        New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)

            '-----write out Thumbnail to the output stream------'        
            'get jpeg image coded info so we can use it when saving'
            Dim ici As Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders().Where(Function(c) c.MimeType = "image/jpeg").First()

            'save image to memory stream'
            Dim ms As New MemoryStream()
            bmpThumb.Save(ms, ici, BuildQualityParams(context))

将它保存到内存流后,您只需将其写入asp.net响应流:

         context.Response.Clear()
            context.Response.ContentType = "image/jpeg"
            ms.WriteTo(context.Response.OutputStream)
            context.Response.Flush()