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

发布于 2024-10-19 03:43:20 字数 1934 浏览 2 评论 0原文

我在将图像从 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();

    } 
}

I am having a problem with converting the image from PNG to JPG. From jpg to jpg is very good but from PNG to jpg is having a pixelated problem. I've already implemented the compression method into this code but it still couldn't produce a high quality image.

Any ideas guys?

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();

    } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末蓝 2024-10-26 03:43:20

也许您需要重新绘制图像?我最近回答了一个关于生成缩略图的问题(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()

Maybe you need to redraw the image? I answered a question on generating thumbnails recently (asp.net version of timthumb php class) that made the images nice and clean.

            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))

Once you save it to the memory stream you just need to write it out to the asp.net response stream:

         context.Response.Clear()
            context.Response.ContentType = "image/jpeg"
            ms.WriteTo(context.Response.OutputStream)
            context.Response.Flush()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文