c# - Bitmap.Save the whole mage is black, not just the background -
this question has answer here:
i taking screenshot of external application printwindow , want save desktop.
this code :
// proc process proc = process.getprocessesbyname("procname").single(); intptr hwnd = proc.mainwindowhandle; // restore proc if minimised int style = getwindowlong(hwnd, gwl_style); if ((style & ws_minimize) == ws_minimize) showwindow(hwnd, windowshowstyle.restore); // rect rect rect; getwindowrect(new handleref(this, hwnd), out rect); // screenshot int width = rect.right - rect.left; int height = rect.bottom - rect.top; bitmap bmp = new bitmap(width, height); using (graphics g = graphics.fromimage(bmp)) { intptr dc = g.gethdc(); if (!printwindow(hwnd, dc, 0)) { int error = marshal.getlastwin32error(); var exception = new system.componentmodel.win32exception(error); debug.writeline("error: " + error + ": " + exception.message); return; } //thread.sleep(200); bmp.save(environment.getfolderpath(environment.specialfolder.desktop) + @"\test.jpeg", imageformat.jpeg); panel1.backgroundimage = bmp; g.releasehdc(dc); }
the panel1 shows me image, actual screenshot of application. when go desktop, find test.jpeg it's black. why?
thanks !
i have successed @ saving .jpg changing code become :
// screenshot int width = rect.right - rect.left; int height = rect.bottom - rect.top; bitmap bmp = new bitmap(width, height, pixelformat.format24bpprgb); using (graphics g = graphics.fromimage(bmp)) { g.clear(color.white); intptr dc = g.gethdc(); if (!printwindow(hwnd, dc, 0)) { int error = marshal.getlastwin32error(); var exception = new system.componentmodel.win32exception(error); debug.writeline("error: " + error + ": " + exception.message); return; } g.releasehdc(dc); } // save screenshot bmp.save(environment.getfolderpath(environment.specialfolder.desktop) + @"\test.jpg", imageformat.jpeg); panel1.backgroundimage = bmp;
Comments
Post a Comment