日期:2014-05-17  浏览次数:20985 次

Image类的Save方法出错,内存泄漏还是文件独占无法释放?
代码如下:这是一个WPF应用程序,是我在做的一个取色器。

//mw为主窗体
 public FullWindow(Window mw)
        {
            InitializeComponent();
            this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            this.Left = 0;
            this.Top = 0;
            this.MouseDown+=
            delegate{
                this.Close();
                if (mw != null && mw.Visibility == Visibility.Hidden)
                    mw.Show();
            };
            double iHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
            double iWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
            //创建一个和屏幕一样大的bitmap
            System.Drawing.Image img = new Bitmap((int)iWidth, (int)iHeight);
            //从一个继承自image类的对象中创建Graphics对象
            Graphics g = Graphics.FromImage(img);
            //抓取全屏幕
            g.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), new System.Drawing.Size((int)iWidth, (int)iHeight));
            tpath= AppDomain.CurrentDomain.BaseDirectory.Replace(@"bin\Debug", "Images") + "1.bmp";
            img.Save(tpath, ImageFormat.Bmp);
            //以下代码将img设置为全屏窗体的背景图片。