Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “无法冻结传递给ImageVisualManager的图像”WPF异常_C#_Wpf_Bitmap - Fatal编程技术网

C# “无法冻结传递给ImageVisualManager的图像”WPF异常

C# “无法冻结传递给ImageVisualManager的图像”WPF异常,c#,wpf,bitmap,C#,Wpf,Bitmap,我正在尝试将视觉效果渲染到我在窗口创建中缓存的WPF RenderTargetBitmap RenderTargetBitmap rtb; 在我的窗口中 public FooWindow() { rtb = new RenderTargetBitmap(500, 500, 96, 96, PixelFormats.Pbgra32); } 稍后,在用户调用的代码中请求屏幕截图- // ... Window window = Window.GetWindow(this);

我正在尝试将视觉效果渲染到我在窗口创建中缓存的WPF RenderTargetBitmap

 RenderTargetBitmap rtb;
在我的窗口中

 public FooWindow()
 {
     rtb = new RenderTargetBitmap(500, 500, 96, 96, PixelFormats.Pbgra32);
 }
稍后,在用户调用的代码中请求屏幕截图-

 // ... 
 Window window = Window.GetWindow(this);
 rtb.Render(window);
 rtb.Freeze();

 // rtb used further
 BitmapFrame bmpFrame = BitmapFrame.Create(renderTargetBitmap);
 // ... 
但是,我在rtb.Render上遇到以下异常

[System.ArgumentException]: {"The Image passed to the ImageVisualManager cannot be frozen."}

它似乎第一次工作,但问题是冻结RenderTargetBitmap,然后尝试重新使用它。要解决此问题,您可以执行以下两种操作之一

为每个请求创建新的渲染目标位图 不要冻结渲染目标位图删除“rtb.freeze行”并继续使用同一实例。考虑到缓存单个实例并尝试重用它的使用,您根本不想冻结它。 在对性能敏感的代码中不建议使用解决方案1,因为创建RenderTargetBitmap可能会很昂贵。如果你希望重新使用它,就没有必要冻结它