跨类库传递 System.Drawing.Bitmap 是否不可靠?
我有一个第 3 方 dll,它生成位图并发回其引用。如果我立即生成一个 System.Windows.Media.Imaging.BitmapSource 那么一切都会顺利。但是,如果我保存引用,然后(几秒钟和多次函数调用后)我尝试生成位图源,我得到
System.AccessViolationException 是 未由用户代码处理
消息=尝试读取或写入 受保护的内存。这通常是一个 表明其他内存正在 腐败。来源=“系统.绘图”
在做时:
System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
关于这里出了什么问题的任何线索吗?任何指针都会有用。谢谢。
I have a 3rd party dll which generates a Bitmap and send back its reference. If I generate a System.Windows.Media.Imaging.BitmapSource out of it immediately then all goes well. But if I save the reference and later on (after a few seconds and many function calls) I try to generate the Bitmapsource, I get
System.AccessViolationException was
unhandled by user code
Message=Attempted to read or write
protected memory. This is often an
indication that other memory is
corrupt. Source="System.Drawing"
when doing :
System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
Any clues on whats going wrong here ? Any pointers will be useful. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这表明 bmp.GetHBitmap 返回的句柄(对操作系统管理的资源的引用,而不是 .Net)不再有效 - 可能已在某处调用了 Dispose 或类似的内容(虽然不一定是你的代码)。
我建议使用另一种不依赖句柄的方式来保存位图数据 - 可能会立即流出位图本身的二进制数据,然后抛出对此的引用。
I think this indicates that the handle (reference to a resource managed by the operating system, rather than .Net) returned by
bmp.GetHBitmap
is no longer valid - possibly a Dispose has been called somewhere or something like that (not necessarily by your code though).I'd recommend using another way of persisting the bitmap data that does not rely on handles - possibly stream out the binary data of the bitmap itself immediately, and then throw a reference to that around.
我在位图和访问冲突方面也遇到了大问题。我相信正在发生的事情是某些位图构造函数在不应该打开文件句柄的情况下将文件句柄打开。因此,您正在运行的程序会检测到这些文件正在使用,而实际上它们不应该使用。
我最终找到了一个解决方案,即复制原始位图,然后处理原始位图。这是我的代码,它保留了原始位图的分辨率:
显然,对于第一行,您的代码将是 Bitmap Temp = MyThirdPartyDLL.GetBitmap();或者什么的。如果你不关心分辨率,它可以简化为:
进行此更改后,我能够执行各种文件 I/O 等,非常好,希望你也能这样做。
I had a big problem with Bitmaps and access violations as well. What I believe to be happening is that certain bitmap constructors leave file handles open when they should not. Thus, the program you are running detects that the files are in use, when they shouldn't be.
I eventually figured out a solution in that I make a copy of the original bitmap and then dispose the original. Here is my code, which preserves the resolution of the original Bitmap:
Obviously, for the first line, yours would be Bitmap Temp = MyThirdPartyDLL.GetBitmap(); or something. If you don't care about the resolution it can be simplified to:
After making this change, I was able to do all kinds of File I/O, etc, perfectly fine, hope you can do the same.