截取桌面圖像,比較簡單,只需要簡單的調(diào)用Graphics的CopyFromScreen方法即可以實(shí)現(xiàn); 關(guān)于將圖像繪制到窗口上很容易實(shí)現(xiàn),但是卻很少看到有文章介紹從窗口上截取圖像的。下面主要介紹一下關(guān)于窗口圖像截取的方法。 要截取窗口的圖像,需要用到系統(tǒng)提供的BitBlt函數(shù),這個(gè)函數(shù)的作用就是從源設(shè)備的上下文中拷貝一張Bitmap圖像至目標(biāo)設(shè)備。具體參數(shù)介紹請參見MSDN文檔 下面是C#對該函數(shù)的引入操作: [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop); 具體操作代碼參見如下: Graphics gSrc = this.CreateGraphics(); //創(chuàng)建窗體的Graphics對象 HandleRef hDcSrc = new HandleRef(null, gSrc.GetHdc()); int width = this.Width-SystemInformation.FrameBorderSize.Width; //獲取寬度 int height = this.Height-SystemInformation.FrameBorderSize.Height; //獲取高度 const int SRCCOPY = 0xcc0020; //復(fù)制圖塊的光柵操作碼 Bitmap bmSave = new Bitmap(width, height); //用于保存圖片的位圖對象 Graphics gSave = Graphics.FromImage(bmSave); //創(chuàng)建該位圖的Graphics對象 HandleRef hDcSave = new HandleRef(null, gSave.GetHdc()); //得到句柄 BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY); 如此操作,當(dāng)前窗體的圖像即被保存之bmSave中。 下面提供一段詳細(xì)的代碼 - using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
-
- namespace ScreenShot
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void btnScreen_Click(object sender, EventArgs e)
- {
- int screenWidth = System.Windows.Forms.SystemInformation.VirtualScreen.Width;
- int screenHeight = System.Windows.Forms.SystemInformation.VirtualScreen.Height;
-
- Bitmap bmSave = new Bitmap(screenWidth, screenHeight);
- Graphics g = Graphics.FromImage(bmSave);
-
- g.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight), CopyPixelOperation.SourceCopy);
- g.Dispose();
-
- SaveFile(bmSave);
- }
-
- private void btnWindow_Click(object sender, EventArgs e)
- {
- Graphics gSrc = this.CreateGraphics();
- HandleRef hDcSrc = new HandleRef(null, gSrc.GetHdc());
-
- int width = this.Width-SystemInformation.FrameBorderSize.Width;
- int height = this.Height-SystemInformation.FrameBorderSize.Height;
-
- const int SRCCOPY = 0xcc0020;
-
- Bitmap bmSave = new Bitmap(width, height);
- Graphics gSave = Graphics.FromImage(bmSave);
- HandleRef hDcSave = new HandleRef(null, gSave.GetHdc());
-
- BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY);
-
- gSrc.ReleaseHdc();
- gSave.ReleaseHdc();
-
- gSrc.Dispose();
- gSave.Dispose();
-
- SaveFile(bmSave);
- }
- private void SaveFile(Bitmap bmSave)
- {
- if (DialogResult.OK == saveFileDialog1.ShowDialog())
- {
- string fileName = saveFileDialog1.FileName;
- if (1 == saveFileDialog1.FilterIndex)
- {
- if (!fileName.EndsWith(".bmp"))
- {
- fileName += ".bmp";
- }
- }
- else if (2 == saveFileDialog1.FilterIndex)
- {
- if (!fileName.EndsWith(".jpg"))
- {
- fileName += ".jpg";
- }
- }
- else if (3 == saveFileDialog1.FilterIndex)
- {
- if (!fileName.EndsWith(".png"))
- {
- fileName += ".png";
- }
- }
- Save(bmSave, fileName);
- }
- }
- private void Save(Bitmap bm,string fileName)
- {
- if (fileName.EndsWith(".bmp"))
- {
- bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
- }
- else if (fileName.EndsWith(".jpg"))
- {
- bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- else if (fileName.EndsWith(".png"))
- {
- bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
- }
- bm.Dispose();
- }
-
- [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
- public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
- }
- }
窗體設(shè)計(jì)部分代碼就省略了,測試窗體圖像如下:
|