我正在运行多线程图像压缩过程。原始文件是1280x 960高分辨率PNG文件,大约1800KB。我需要压缩到< 70KB JPEG文件。当我处理几辆车时,这个过程运行良好。当我处理超过20辆车时,我开始出现内存错误。这是代码。
private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo []编码器; encoders = ImageCodecInfo.GetImageEncoders(); for(j = 0; j< encoders.Length; ++ j) { if(encoders [j] .MimeType == mimeType) 返回编码器[j]; } return null; }
public static void SaveAsJpg(string inFilePath = null, string outputFileName = null, long compression = 70, long quality = 70)
{
System.Drawing.Image orgimage = System.Drawing.Image.FromFile(inFilePath);
var imgIn = new Bitmap(orgimage);
var imgOut = new Bitmap(imgIn.Width, imgIn.Height);
Graphics g = Graphics.FromImage(imgOut);
g.Clear(Color.White);
g.DrawImage(imgIn, 0, 0, imgIn.Width, imgIn.Height);
EncoderParameters encoding = new EncoderParameters(2);
encoding.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, compression);
encoding.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
imgOut.Save(outputFileName, myImageCodecInfo, encoding);
}
提前感谢任何建议。
答案 0 :(得分:0)
我认为你需要在离开方法之前处理图形对象。
g.Dispose();
orgiImage.Dispose();
imgIn.Dispose();
imgOut.Dispose();