C#截图

    公司里不知道搞了网络什么限制,我这个机器自古以来上午都登不上腾讯QQ客户端,每次都是上午用WebQQ,中午吃完饭回来才能登客户端。WebQQ用起来慢,而且卡不说,有时候截个图都没有客户端来的便捷。从网上虽然下载了一些截图软件,用起来都不大方便,摸索着自己从网上找了些关于C#截图方面的核心代码,打造了一款属于自己的Winform截图工具。

 

点此下载

    自己做的这款截图工具自我感觉用起来挺好的,大概介绍下使用方法,抓图的过程和其他的一样,抓完图后,双击左键复制到粘贴板,也可以在菜单里面操作,按ESC退出当前截图。

    先上个主界面和截图界面如下(截图软件就这么简单,自己闲来没事,在winform上模仿了个百度搜索,虽然画蛇添足,放上去后感觉给界面还添加了点灵气……):

C#截图

C#截图

C#截图

 

下面分享下核心代码:

  1. 左键按下开始绘图代码
    复制代码
     1         /// <summary>
     2         /// 按下左键开始绘制
     3         /// </summary>
     4         private void imageProcessBox1_MouseDown(object sender, MouseEventArgs e) {
     5             if (imageProcessBox1.Cursor != Cursors.SizeAll &&
     6                 imageProcessBox1.Cursor != Cursors.Default)
     7                 panel1.Visible = false;         //表示改变选取大小 隐藏工具条
     8             //若果在选取类点击 并且有选择工具
     9             if (e.Button == MouseButtons.Left && imageProcessBox1.IsDrawed && HaveSelectedToolButton()) {
    10                 if (imageProcessBox1.SelectedRectangle.Contains(e.Location)) {
    11                     m_ptOriginal = e.Location;
    12                     if (tBtn_Text.IsSelected) {         //如果选择的是绘制文本 弹出文本框
    13                         textBox1.Location = e.Location;
    14                         textBox1.Visible = true;
    15                         textBox1.Focus();
    16                         return;
    17                     }
    18                     m_isStartDraw = true;
    19                     Cursor.Clip = imageProcessBox1.SelectedRectangle;
    20                 }
    21             }
    22         }
    复制代码

     

  2. 移动鼠标开始截图代码
    复制代码
     1         /// <summary>
     2         /// 移动鼠标获取截图区域
     3         /// </summary>
     4         private void imageProcessBox1_MouseMove(object sender, MouseEventArgs e) {
     5             m_ptCurrent = e.Location;
     6             //根据是否选择有工具决定 鼠标指针样式
     7             if (imageProcessBox1.SelectedRectangle.Contains(e.Location) && HaveSelectedToolButton() && imageProcessBox1.IsDrawed)
     8                 this.Cursor = Cursors.Cross;
     9             else if (!imageProcessBox1.SelectedRectangle.Contains(e.Location))
    10                 this.Cursor = Cursors.Default;
    11 
    12             if (imageProcessBox1.IsStartDraw && panel1.Visible)   //在重置选取的时候 重置工具条位置(成立于移动选取的时候)
    13                 this.SetToolBarLocation();
    14 
    15             if (m_isStartDraw) {        //如果在区域内点下那么绘制相应图形
    16                 using (Graphics g = Graphics.FromImage(m_bmpLayerShow)) {
    17                     int tempWidth = 1;
    18                     if (toolButton2.IsSelected) tempWidth = 3;
    19                     if (toolButton3.IsSelected) tempWidth = 5;
    20                     Pen p = new Pen(colorBox1.SelectedColor, tempWidth);
    21 
    22                     #region   绘制矩形
    23 
    24                     if (tBtn_Rect.IsSelected) {
    25                         int tempX = e.X - m_ptOriginal.X > 0 ? m_ptOriginal.X : e.X;
    26                         int tempY = e.Y - m_ptOriginal.Y > 0 ? m_ptOriginal.Y : e.Y;
    27                         g.Clear(Color.Transparent);
    28                         g.DrawRectangle(p, tempX - imageProcessBox1.SelectedRectangle.Left, tempY - imageProcessBox1.SelectedRectangle.Top, Math.Abs(e.X - m_ptOriginal.X), Math.Abs(e.Y - m_ptOriginal.Y));
    29                         imageProcessBox1.Invalidate();
    30                     }
    31 
    32                     #endregion
    33 
    34                     #region    绘制圆形
    35 
    36                     if (tBtn_Ellipse.IsSelected) {
    37                         g.DrawLine(Pens.Red, 0, 0, 200, 200);
    38                         g.Clear(Color.Transparent);
    39                         g.DrawEllipse(p, m_ptOriginal.X - imageProcessBox1.SelectedRectangle.Left, m_ptOriginal.Y - imageProcessBox1.SelectedRectangle.Top, e.X - m_ptOriginal.X, e.Y - m_ptOriginal.Y);
    40                         imageProcessBox1.Invalidate();
    41                     }
    42 
    43                     #endregion
    44 
    45                     #region    绘制箭头
    46 
    47                     if (tBtn_Arrow.IsSelected) {
    48                         g.Clear(Color.Transparent);
    49                         System.Drawing.Drawing2D.AdjustableArrowCap lineArrow =
    50                             new System.Drawing.Drawing2D.AdjustableArrowCap(4, 4, true);
    51                         p.CustomEndCap = lineArrow;
    52                         g.DrawLine(p, (Point)((Size)m_ptOriginal - (Size)imageProcessBox1.SelectedRectangle.Location), (Point)((Size)m_ptCurrent - (Size)imageProcessBox1.SelectedRectangle.Location));
    53                         imageProcessBox1.Invalidate();
    54                     }
    55 
    56                     #endregion
    57 
    58                     #region    绘制线条
    59 
    60                     if (tBtn_Brush.IsSelected) {
    61                         Point ptTemp = (Point)((Size)m_ptOriginal - (Size)imageProcessBox1.SelectedRectangle.Location);
    62                         p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
    63                         g.DrawLine(p, ptTemp, (Point)((Size)e.Location - (Size)imageProcessBox1.SelectedRectangle.Location));
    64                         m_ptOriginal = e.Location;
    65                         imageProcessBox1.Invalidate();
    66                     }
    67 
    68                     #endregion
    69 
    70                     p.Dispose();
    71                 }
    72             }
    73         }
    复制代码

     

  3. 保存到粘贴板

    1         //将图像保存到剪贴板
    2         private void tBtn_Finish_Click(object sender, EventArgs e) {
    3             Clipboard.SetImage(m_bmpLayerCurrent);
    4             this.Close();
    5         }

     

  4. 保存图片到硬盘

    复制代码
     1 private void tBtn_Save_Click(object sender, EventArgs e) {
     2             SaveFileDialog saveDlg = new SaveFileDialog();
     3             saveDlg.Filter = "位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";
     4             saveDlg.FilterIndex = 1;
     5             saveDlg.FileName = "CAPTURE_" + GetTimeString();
     6             if (saveDlg.ShowDialog() == DialogResult.OK) {
     7                 switch (saveDlg.FilterIndex) {
     8                     case 1:
     9                         m_bmpLayerCurrent.Clone(new Rectangle(0, 0, m_bmpLayerCurrent.Width, m_bmpLayerCurrent.Height),
    10                             System.Drawing.Imaging.PixelFormat.Format24bppRgb).Save(saveDlg.FileName,
    11                             System.Drawing.Imaging.ImageFormat.Bmp);
    12                         this.Close();
    13                         break;
    14                     case 2:
    15                         m_bmpLayerCurrent.Save(saveDlg.FileName,
    16                             System.Drawing.Imaging.ImageFormat.Jpeg);
    17                         this.Close();
    18                         break;
    19                 }
    20             }
    21         }
    复制代码

     

    正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!旁边有“推荐”二字,你就顺手把它点了吧,相得准,我分文不收;相不准,你也好回来找我!

 
 http://www.cnblogs.com/wenyang-rio/archive/2013/01/23/2865958.html
 
标签:  分享

你可能感兴趣的:(分享)