当前位置:   article > 正文

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

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

需要注意的是,在这段代码中,除了使用new新建的目标会在循环中出现内存泄漏之外,Graphics g1,Image img1,Image img2这几个新建的画图同样会在循环时造成内存泄漏,
现归纳三个方法防止泄漏:
一是每个目标使用完成后在末尾添加.Dospose()
二是在所有目标使用完成后,在结尾添加GC.Collect()
三是在创建目标时使用using,以使其在使用完成后自动释放

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/115375
推荐阅读
相关标签
  

闽ICP备14008679号