# 学习记录1(C#-解决内存泄漏的几种方法)

这里写自定义目录标题

            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,以使其在使用完成后自动释放

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值