Screen Capture

出自ProgWiki
跳至導覽 跳至搜尋

Screen Capture(抓圖)

抓圖方式

GDI

  • MFC方式,抓圖到
TonyJpegLib的CDib m_dib;
以方便存成不同格式的圖檔

GDI+

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; 
 
namespace ImageToolkit
{ 
    public class ScreenCapture
    {
        //全螢幕
        public static Bitmap CaptureFullScreen()
        {
            int width = Screen.PrimaryScreen.Bounds.Width;
            int height = Screen.PrimaryScreen.Bounds.Height;
            Bitmap img = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            using (Graphics g = Graphics.FromImage(img))
            {
                g.CopyFromScreen(
                    new Point(0,0), 
                    new Point(0, 0), 
                    new Size(width, height));
            }
            return img;
        }
 
        //視窗(Windows Form)
        public static Bitmap CaptureFullForm(Form form1)
        {
            int width = form1.Width;
            int height = form1.Height;
            Bitmap img = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            using (Graphics g = Graphics.FromImage(img))
            {
                g.CopyFromScreen(
                    new Point(form1.Location.X, form1.Location.Y),
                    new Point(0, 0),
                    new Size(width, height));
            }
            return img;
        }
 
        //視窗(Windows Form的Client)
        public static Bitmap CaptureFormClient(Form form1)
        {
            int width = form1.ClientSize.Width;
            int height = form1.ClientSize.Height;
            Bitmap img = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            using (Graphics g = Graphics.FromImage(img))
            {
                g.CopyFromScreen(
                    new Point(form1.RectangleToScreen(form1.ClientRectangle).X
                            , form1.RectangleToScreen(form1.ClientRectangle).Y),
                    new Point(0, 0),
                    new Size(width, height));
            }
            return img;
        }
    }
}

相關