Take a screenshot of a window using C# .NET - missing pixel information -
i attempting take screenshot of window using c# .net calling windows api. came following code:
public void screenshotwindow(intptr windowhandle) { rect rect = new rect(); getwindowrect(windowhandle, out rect); int width = rect.right - rect.left; int height = rect.bottom - rect.top; intptr windowdevicecontext = getwindowdc(windowhandle); intptr destdevicecontext = createcompatibledc(windowdevicecontext); intptr bitmaphandle = createcompatiblebitmap(windowdevicecontext, width, height); intptr oldobject = selectobject(destdevicecontext, bitmaphandle); bitblt(destdevicecontext, 0, 0, width, height, windowdevicecontext, 0, 0, captureblt | srccopy); selectobject(destdevicecontext, oldobject); deletedc(destdevicecontext); releasedc(windowhandle, destdevicecontext); image screenshot = image.fromhbitmap(bitmaphandle); deleteobject(bitmaphandle); screenshot.save("c:\\screenshots\\" + windowhandle.tostring() + ".png", system.drawing.imaging.imageformat.png); }
it common series of windows api calls obtain window screenshot.
note not looking alternative ways of obtaining screenshots. to compare speed of (fixed) approach , speed of .net graphics.copyfromscreen()
method.
the problem is, when attempt take screenshot of maximized window running windows 7, titlebar , border (and other parts of window) black.
i think caused either fact window layered or because title bar of window managed window , therefore pixel information cannot accessed (as have read somewhere).
does have idea how fix behaviour?
you're calling kinds of apis should staying long distance away from, because taking screenshot comfortably covered in .net framework. it's simpler might think:
var screen = screen.primaryscreen; using (var bitmap = new bitmap(screen.bounds.width, screen.bounds.height)) using (var graphics = graphics.fromimage(bitmap)) { graphics.copyfromscreen(new point(screen.bounds.left, screen.bounds.top), new point(0, 0), screen.bounds.size); bitmap.save("test.png", imageformat.png); }
Comments
Post a Comment