WPF 中 图片大小的压缩
求在WPF中 将图片压缩的方法
比如原先有500K的图,压缩成50K的
[解决办法]
自己找到解决方法了,真不容易
public void ystp(string filePath, string filePath_ystp) //压缩图片
{
//Bitmap
Bitmap bmp = null;
//ImageCoderInfo
ImageCodecInfo ici = null;
//Encoder
System.Drawing.Imaging.Encoder ecd = null;
//EncoderParameter
EncoderParameter ept = null;
//EncoderParameters
EncoderParameters eptS = null;
try
{
using (FileStream fs = new FileStream(@"C:\Users\HeroHua\Desktop\123.png", FileMode.Open))
{
bmp = new Bitmap(fs);
ici = this.getImageCoderInfo(@"image/png");
ecd = System.Drawing.Imaging.Encoder.Quality;
eptS = new EncoderParameters(1);
ept = new EncoderParameter(ecd, 10L);
eptS.Param[0] = ept;
bmp.Save(filePath_ystp, ici, eptS);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
bmp.Dispose();
ept.Dispose();
eptS.Dispose();
}
}
private ImageCodecInfo getImageCoderInfo(string coderType)// 获取图片编码类型信息
{
ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo retIci = null;
foreach (ImageCodecInfo ici in iciS)
{
if (ici.MimeType.Equals(coderType))
retIci = ici;
}
return retIci;
}