溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用C#仿QQ實現(xiàn)簡單的截圖功能

發(fā)布時間:2022-08-25 10:42:18 來源:億速云 閱讀:213 作者:iii 欄目:開發(fā)技術

這篇“怎么用C#仿QQ實現(xiàn)簡單的截圖功能”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“怎么用C#仿QQ實現(xiàn)簡單的截圖功能”文章吧。

實現(xiàn)功能

屏幕選擇區(qū)域截圖

開發(fā)環(huán)境

開發(fā)工具:Visual Studio 2013

.NET Framework版本:4.5

實現(xiàn)代碼

//將上一篇的內容改成以下內容
// pictureBox1.Image = bmp;
Form2 frm = new Form2();
frm.BaseImage = bmp;
frm.TopMost = true;
frm.Show();
/*Form2代碼*/
#region Dll引用
[DllImport("User32.dll", EntryPoint = "GetDC")]
private extern static IntPtr GetDC(IntPtr hWnd);

[DllImport("User32.dll", EntryPoint = "ReleaseDC")]
private extern static int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

[DllImport("User32.dll")]
public static extern int GetSystemMetrics(int hWnd);

const int DESKTOPVERTRES = 117;
const int DESKTOPHORZRES = 118;

const int SM_CXSCREEN = 0;
const int SM_CYSCREEN = 1;

#endregion

//<summary>
//獲取DPI縮放比例
//</summary>
//<param name="dpiscalex"></param>
//<param name="dpiscaley"></param>
public static void GetDPIScale(ref float dpiscalex, ref float dpiscaley)
{
    int x = GetSystemMetrics(SM_CXSCREEN);
    int y = GetSystemMetrics(SM_CYSCREEN);
    IntPtr hdc = GetDC(IntPtr.Zero);
    int w = GetDeviceCaps(hdc, DESKTOPHORZRES);
    int h = GetDeviceCaps(hdc, DESKTOPVERTRES);
    ReleaseDC(IntPtr.Zero, hdc);
    dpiscalex = (float)w / x;
    dpiscaley = (float)h / y;
}


public Bitmap BaseImage { get; set; }

Graphics picGraphics;
//記錄鼠標開始截圖的位置
int startX = 0, startY = 0;
//記錄鼠標結束截圖的位置
int endX = 0, endY = 0;
//記錄DPI縮放比例
float x = 0f, y = 0f;
public Form2()
{
    InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
    /* 初始化賦值*/

    GetDPIScale(ref x, ref y);
    picGraphics = pictureBox1.CreateGraphics();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    DrawRect(endX - startX, endY - startY,e.Graphics);
}

private void Form2_KeyPress(object sender, KeyPressEventArgs e)
{
    //按下esc退出
    if (e.KeyChar == 27)
    {
        this.Close();
    }
}

//完成截圖
private void lbSucess_Click(object sender, EventArgs e)
{
    int clip_w = (int)((endX - startX) * x) - 4, clip_h = (int)((endY - startY) * y) - 4;
    if (clip_w < 1 || clip_h < 1)
    {
        return;
    }
    //獲取截圖
    Bitmap clipBmp = new Bitmap(clip_w, clip_h);
    Graphics g = Graphics.FromImage(clipBmp);
    g.CopyFromScreen((int)(startX * x) + 2, (int)(startY * y) + 2, 0, 0, new Size(clip_w, clip_h), CopyPixelOperation.SourceCopy);
    //將截圖設置到剪切板
    Clipboard.SetImage(clipBmp);
    g.Dispose();
    this.Close();
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    //隱藏操作面板
    panel1.Visible = false;
    //記錄鼠標開始截圖的位置
    if (e.Button == MouseButtons.Left)
    {
        startX = e.X;
        startY = e.Y;
        endX = 0; endY = 0;
    }
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        //繪制截圖區(qū)域
        DrawRect(e.X - startX, e.Y - startY, picGraphics);
    }
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    //截圖完成
    if (e.Button == MouseButtons.Left)
    {
        endX = e.X;
        endY = e.Y;

        DrawRect(endX - startX, endY - startY, picGraphics);
        if (endX > startX && endY > startY)
        {
            //顯示操作面板
            Thread.Sleep(100);
            panel1.Location = new Point(e.X - panel1.Width, e.Y + 5);
            panel1.Visible = true;
           
        }
    }
}

//繪制截圖區(qū)域
private void DrawRect(int w, int h,Graphics g)
{
    Bitmap img = (Bitmap)BaseImage.Clone();

    //雙緩沖技術畫矩形,防止重影和抖動
    Graphics Painter = Graphics.FromImage(img);
    Painter.DrawRectangle(new Pen(Color.Red), startX * x, startY * y, w * x, h * y);
    g.DrawImage(img, 0, 0, img.Width / x, img.Height / y);

    Painter.Dispose();
    img.Dispose();
}

實現(xiàn)效果

怎么用C#仿QQ實現(xiàn)簡單的截圖功能

以上就是關于“怎么用C#仿QQ實現(xiàn)簡單的截圖功能”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI