代码如下:这是一个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设置为全屏窗体的背景图片。
            ImageBrush b = new ImageBrush();
            b.ImageSource = new BitmapImage(new Uri(tpath));
            b.Stretch = Stretch.Fill;
            (b.ImageSource as BitmapImage).CacheOption = BitmapCacheOption.None;
            this.Background = b;
            if (mw != null&&mw.Visibility==Visibility.Visible)
                mw.Hide();
        }现在的问题是,每当第一次运行该窗体时,则不报错,但是关闭该窗体再次打开后,就会在img.Save(tpath, ImageFormat.Bmp);//此代码处
报错。
另外,Save方法不能使用相对路径吗?迫不得已构造了绝对路径。帮我看看啊,纠结一下午了!bitmap图片

解决方案 »

  1.   

    错误为:gdi+发生了一般性错误。
      

  2.   

    我将“1.bmp”改为Guid.NewGuid.ToString("N")+".bmp",
    经过测试,发现在多次打开关闭该窗体之后,程序的内存占用越来越高,并且手动删除图片提示"file open in vshost32.exe"
      

  3.   

    b.ImageSource = new BitmapImage(new Uri(tpath));这个文件你又打开了,这时就被新的BitmapImage锁了。
    若想修改文件,需要关闭这个BitmapImage。
      

  4.   

    从你的代码看来,应该是取色器的窗体在Close的时候,相关资源并未释放造成的。
    首先是System.Drawing.Image img这里,你缺少了对img的dispose操作,save之后就应该dispose了,后面你再也不会用到那个img对象。
    然后是ImageBrush b = new ImageBrush();
    b.ImageSource = new BitmapImage(new Uri(tpath));
    这里将路径中的文件锁定了,只能读取不能写入,如果Close后并未直接释放其BitmapImage资源(可能被WPF缓存了),就会导致无法修改它了。
    你可以在img.Save的时候写入MemoryStream中,然后用这个MemoryStream写入到文件和加载BitmapImage,两不误。
      

  5.   

    楼上 +1  写到流里面,让另外一个Image对象去做保存
      

  6.   

                using (MemoryStream Ms = new MemoryStream())
                {
                    Img.Save(Ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    Image.FromStream(Ms).Save("FileName");
                }
      

  7.   

    你这是画蛇添足,MemoryStream直接写入文件不是更好?
      

  8.   

                MemoryStream Ms = new MemoryStream();
                Img.Save(Ms, System.Drawing.Imaging.ImageFormat.Bmp);
                FileStream Fs = new FileStream("FileName", FileMode.Create);
                Fs.Write(Ms.GetBuffer(), 0, Ms.GetBuffer().Length);
                Fs.Close();
                Ms.Close(); 这样写啊? 虽然是有点多余,不过这样写可以省两行代码
      

  9.   

    代码改成如下:......
    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));
                MemoryStream ms = new MemoryStream();
                img.Save(ms, ImageFormat.Bmp);
                ImageBrush b = new ImageBrush();
                BitmapImage bp = new BitmapImage();
                bp.BeginInit();
                bp.StreamSource = ms;
                bp.EndInit();
                b.ImageSource =bp;
                b.Stretch = Stretch.Fill;
                this.Background = b;
    ......
      

  10.   

    别忘了在close事件里关闭ms流