ASP.NET convert image from PNG to jpg
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 hig开发者_如何学编程h 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();
}
}
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()
精彩评论