GDI screen output, painting on the desktop of the current computer

xiaoxiao2021-03-06  55

Now you can draw on your desktop, but when you redraw, there is no solution yet.

using System; using System.Runtime.InteropServices; using System.Drawing; namespace WindowsApplication2 {public class WinScreen {[DllImport ( "gdi32.dll")] static extern IntPtr CreateDC (string lpDriverName, string lpDeviceName, string lpOutput, string lpInitData);

[DLLIMPORT ("gdi32.dll")] static extern INTPTR CREATECOMPATIBLEDC (INTPTR HDC);

[DLLIMPORT ("gdi32.dll")] static extern INTPTR CREATECOMPATIBLEBITMAP (INTPTR HDC, INT NWIDTH, INT NHEIGHT);

[DLLIMPORT ("GDI32.DLL")] static extern int getDevicecaps (INTPTR HDC, INT NINDEX);

[DLLIMPORT ("gdi32.dll")] static extern INTPTR SELECTOBJECT (INTPTR HDC, INTPTR HOBJECT);

[DLLIMPORT ("GDI32.DLL")] Static Extern Int Bitblt (INTSTHDC, INT SRCX, INT SRCY, INT SRCW, INT SRCH, INTPTR SRCHDC, INT DESTX, INT DESTY, INT OP);

[DLLIMPORT ("GDI32.DLL")] static extern int deletedc (INTPTR HDC);

[DllImport ( "gdi32.dll")] static extern int DeleteObject (IntPtr hObj); public static Bitmap CaptureControl (IntPtr SourceDC, int SourceWidth, int SourceHeight) {return Capture (SourceDC, SourceWidth, SourceHeight);}

public static Bitmap CaptureScreen () {IntPtr screen = CreateDC ( "DISPLAY", "", "", ""); int sourceWidth = GetDeviceCaps (screen, 8); int sourceHeight = GetDeviceCaps (screen, 10); Capture (screen, SourceWidth, SourceHeight; Bitmap Ret = Capture (Screen, SourceWidth, SourceHeight); deletedc (screen); return ret;}

static Bitmap Capture (IntPtr SourceDC, int SourceWidth, int SourceHeight) {IntPtr destDC; IntPtr BMP, BMPOld; destDC = CreateCompatibleDC (SourceDC); BMP = CreateCompatibleBitmap (SourceDC, SourceWidth, SourceHeight); BMPOld = SelectObject (destDC, BMP); BitBlt (destDC, 0, 0, SourceWidth, SourceHeight, SourceDC, 0, 0, 13369376); BMP = SelectObject (destDC, BMPOld); DeleteDC (destDC); Bitmap ret = Image.FromHbitmap (BMP); DeleteObject (BMP); return RET;} // Code for output on the desktop

public static void PrintScreen () {System.Windows.Forms.SendKeys.Send ( "{PRTSC}"); System.Windows.Forms.IDataObject obj = System.Windows.Forms.Clipboard.GetDataObject (); if (obj == null) return; if (obj.GetDataPresent (System.Windows.Forms.DataFormats.Bitmap)) {Image img = (Bitmap) obj.GetData (System.Windows.Forms.DataFormats.Bitmap); IntPtr screen = CreateDC ( "DISPLAY ",", ",", ""); INTPTR DESTDC; deSTDC = CreateCompatibleDC (Screen); graphics g = graphics.fromhdc (screen, destdc); g.drawImage (IMG, 200, 100); deletedc (screen);}} }

}

// Call the code

private void button1_Click (object sender, System.EventArgs e) {Bitmap bitmap = WinScreen.CaptureControl (button1.CreateGraphics () GetHdc (), button1.Width, button1.Height.); bitmap.Save (@ "c: / 1. BMP "); Bitmap = Winscreen.captureScreen (); Bitmap.save (@" c: /2.bmp "); Winscreen.PrintScreen ();

Can try it out

转载请注明原文地址:https://www.9cbs.com/read-40667.html

New Post(0)