gdi+非独占打开图片

本文介绍了一种利用GDI+中的Bitmap类非独占加载图片的方法,避免了因图片被独占而无法进行读写的问题。通过使用IStream接口并配合SHCreateStreamOnFile函数,可以实现图片文件的共享读取。

gdi+打开图片,用Bitmap有很多构造函数,之前我一直使用

 

Bitmap(      
    const WCHAR *filename ,     BOOL useIcm );

 

但是,这个构造方法有个缺陷:图片竟然是以独占形式打开的!这样我就不能对他进行读写了。

 

我的解决方案是采用另一个构造函数:

Bitmap(      
    IStream *stream ,     BOOL useIcm );

 

 

 

IStream也要注意Release,否则虽然不是独占的,但也会因为有个计数被你拿着而导致图片无法删除。

 

:)

 

---THE END---

C# winfrom System.Runtime.InteropServices.ExternalException HResult=0x80004005 Message=GDI+ 中发生一般性错误。 Source=System.Drawing StackTrace: 在 System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) 在 EOLTest.EOLTEst站.CaptureFormScreenshot() 在 E:\RenZhong1\mes.testclient\模组绝缘测试1\重构版\RZEOLTest\模组接触点配方.cs 中: 第 157 行 在 EOLTest.EOLTEst站.button_保存模组接触点配方_Click(Object sender, EventArgs e) 在 E:\RenZhong1\mes.testclient\模组绝缘测试1\重构版\RZEOLTest\模组接触点配方.cs 中: 第 73 行 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 EOLTest.Program.Main() 在 E:\RenZhong1\mes.testclient\模组绝缘测试1\重构版\RZEOLTest\Program.cs 中: 第 23 行
05-24
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值