我发布了这个其他主题,得到了一些温和的回复,但虽然答案解决了问题的质量方面,但他们用kb中的图像大小创建了另一个问题。 Asp.net image resizing quality
这是问题所在。
我有一个2MB的图像,我想减少到480px的宽度。
我使用了三种不同的方法来调整它的大小:
1)在Fileupload上运行此代码:
System.IO.Stream st = FileUploadPost.PostedFile.InputStream;
myImage = System.Drawing.Image.FromStream(st);
thumb = myImage.GetThumbnailImage(newWidth, newHeight, null, System.IntPtr.Zero);
thumb.Save(myPath);
好处:kb的尺寸变小(约80Kb)。
缺点:视觉质量太可怕了
2)在Fileupload上运行此代码(在上述帖子中提供的解决方案):
Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
newGraphic.Dispose();
newImg.Save(myPath);
好处:视觉质量很好
下行:规模持续很大(约400kb)
3)使用Windows Paint软件并“手动”将图像缩小为480px宽度
好处:1)和2)的好处 - >视觉质量良好,尺寸减小到约80kb
问题是:
什么是重现第3项行为的代码(良好的视觉质量和较小的kb尺寸)?
由于
答案 0 :(得分:4)
尝试以下方法之一:
Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
newGraphic.Dispose();
newImg.Save(myPath);
或
Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
newGraphic.Dispose();
newImg.Save(myPath);
或
Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
newGraphic.Dispose();
newImg.Save(myPath);
这应该只会略微降低质量并大幅减小尺寸,但我不确定哪一种会更好。
以下是一大堆其他选项:
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat%28v=vs.110%29.aspx
答案 1 :(得分:1)
protected void Page_Load(object sender, EventArgs e)
{
//BR**
string filePath = "";//Source file path with image name
int CompressLevel = 50;//Image compression leve as per our requirement
string DestintionPath = "";//Destination file path
string Filename = "";//Output image name to save in destination path
Stream bmpStream = System.IO.File.Open(filePath, System.IO.FileMode.Open);
Bitmap bmp1 = new Bitmap(bmpStream);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, CompressLevel);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(DestintionPath + "\\" + Filename, jpgEncoder, myEncoderParameters);
//BR**
bmpStream.Close();
string lblmsg = "Compressed Sucessfully with Compression Level { " + CompressLevel.ToString() + " }";
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
/*/ Hope this will work.Thanks in advance /*/
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}