程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#實現類似qq的屏幕截圖程序

C#實現類似qq的屏幕截圖程序

編輯:C#基礎知識
因為近來想寫個類似於遠程桌面監控的程序,該程序中要用到屏幕捕捉.為實現該程序的一部分功能,做了個小DEMO.程序很簡單,用到的技術也不多,只能實現類似qq的截圖功能(方法雖然很笨)
程序流程如下:

1.截取整個屏幕並保存
2.新開一個全屏窗口,將保存的屏幕作為背景
3.鼠標拖動改變截取范圍,右鍵取消
4.雙擊截取,保存在粘貼板,全屏窗口關閉

好了,下面的是代碼部分

首先新建一個項目ScreenCutter(VS2005),將窗體名改為MainForm,再新建一個窗體ScreenBody.
添加一個按鈕btnCutter到ScreenCutter並添加按鈕事件:
        private void btnCutter_Click(object sender, EventArgs e)
         {
             Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
             Graphics g = Graphics.FromImage(img);
             g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
             ScreenBody body = new ScreenBody();
             body.BackgroundImage = img;
             body.Show();
         }Screen.AllScreens[0]是獲取當前所有設備窗口的第一個,我這裡只有一個顯示器,當然我就是第一個.
利用Graphics的CopyFromScreen函數獲取當前屏幕.

好了,現在按下按鈕全屏窗口就會出來了.

下面講全屏窗口ScreenBody,首先設置窗體的FormBorderStyle為None,然後聲明以下變量
private Graphics MainPainter;   //主畫筆
private Pen pen;                //就是筆咯
private bool isDowned;          //判斷鼠標是否按下
private bool RectReady;          //矩形是否繪制完成
private Image baseImage;        //基本圖形(原來的畫面)
private Rectangle Rect;         //就是要保存的矩形
private Point downPoint;         //鼠標按下的點
int tmpx;                     
int tmpy;
之後就是窗體的鼠標函數了,裡面很多代碼都沒有作出整理,看了一下,整理後的代碼應該會更少更精簡的



private void ScreenBody_DoubleClick(object sender, EventArgs e)
{
    if (((MouseEventArgs)e).Button == MouseButtons.Left &&Rect.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
     {
         //保存的時候有很多種方法的......我這裡只用了這種
         Image memory = new Bitmap(Rect.Width, Rect.Height);
         Graphics g = Graphics.FromImage(memory);
         g.CopyFromScreen(Rect.X + 1, Rect.Y + 1, 0, 0, Rect.Size);
         Clipboard.SetImage(memory);
        this.Close();
     }
}

private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
     {
         isDowned = true;

        if (RectReady == false)
         {
             Rect.X = e.X;
             Rect.Y = e.Y;
             downPoint = new Point(e.X, e.Y);
         }
        if (RectReady == true)
         {
             tmpx = e.X;
             tmpy = e.Y;
         }
     }
    if (e.Button == MouseButtons.Right)
     {
        if (RectReady != true)
         {
            this.Close();
            return;
         }
         MainPainter.DrawImage(baseImage, 0, 0);
         RectReady = false;
     }

}

private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
     {
         isDowned = false;
         RectReady = true;
     }
}

private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
{

    if (RectReady == false)
     {
        if (isDowned == true)
         {
             Image New = DrawScreen((Image)baseImage.Clone(), e.X, e.Y);
             MainPainter.DrawImage(New, 0, 0);
             New.Dispose();
         }
     }
    if (RectReady == true)
     {
        if (Rect.Contains(e.X, e.Y))
         {
            //this.Cursor = Cursors.Hand;
            if (isDowned == true)
             {
                //和上一次的位置比較獲取偏移量
                 Rect.X = Rect.X + e.X - tmpx;
                 Rect.Y = Rect.Y + e.Y - tmpy;
                //記錄現在的位置
                 tmpx = e.X;
                 tmpy = e.Y;
                 MoveRect((Image)baseImage.Clone(), Rect);
             }
         }
     }

}

private void ScreenBody_Load(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Maximized;
     MainPainter = this.CreateGraphics();
     pen = new Pen(Brushes.Blue);
     isDowned = false;
     baseImage = this.BackgroundImage;
     Rect = new Rectangle();
     RectReady = false;
}


輔助函數
本來應該寫更多的輔助函數的,將窗體響應函數裡面的代碼放到裡面來,不過本人很懶,就這樣將就了.呵呵


private void DrawRect(Graphics Painter, int Mouse_x, int Mouse_y)
{
    int width = 0;
    int heigth = 0;
    if (Mouse_y < Rect.Y)
     {
         Rect.Y = Mouse_y;
         heigth = downPoint.Y - Mouse_y;
     }
    else
     {
         heigth = Mouse_y - downPoint.Y;
     }
    if (Mouse_x < Rect.X)
     {
         Rect.X = Mouse_x;
         width = downPoint.X - Mouse_x;
     }
    else
     {
         width = Mouse_x - downPoint.X;
     }
     Rect.Size = new Size(width, heigth);
     Painter.DrawRectangle(pen, Rect);
}

private Image DrawScreen(Image back, int Mouse_x, int Mouse_y)
{
     Graphics Painter = Graphics.FromImage(back);
     DrawRect(Painter, Mouse_x, Mouse_y);
    return back;
}
private void MoveRect(Image image, Rectangle Rect)
{
     Graphics Painter = Graphics.FromImage(image);
     Painter.DrawRectangle(pen, Rect.X, Rect.Y, Rect.Width, Rect.Height);
     DrawRects(Painter);
     MainPainter.DrawImage(image, 0, 0);
     image.Dispose();
}

到這裡,代碼就算是寫完了,運行



截取結果,這裡截取的邊界沒有控制好,所以還有邊界可以見到,稍微設置一下就可以了



好了,這個東東就這樣搞完了,接下來要做利用鉤子的了....希望能夠快點完成,累呀~~~~
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved