赞
踩
myImageCodecInfo = GetEncoderInfo("image/jpeg"); myEncoder = Encoder.Quality; myEncoderParameters = new EncoderParameters(1); Int64 ratio = Convert.ToInt64(TextBlock_Compress.Text); myEncoderParameter = new EncoderParameter(myEncoder, ratio); myEncoderParameters.Param[0] = myEncoderParameter; Bitmap bitMap = null; //Bitmap map2 = new Bitmap(img2); if (fused) { //using (Image img1 = Image.FromFile(sourcefiles[i_1])) { Image img1 = Image.FromFile(sourcefiles[i_1]); //Bitmap map1 = new Bitmap(img1); //using ()) { Image img2 = Image.FromFile(sourcefiles[i_1 + 1]); var width = img1.Width + img2.Width; var height = img1.Height; // 初始化画布(最终的拼图画布)并设置宽高 bitMap = new Bitmap(width, height); // 初始化画板 //using () { Graphics g1 = Graphics.FromImage(bitMap); // 将画布涂为白色(底部颜色可自行设置) g1.FillRectangle(System.Drawing.Brushes.White, new System.Drawing.Rectangle(0, 0, width, height)); //在x=0,y=0处画上图一 g1.DrawImage(img1, 0, 0, img1.Width, img1.Height); //在x=0,y在图一往下10像素处画上图二 g1.DrawImage(img2, img1.Width, 0, img2.Width, img2.Height); g1.Dispose(); } img1.Dispose(); img2.Dispose(); } } } else bitMap = new Bitmap(MyImg) if (FileSuffix == "png" || FileSuffix == "bmp" || FileSuffix == "jpg" || FileSuffix == "dds") { //Image img = new Bitmap(MyImg); if (Rb2png.IsChecked == true) { if (Cum_Textbox.IsEnabled == true) { if (Img_Name.Substring(0, 1) == "0") bitMap.Save(path + Cum_Textbox.Text + detect_time + ".png", myImageCodecInfo, myEncoderParameters); else bitMap.Save(path + Cum_Textbox.Text + detect_time + "_NOK.png", myImageCodecInfo, myEncoderParameters); } else { if (Img_Name.Substring(0, 1) == "0") bitMap.Save(path + detect_time + ".png", myImageCodecInfo, myEncoderParameters); else bitMap.Save(path + detect_time + "_NOK.png", myImageCodecInfo, myEncoderParameters); } } else if (Rb2jpg.IsChecked == true) { if (Cum_Textbox.IsEnabled == true) { if (Img_Name.Substring(0, 1) == "0") bitMap.Save(path + Cum_Textbox.Text + detect_time + ".jpg", myImageCodecInfo, myEncoderParameters); else bitMap.Save(path + Cum_Textbox.Text + detect_time + "_NOK.jpg", myImageCodecInfo, myEncoderParameters); } else { if (Img_Name.Substring(0, 1) == "0") bitMap.Save(path + detect_time + ".jpg", myImageCodecInfo, myEncoderParameters); else bitMap.Save(path + detect_time + "_NOK.jpg", myImageCodecInfo, myEncoderParameters); } } else if (Rb2BMP.IsChecked == true) { if (Cum_Textbox.IsEnabled == true) { if (Img_Name.Substring(0, 1) == "0") bitMap.Save(path + Cum_Textbox.Text + detect_time + ".bmp", myImageCodecInfo, myEncoderParameters); else bitMap.Save(path + Cum_Textbox.Text + detect_time + "_NOK.bmp", myImageCodecInfo, myEncoderParameters); } else { if (Img_Name.Substring(0, 1) == "0") bitMap.Save(path + detect_time + ".bmp", myImageCodecInfo, myEncoderParameters); else bitMap.Save(path + detect_time + "_NOK.bmp", myImageCodecInfo, myEncoderParameters); } } } else { MessageBox.Show("文件格式错误,请拖入图片文件(png,bmp,jpg)"); } if (bitMap != null) bitMap.Dispose(); myEncoderParameter.Dispose(); myEncoderParameters.Dispose(); //GC.Collect();
需要注意的是,在这段代码中,除了使用new新建的目标会在循环中出现内存泄漏之外,Graphics g1,Image img1,Image img2这几个新建的画图同样会在循环时造成内存泄漏,
现归纳三个方法防止泄漏:
一是每个目标使用完成后在末尾添加.Dospose()
二是在所有目标使用完成后,在结尾添加GC.Collect()
三是在创建目标时使用using,以使其在使用完成后自动释放
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。