日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C#模仿QQ截圖功能

 klv091 2011-03-01

最新接到任務(wù)需要給客戶制作個能圖片截取并合成編輯的小程序,研究了兩天GDI+,改了段C#截圖功能的代碼,現(xiàn)貼上來希望對大家有用

 

主文件

CaptureScreenForm.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace CaptureScreen
{
 /// <summary>
 /// CaptureScreenForm 的摘要說明。
 /// </summary>
 public class CaptureScreenForm : System.Windows.Forms.Form
 {
  private System.Windows.Forms.PictureBox picbox;
  private System.Windows.Forms.Button exitBtn;
  private System.Windows.Forms.Button aboutBtn;
  private System.Windows.Forms.Button ClearBtn;
  private System.Windows.Forms.Button saveBtn;
  private System.Windows.Forms.Button fullScreenBtn;
  private System.Windows.Forms.Button mouseCaptureBtn;

  public static Image image = null ;
  private System.Windows.Forms.Panel panel1;

  /// <summary>
  /// 必需的設(shè)計器變量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public CaptureScreenForm()
  {
   //
   // Windows 窗體設(shè)計器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗體設(shè)計器生成的代碼
  /// <summary>
  /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </summary>
  private void InitializeComponent()
  {
   this.picbox = new System.Windows.Forms.PictureBox();
   this.fullScreenBtn = new System.Windows.Forms.Button();
   this.mouseCaptureBtn = new System.Windows.Forms.Button();
   this.ClearBtn = new System.Windows.Forms.Button();
   this.saveBtn = new System.Windows.Forms.Button();
   this.aboutBtn = new System.Windows.Forms.Button();
   this.exitBtn = new System.Windows.Forms.Button();
   this.panel1 = new System.Windows.Forms.Panel();
   this.panel1.SuspendLayout();
   this.SuspendLayout();
   //
   // picbox
   //
   this.picbox.Location = new System.Drawing.Point(0, 40);
   this.picbox.Name = "picbox";
   this.picbox.Size = new System.Drawing.Size(680, 312);
   this.picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
   this.picbox.TabIndex = 0;
   this.picbox.TabStop = false;
   //
   // fullScreenBtn
   //
   this.fullScreenBtn.Location = new System.Drawing.Point(0, 0);
   this.fullScreenBtn.Name = "fullScreenBtn";
   this.fullScreenBtn.Size = new System.Drawing.Size(96, 32);
   this.fullScreenBtn.TabIndex = 1;
   this.fullScreenBtn.Text = "全屏截取";
   this.fullScreenBtn.Click += new System.EventHandler(this.fullScreenBtn_Click);
   //
   // mouseCaptureBtn
   //
   this.mouseCaptureBtn.Location = new System.Drawing.Point(120, 0);
   this.mouseCaptureBtn.Name = "mouseCaptureBtn";
   this.mouseCaptureBtn.Size = new System.Drawing.Size(96, 32);
   this.mouseCaptureBtn.TabIndex = 2;
   this.mouseCaptureBtn.Text = "鼠標(biāo)截取";
   this.mouseCaptureBtn.Click += new System.EventHandler(this.mouseCaptureBtn_Click);
   //
   // ClearBtn
   //
   this.ClearBtn.Location = new System.Drawing.Point(240, 0);
   this.ClearBtn.Name = "ClearBtn";
   this.ClearBtn.Size = new System.Drawing.Size(88, 32);
   this.ClearBtn.TabIndex = 3;
   this.ClearBtn.Text = "清除截取圖片";
   this.ClearBtn.Click += new System.EventHandler(this.ClearBtn_Click);
   //
   // saveBtn
   //
   this.saveBtn.Location = new System.Drawing.Point(352, 0);
   this.saveBtn.Name = "saveBtn";
   this.saveBtn.Size = new System.Drawing.Size(96, 32);
   this.saveBtn.TabIndex = 4;
   this.saveBtn.Text = "保存截取圖片";
   this.saveBtn.Click += new System.EventHandler(this.saveBtn_Click);
   //
   // aboutBtn
   //
   this.aboutBtn.Location = new System.Drawing.Point(472, 0);
   this.aboutBtn.Name = "aboutBtn";
   this.aboutBtn.Size = new System.Drawing.Size(96, 32);
   this.aboutBtn.TabIndex = 5;
   this.aboutBtn.Text = "關(guān)  于";
   this.aboutBtn.Click += new System.EventHandler(this.aboutBtn_Click);
   //
   // exitBtn
   //
   this.exitBtn.Location = new System.Drawing.Point(592, 0);
   this.exitBtn.Name = "exitBtn";
   this.exitBtn.Size = new System.Drawing.Size(88, 32);
   this.exitBtn.TabIndex = 6;
   this.exitBtn.Text = "退  出";
   this.exitBtn.Click += new System.EventHandler(this.exitBtn_Click);
   //
   // panel1
   //
   this.panel1.Controls.Add(this.fullScreenBtn);
   this.panel1.Controls.Add(this.mouseCaptureBtn);
   this.panel1.Controls.Add(this.ClearBtn);
   this.panel1.Controls.Add(this.saveBtn);
   this.panel1.Controls.Add(this.aboutBtn);
   this.panel1.Controls.Add(this.exitBtn);
   this.panel1.Location = new System.Drawing.Point(0, 0);
   this.panel1.Name = "panel1";
   this.panel1.Size = new System.Drawing.Size(680, 32);
   this.panel1.TabIndex = 7;
   //
   // CaptureScreenForm
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.AutoScroll = true;
   this.ClientSize = new System.Drawing.Size(696, 358);
   this.Controls.Add(this.panel1);
   this.Controls.Add(this.picbox);
   this.Name = "CaptureScreenForm";
   this.Text = "截取屏幕";
   this.panel1.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 應(yīng)用程序的主入口點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new CaptureScreenForm());
  }

  protected override void OnPaint(PaintEventArgs e)
  {
   base.OnPaint (e);
   if(image != null)
   {
    this.picbox.Image = image;
   }
   else
   {
    this.picbox.Image = null;
    this.picbox.Refresh();
   }
  }


  // 引用API

  [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
  public static extern bool BitBlt(
   IntPtr hdcDest, //目標(biāo)設(shè)備的句柄
   int nXDest, // 目標(biāo)對象的左上角的X坐標(biāo)
   int nYDest, // 目標(biāo)對象的左上角的X坐標(biāo)
   int nWidth, // 目標(biāo)對象的矩形的寬度
   int nHeight, // 目標(biāo)對象的矩形的長度
   IntPtr hdcSrc, // 源設(shè)備的句柄
   int nXSrc, // 源對象的左上角的X坐標(biāo)
   int nYSrc, // 源對象的左上角的X坐標(biāo)
   System.Int32 dwRop // 光柵的操作值
   );


  private void exitBtn_Click(object sender, System.EventArgs e)
  {
   Application.Exit();
  }

  private void aboutBtn_Click(object sender, System.EventArgs e)
  {
   MessageBox.Show("Code by MaxWeill in 2009-9-4,E_mail: maxweill@126.com ,thank you to use this.");
  }

  private void ClearBtn_Click(object sender, System.EventArgs e)
  {
   CaptureScreenForm.image = null;
   this.Invalidate();
  }

  private void saveBtn_Click(object sender, System.EventArgs e)
  {
   bool isSave = true;
   SaveFileDialog saveImageDialog = new SaveFileDialog();
   saveImageDialog.Title = "Capture screen image save dialog";
   saveImageDialog.Filter= @"jpeg*.jpgbmp*.bmpgif*.gif";

   if(saveImageDialog.ShowDialog() == DialogResult.OK)
   {
     string fileName = saveImageDialog.FileName.ToString();
    
    if(fileName != "" && fileName != null)
    {
     string fileExtName = fileName.Substring(fileName.LastIndexOf(".")+1).ToString();

     System.Drawing.Imaging.ImageFormat imgformat = null;     
 
     if(fileExtName!="")
     {
      switch(fileExtName)
      {
       case "jpg":
        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
        break;
       case "bmp":
        imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
        break;
       case "gif":
        imgformat = System.Drawing.Imaging.ImageFormat.Gif;
        break;
       default:
        MessageBox.Show("只能存取為: jpg,bmp,gif 格式");
        isSave = false;
        break;
      }

     }

     //默認(rèn)保存為JPG格式
     if(imgformat == null)
     {
      imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
     }
                   
     if(isSave)
     {
      try
      {
       CaptureScreenForm.image.Save(fileName,imgformat);
       MessageBox.Show("圖片已經(jīng)成功保存!~~");
      }
      catch
      {
       MessageBox.Show("保存失敗,你還沒有截取過圖片或已經(jīng)清空圖片!");
      }
     }

    }

   }
  }

  private void fullScreenBtn_Click(object sender, System.EventArgs e)
  {
      CaptureScreenForm.image = windowFullScreen();
   picbox.Image = CaptureScreenForm.image;
  }

  public static Bitmap windowFullScreen()
  {
   //建立屏幕Graphics
   Graphics grpScreen = Graphics.FromHwnd(IntPtr.Zero);
   //根據(jù)屏幕大小建立位圖
   Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,grpScreen);
   //建立位圖相關(guān)Graphics
   Graphics grpBitmap = Graphics.FromImage(bitmap);
            //建立屏幕上下文
   IntPtr hdcScreen = grpScreen.GetHdc();
   //建立位圖上下文
   IntPtr hdcBitmap = grpBitmap.GetHdc();
   //將屏幕捕獲保存在圖位中
   BitBlt(hdcBitmap,0,0,bitmap.Width,bitmap.Height,hdcScreen,0,0,0x00CC0020);
   //關(guān)閉位圖句柄
   grpBitmap.ReleaseHdc(hdcBitmap);
   //關(guān)閉屏幕句柄
   grpScreen.ReleaseHdc(hdcScreen);
   //釋放位圖對像
   grpBitmap.Dispose();
   //釋放屏幕對像
   grpScreen.Dispose();

   //返回捕獲位圖
   return bitmap;
  }

  private void mouseCaptureBtn_Click(object sender, System.EventArgs e)
  {
   MouseCaptureForm mouseCapture = new MouseCaptureForm();
   mouseCapture.Show();
  }
 }
}

