ASP.NET 将图像从 PNG 转换为 jpg
我在将图像从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您需要重新绘制图像?我最近回答了一个关于生成缩略图的问题(asp.net version of timthumb php class )使图像变得漂亮干净。
将其保存到内存流后,您只需将其写入 asp.net 响应流:
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.
Once you save it to the memory stream you just need to write it out to the asp.net response stream: