跨类库传递 System.Drawing.Bitmap 是否不可靠?

发布于 2024-08-25 02:31:25 字数 602 浏览 3 评论 0原文

我有一个第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮坠入山谷 2024-09-01 02:31:25

我认为这表明 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.

诠释孤独 2024-09-01 02:31:25

我在位图和访问冲突方面也遇到了大问题。我相信正在发生的事情是某些位图构造函数在不应该打开文件句柄的情况下将文件句柄打开。因此,您正在运行的程序会检测到这些文件正在使用,而实际上它们不应该使用。

我最终找到了一个解决方案,即复制原始位图,然后处理原始位图。这是我的代码,它保留了原始位图的分辨率:

Bitmap Temp = new Bitmap(inFullPathName);
Bitmap Copy = new Bitmap(Temp.Width, Temp.Height);
Copy.SetResolution(Temp.HorizontalResolution, Temp.VerticalResolution);
using (Graphics g = Graphics.FromImage(Copy))
{
     g.DrawImageUnscaled(Temp, 0, 0);
}
Temp.Dispose();
return Copy;

显然,对于第一行,您的代码将是 Bitmap Temp = MyThirdPartyDLL.GetBitmap();或者什么的。如果你不关心分辨率,它可以简化为:

Bitmap Temp = MyThirdPartyDLL.GetBitmap();
Bitmap Copy = new Bitmap(Temp, Temp.Width, Temp.Height);
Temp.Dispose();
return Copy;

进行此更改后,我能够执行各种文件 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:

Bitmap Temp = new Bitmap(inFullPathName);
Bitmap Copy = new Bitmap(Temp.Width, Temp.Height);
Copy.SetResolution(Temp.HorizontalResolution, Temp.VerticalResolution);
using (Graphics g = Graphics.FromImage(Copy))
{
     g.DrawImageUnscaled(Temp, 0, 0);
}
Temp.Dispose();
return Copy;

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:

Bitmap Temp = MyThirdPartyDLL.GetBitmap();
Bitmap Copy = new Bitmap(Temp, Temp.Width, Temp.Height);
Temp.Dispose();
return Copy;

After making this change, I was able to do all kinds of File I/O, etc, perfectly fine, hope you can do the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文