另一個截屏文件:

MouseCaptureForm.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace CaptureScreen
{
 /// <summary>
 /// MouseCaptureForm 的摘要說明。
 /// </summary>
 public class MouseCaptureForm : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的設(shè)計器變量。
  /// </summary>
  //
  //自定義變量
  //
  private Point pot;
  private Rectangle area = Rectangle.Empty;
  private Image img;
  private int index = -1;
  private string noselectString   =  "\n.請按下鼠標(biāo)左鍵不放拖動選取截圖區(qū)域\n\n .單擊鼠標(biāo)右鍵或按ESC取消截圖";
  private string selectingString  =  "\n.松開鼠標(biāo)左鍵確定選取范圍\n\n.按ESC重新選擇";
  private string selectedString   =  "\n.按鼠標(biāo)左鍵調(diào)整選擇范圍\n\n .雙擊鼠標(biāo)左鍵保存截圖 \n\n.按鼠標(biāo)右鍵重新選擇 \n\n.ESC鍵取消截圖";
  private System.Windows.Forms.Label tiptext;
       
  //
  //系統(tǒng)生成定義
  //
  private System.ComponentModel.Container components = null;

  public MouseCaptureForm()
  {
   this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
   this.BackgroundImage = CaptureScreenForm.windowFullScreen(); 

   //
   // Windows 窗體設(shè)計器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗體設(shè)計器生成的代碼
  /// <summary>
  /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </summary>
  private void InitializeComponent()
  {
   this.tiptext = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // tiptext
   //
   this.tiptext.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
   this.tiptext.ForeColor = System.Drawing.Color.White;
   this.tiptext.Location = new System.Drawing.Point(10, 10);
   this.tiptext.Name = "tiptext";
   this.tiptext.Size = new System.Drawing.Size(166, 140);
   this.tiptext.TabIndex = 0;
   this.tiptext.MouseMove += new System.Windows.Forms.MouseEventHandler(this.tiptext_MouseMove);
   //
   // MouseCaptureForm
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(456, 320);
   this.Controls.Add(this.tiptext);

   this.DoubleBuffered = true;
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
   this.Name = "MouseCaptureForm";
   this.ShowInTaskbar = false;
   this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds;
   this.Text = "鼠標(biāo)截屏";
   this.TopMost = true;
   this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
   this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MouseCaptureForm_KeyDown);
   this.Load += new System.EventHandler(this.MouseCaptureForm_Load);
   this.DoubleClick += new System.EventHandler(this.MouseCaptureForm_DoubleClick);
   this.ResumeLayout(false);

  }


  #endregion

  /// <summary>
  /// 重載MouseDown
  /// </summary>
  ///
  protected override void OnMouseDown(MouseEventArgs e)
  {    
   base.OnMouseDown (e);
    {
   if(this.area == Rectangle.Empty && e.Button == MouseButtons.Left)
   {
    this.tiptext.Text = selectingString;
    this.area.Location = new Point(e.X,e.Y);
   }
   this.pot = new Point(e.X,e.Y);
   this.index = this.GetSelectedHandle(new Point(e.X,e.Y));
   this.SetCursor();    
    }
  }

  /// <summary>
  /// 設(shè)置鼠標(biāo)方案
  /// </summary>
  private void SetCursor()
  {
   Cursor cr = Cursors.Default;
 
   if(index == 1&& index == 5)
   {
    cr = Cursors.SizeNWSE;
   }
   else if(index == 2 && index == 6)
   {
    cr = Cursors.SizeNS;
   }
   else if(index == 3&&  index == 7)
   {
    cr = Cursors.SizeNESW;
   }
   else if(index == 4 && index == 8)
   {
    cr = Cursors.SizeWE;
   }
   else if(index == 0)
   {
    cr = Cursors.SizeAll;
   }
   Cursor.Current = cr;
  }

  protected override void OnPaint(PaintEventArgs e)
  {
   base.OnPaint(e);
   e.Graphics.DrawRectangle(new Pen(this.ForeColor),this.area);
 
   for(int i = 1;i < 9;i++)
   {
    e.Graphics.FillRectangle(new SolidBrush(Color.Green),this.GetHandleRect(i));
   }
  }

  private Rectangle GetHandleRect(int index)
  {
   Point point = GetHandle(index);
   return new Rectangle(point.X - 3, point.Y - 3, 6, 6);
  }

  private Point GetHandle(int index)
  {
   int x, y, xCenter, yCenter;

   xCenter = area.X + area.Width/2;
   yCenter = area.Y + area.Height/2;
   x = area.X;
   y = area.Y;

   switch ( index )
   {
    case 1:
     x = area.X;
     y = area.Y;
     break;
    case 2:
     x = xCenter;
     y = area.Y;
     break;
    case 3:
     x = area.Right;
     y = area.Y;
     break;
    case 4:
     x = area.Right;
     y = yCenter;
     break;
    case 5:
     x = area.Right;
     y = area.Bottom;
     break;
    case 6:
     x = xCenter;
     y = area.Bottom;
     break;
    case 7:
     x = area.X;
     y = area.Bottom;
     break;
    case 8:
     x = area.X;
     y = yCenter;
     break;
   }

   return new Point(x, y);
  }

  protected override void OnMouseUp(MouseEventArgs e)
  {
   base.OnMouseUp (e);
   int left = area.Left;
   int top = area.Top;
   int right = area.Right;
   int bottom = area.Bottom;
   area.X = Math.Min(left,right);
   area.Y = Math.Min(top,bottom);
   area.Width = Math.Abs(left-right);
   area.Height = Math.Abs(top-bottom);
   this.tiptext.Text = selectedString;
   if(e.Button == MouseButtons.Right)
   {
    if(this.area == Rectangle.Empty)
    {
     this.Close();
    }
    else
    {
     this.area=Rectangle.Empty;
     this.Invalidate();
    }
   }
   this.index = this.GetSelectedHandle(new Point(e.X,e.Y));
   this.SetCursor();
  }

  private int GetSelectedHandle(Point p)
  {
   int index = -1;
   for(int i = 1;i < 9;i++)
   {
    if(GetHandleRect(i).Contains(p))
    {
     index=i;
     break;
    }
   }
   if(this.area.Contains(p))index = 0;

   return index;
  }

  protected override void OnMouseMove(MouseEventArgs e)
  {
   base.OnMouseMove (e);
   if(this.Capture)
   {
    this.MoveHandleTo(new Point(e.X,e.Y));
    this.Invalidate();
   }
   else
   {
    this.index = this.GetSelectedHandle(new Point(e.X,e.Y));
    this.SetCursor();
   }
  }

  private void MoveHandleTo(Point point)
  {
   int left = area.Left;
   int top = area.Top;
   int right = area.Right;
   int bottom = area.Bottom;

   switch ( index )
   {
    case 0:
     area.X +=point.X - this.pot.X;
     area.Y += point.Y - pot.Y;
     this.pot = point;
     return;
    case 1:
     left = point.X;
     top = point.Y;
     break;
    case 2:
     top = point.Y;
     break;
    case 3:
     right = point.X;
     top = point.Y;
     break;
    case 4:
     right = point.X;
     break;
    case 5:
     right = point.X;
     bottom = point.Y;
     break;
    case 6:
     bottom = point.Y;
     break;
    case 7:
     left = point.X;
     bottom = point.Y;
     break;
    case 8:
     left = point.X;
     break;
   }
   this.pot = point;
   area.X = left;
   area.Y = top;
   area.Width = right - left;
   area.Height = bottom - top;
  }

  private void MouseCaptureForm_DoubleClick(object sender, System.EventArgs e)
  {
   //修正截取圖片過界提示內(nèi)存不足BUG
   int left   =  area.Left;
   int right  =  area.Right;
   int top    =  area.Top;
   int bottom =  area.Bottom;
   if(left < Screen.PrimaryScreen.Bounds.Left)
   {left = Screen.PrimaryScreen.Bounds.Left; }
   if(right > Screen.PrimaryScreen.Bounds.Right )
   {right = Screen.PrimaryScreen.Bounds.Right;}
   if(top < Screen.PrimaryScreen.Bounds.Top)
   {top = Screen.PrimaryScreen.Bounds.Top;}
   if(bottom > Screen.PrimaryScreen.Bounds.Bottom)
   {bottom = Screen.PrimaryScreen.Bounds.Bottom;}
   area.X = left;
   area.Y = top;
   area.Width = right - left;
   area.Height = bottom - top;

   //截取選擇區(qū)域圖片
   Bitmap bm  = new Bitmap(this.BackgroundImage);
   this.img = bm.Clone(this.area,System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);
   CaptureScreenForm.image = img;
   CaptureScreenForm.ActiveForm.Invalidate();
   this.Close();
  }

  private void MouseCaptureForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
   if(e.KeyData == Keys.Escape && System.Windows.Forms.Button.MouseButtons == MouseButtons.Left)
   {
    this.area=Rectangle.Empty;
    this.Invalidate(); 
    tiptext.Text = noselectString;
   }
   else if(e.KeyData == Keys.Escape)
   {this.Close();}
  }

  private void tiptext_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   if (tiptext.Location.X == 10 && tiptext.Location.Y == 10)
   {
    Point formloc = new Point((Screen.PrimaryScreen.Bounds.Width - 176),10) ;
    tiptext.Location = formloc;
   }
   else
   {tiptext.Location = new Point(10,10);}    
  }

  private void MouseCaptureForm_Load(object sender, System.EventArgs e)
  {
   tiptext.Text = noselectString;
  }
 }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多