c# - How can i get ap rocess window handle intptr and save the process window to image file? -
i have method drawtobitmap how handle of process window ? example notepad.exe
what want when start program if process(for example notepad.exe) not running yet start once it's running it's window handle , save window image file.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using elgato.baseclasses.ui; using elgato; using elgato.baseclasses.streaming; using system.diagnostics; using system.runtime.interopservices; namespace windowsformsapplication1 { public partial class form1 : form { [dllimport("user32.dll")] private static extern bool printwindow(handleref hwnd, intptr hdcblt, int nflags); [dllimport("user32.dll", setlasterror = true)] [return: marshalas(unmanagedtype.bool)] private static extern bool getwindowrect(intptr hwnd, ref rect lprect); [structlayout(layoutkind.sequential)] private struct rect { public int left; public int top; public int right; public int bottom; } public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { } private void button1_click(object sender, eventargs e) { } public static image drawtobitmap(intptr handle) { rect rect = new rect(); getwindowrect(handle, ref rect); bitmap image = new bitmap(rect.right - rect.left, rect.bottom - rect.top); using (graphics graphics = graphics.fromimage(image)) { intptr hdc = graphics.gethdc(); printwindow(new handleref(graphics, handle), hdc, 0); graphics.releasehdc(hdc); } return image; } } }
to check whether process exists , window handle if exists, use following code:
var processes = process.getprocessesbyname("notepad"); if (processes.length > 0) { var handle = processes[0].mainwindowhandle; } else { // notepad not started }
Comments
Post a Comment