Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET将图像从PNG转换为jpg_Asp.net_Gdi+ - Fatal编程技术网

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

ASP.NET将图像从PNG转换为jpg,asp.net,gdi+,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 S

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

    } 
}

也许你需要重新绘制图像?我最近回答了一个关于生成缩略图的问题(),它使图像变得漂亮和干净

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

你最近没问这个吗?您只是删除并重新询问吗?是的,问题中有一个错误。漂亮的接球手看起来还是很模糊。如果我输出为png,图片看起来很清晰,JPEG看起来很模糊。我不得不做一些事情使其“网络安全”,这就是为什么压缩很糟糕的原因。有一些关于“颜色量化”的文章,但它只支持版本2,代码有点太多。我别无选择,只能将结果保存为带有白色背景的PNG,以防止IE 6出现问题。也许你可以使用一个jquery插件,使PNG图像在IE6上透明。谢谢,我一直在使用它,但不再使用了,谢谢我的新技术(添加白色背景)…哈哈