.net - Generic GDI+ Error -


i generic gdi error following code. complies , executes fine fails generic gdi+ error. there way solve issue, or way take screenshot without using inter-op?

public function copyimagefromscreen(region int32rect) bitmapsource      dim img new system.drawing.bitmap(region.width, region.height)     dim gfx system.drawing.graphics = system.drawing.graphics.fromimage(img)     gfx.copyfromscreen(region.x, region.y, 0, 0, new system.drawing.size(region.width, region.height))     img.lockbits(new system.drawing.rectangle(0, 0, img.width, img.height), system.drawing.imaging.imagelockmode.readonly, img.pixelformat)      dim hobject intptr = img.gethbitmap 'this line causes error      dim wpfimage bitmapsource = interop.imaging.createbitmapsourcefromhbitmap(hobject, intptr.zero, _         system.windows.int32rect.empty, system.windows.media.imaging.bitmapsizeoptions.fromemptyoptions())      return wpfimage  end function 

this may or may not help, near can tell not cleaning of resources allocated in method. if doesn't help, it's best practice , proper clean disposable objects , not rely on gc.

that said, "classic" image support (i.e. system.drawing.image) in .net framework wrapper on native gdi/gdi+ libraries , prone leaking unmanaged resources.

what suggest wrapping img , gfx objects in using block, explicitly deleting hobject handle interop call deleteobject, enclosed in try/finally block in case createbitmapsourcefromhbitmap fails.

...why framework provides gethbitmap method on image class, not way delete without interop code mystery. check out msdn docs on gethbitmap more details on that.


Comments