首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# GDI+ 簡單繪圖(三):仿 QQ 螢幕截圖功能

     編輯:關於C#

效果預覽

截圖介面1 截圖介面2

實作原理

  1. 聊天視窗點擊「截圖」按鈕 → 隱藏自身視窗。
  2. 建立全螢幕無邊框視窗 CatchForm,背景設為螢幕截圖(Graphics.CopyFromScreen)。
  3. 在 CatchForm 上以滑鼠拖曳繪製矩形選取區域。
  4. 雙擊矩形內部 → 將選取區域圖片存入剪貼板 → 關閉 CatchForm → 顯示原視窗並貼上圖片。

關鍵技術:消除拖曳殘影(二次緩衝)

直接在視窗上繪製矩形會產生大量殘影。解法:

  • 保存原始螢幕截圖 originBmp。
  • 滑鼠移動時:Clone originBmp → 在副本上繪製目前矩形 → 將副本整張繪製到視窗 → 釋放副本。
  • 視窗每次重繪皆為完整畫面,無殘留矩形。

核心程式碼節錄

CatchForm 滑鼠事件

Point _downPt; bool _selecting = false, _finished = false;
Rectangle _selectRect; Bitmap _originBmp;

void CatchForm_Load() {
    SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
    _originBmp = new Bitmap(this.BackgroundImage);
}

void CatchForm_MouseDown(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left && !_selecting) {
        _selecting = true; _downPt = new Point(e.X, e.Y);
    }
}

void CatchForm_MouseMove(object sender, MouseEventArgs e) {
    if (!_selecting) return;
    Bitmap temp = (Bitmap)_originBmp.Clone();
    Point pt = new Point(Math.Min(_downPt.X, e.X), Math.Min(_downPt.Y, e.Y));
    int w = Math.Abs(e.X - _downPt.X), h = Math.Abs(e.Y - _downPt.Y);
    _selectRect = new Rectangle(pt, new Size(w, h));
    using (Graphics g = Graphics.FromImage(temp))
        g.DrawRectangle(Pens.Blue, _selectRect);
    using (Graphics g = this.CreateGraphics())
        g.DrawImage(temp, Point.Empty);
    temp.Dispose();
}

void CatchForm_MouseUp(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left && _selecting) { _selecting = false; _finished = true; }
}

void CatchForm_MouseDoubleClick(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left && _finished && _selectRect.Contains(e.Location)) {
        Bitmap crop = new Bitmap(_selectRect.Width, _selectRect.Height);
        using (Graphics g = Graphics.FromImage(crop))
            g.DrawImage(_originBmp, new Rectangle(0,0,_selectRect.Width,_selectRect.Height), _selectRect, GraphicsUnit.Pixel);
        Clipboard.SetImage(crop);
        crop.Dispose();
        this.DialogResult = DialogResult.OK; this.Close();
    }
}

void CatchForm_MouseClick(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Right) { this.DialogResult = DialogResult.Cancel; this.Close(); }
}

主視窗啟動邏輯

void btnCapture_Click() {
    if (chkHideCurrent.Checked) { this.Hide(); Thread.Sleep(50); }
    using (CatchForm cf = new CatchForm()) {
        Bitmap screen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        using (Graphics g = Graphics.FromImage(screen))
            g.CopyFromScreen(Point.Empty, Point.Empty, screen.Size);
        cf.BackgroundImage = screen;
        if (cf.ShowDialog() == DialogResult.OK) {
            IDataObject data = Clipboard.GetDataObject();
            if (data.GetDataPresent(DataFormats.Bitmap))
                richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
            Clipboard.Clear();
        }
    }
    this.Show();
}

重點總結

  • 全螢幕無邊框視窗模擬桌面截圖。
  • 二次緩衝(Clone 原圖 → 繪製 → 整張貼上)完美解決拖曳殘影。
  • 剪貼板作為跨視窗資料傳遞媒介。
  • 右鍵取消、雙擊確認、Esc 鍵可擴充為取消。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved