【问题标题】:C#/WPF - Using Bitmap/SystemArgumentException ErrorC#/WPF - 使用位图/SystemArgumentException 错误
【发布时间】:2014-12-11 05:20:36
【问题描述】:

基本上,我需要从现有位图 (this.Document.Bitmap) 创建一个新位图,然后用同一属性中的新位图替换现有位图。我也希望处理此克隆可能导致的任何额外内存,但我收到此错误。

using 块之外的语句导致抛出此异常,我不知道为什么。帮忙?

System.Drawing.dll 中出现“System.ArgumentException”类型的未处理异常。

            using (Bitmap b = this.Document.Bitmap.Clone(new RectangleF() { Width = (int)this.croppingBorder.Width, Height = (int)this.croppingBorder.Height, X = (int)Canvas.GetLeft(this.croppingBorder), Y = (int)Canvas.GetTop(this.croppingBorder) }, this.Document.Bitmap.PixelFormat))
            {
                this.Document.Bitmap = b;
                BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(b.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                image1.Source = bs;
            }

            canvas1.Width = this.Document.Bitmap.Width;
            canvas1.Height = this.Document.Bitmap.Height;

【问题讨论】:

    标签: c# wpf bitmap


    【解决方案1】:

    using 处理位图。当您分配this.Document.Bitmap = b; 时,您只是在设置一个参考。但随后using 语句结束并处理位图b,这是this.Document.Bitmap 引用的位图。

    我想你想要的是:

    Bitmap b = this.Document.Bitmap.Clone(...);
    this.Document.Bitmap.Dispose();
    this.Document.Bitmap = b;
    

    【讨论】:

      【解决方案2】:

      通过使用“using”语句,您正在处理刚刚创建的位图对象。一旦您尝试访问它(例如,通过检索其宽度以分配给“canvas1.Width”),您就会收到错误消息。

      相反,您可能应该将对原始 Bitmap 的引用保存在临时变量中,将新的 Bitmap 实例分配给 Document.Bitmap 属性,然后显式释放原始 Bitmap。全部不用“using”语句。

      【讨论】:

        【解决方案3】:

        以下是您在遇到问题时正在做的事情:

        • 您正在将原始位图克隆到b
        • 你用克隆覆盖Document.B,但忘记释放旧值
        • 然后当您退出using 块时,位图b 也被处理为Document.B,因此尝试获取该位图的尺寸失败。
        • 此外,您应该在复制像素后处理 b.GetHBitmap()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-21
          • 1970-01-01
          • 2016-04-05
          • 1970-01-01
          • 1970-01-01
          • 2010-12-16
          • 2015-10-06
          • 1970-01-01
          相关资源
          最近更新 更多