GDI螢幕輸出,在當前電腦的案頭上進行繪畫

來源:互聯網
上載者:User

現在可以在案頭上進行繪畫了,但是當何時進行重繪,現在還沒有解決方案。

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(IntPtr desthDC, 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;
  }

//在案頭上進行輸出的代碼

  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);
   }
  }
 }

}

//調用代碼

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();
  }

可以試一下來看看

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.