如何使用c#实现类似QQ截图那样的效果呢??看了晚上很多教程,但是都看不懂~~!请高手们指点迷津!不要很多效果,就实现鼠标截图即可

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                    //获得当前屏幕的分辨率
                Screen scr = Screen.PrimaryScreen;
                Rectangle rc = scr.Bounds;
                int iWidth = rc.Width;   
                int iHeight = rc.Height;
                    //创建一个和屏幕一样大的Bitmap
                Image myImage = new Bitmap(iWidth, iHeight); 
                    //从一个继承自Image类的对象中创建Graphics对象
                Graphics g = Graphics.FromImage(myImage);
                    //抓屏并拷贝到myimage里
                g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
                    //保存为文件
                myImage.Save(@"c:/1.jpeg");        }    }
    }
      

  2.   

    建议楼主结合楼上的方法和MouseDown事件来做
      

  3.   

    Catch.cs文件using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace test
    {
        public partial class Catch : Form
        {
            public Catch()
            {
                InitializeComponent();
            }
            #region 用户变量
            private Point DownPoint = Point.Empty;//记录鼠标按下坐标,用来确定绘图起点
            private bool CatchFinished = false;//用来表示是否截图完成
            private bool CatchStart = false;//表示截图开始
            private Bitmap originBmp;//用来保存原始图像
            private Rectangle CatchRect;//用来保存截图的矩形
            #endregion        private void Catch_Load(object sender, EventArgs e)
            {
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
                this.UpdateStyles();
                 //以上两句是为了设置控件样式为双缓冲,这可以有效减少图片闪烁的问题,关于这个大家可以自己去搜索下
                originBmp = new Bitmap(this.BackgroundImage);//BackgroundImage为全屏图片,我们另用变量来保存全屏图片
            }
            //鼠标右键点击结束截图
            private void Catch_MouseClick(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            //鼠标左键按下时动作
            private void Catch_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (!CatchStart)
                    {//如果捕捉没有开始
                        CatchStart = true;
                        DownPoint = new Point(e.X, e.Y);//保存鼠标按下坐标
                    }
                }
            }
            private void Catch_MouseMove(object sender, MouseEventArgs e)
            {
                if (CatchStart)
                {//如果捕捉开始
                    Bitmap destBmp = (Bitmap)originBmp.Clone();//新建一个图片对象,并让它与原始图片相同
                    Point newPoint = new Point(DownPoint.X, DownPoint.Y);//获取鼠标的坐标
                    Graphics g = Graphics.FromImage(destBmp);//在刚才新建的图片上新建一个画板
                    Pen p = new Pen(Color.Blue, 1);
                    int width = Math.Abs(e.X - DownPoint.X), height = Math.Abs(e.Y - DownPoint.Y);//获取矩形的长和宽
                    if (e.X < DownPoint.X)
                    {
                        newPoint.X = e.X;
                    }
                    if (e.Y < DownPoint.Y)
                    {
                        newPoint.Y = e.Y;
                    }
                    CatchRect = new Rectangle(newPoint, new Size(width, height));//保存矩形
                    g.DrawRectangle(p, CatchRect);//将矩形画在这个画板上
                    g.Dispose();//释放目前的这个画板
                    p.Dispose();
                    Graphics g1 = this.CreateGraphics();//重新新建一个Graphics类
                    //如果之前那个画板不释放,而直接g=this.CreateGraphics()这样的话无法释放掉第一次创建的g,因为只是把地址转到新的g了.如同string一样
                    g1 = this.CreateGraphics();//在整个全屏窗体上新建画板
                    g1.DrawImage(destBmp, new Point(0, 0));//将刚才所画的图片画到这个窗体上
                    //这个也可以属于二次缓冲技术,如果直接将矩形画在窗体上,会造成图片抖动并且会有无数个矩形.
                    g1.Dispose();
                    destBmp.Dispose();//要及时释放,不然内存将会被大量消耗            }
            }
            private void Catch_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (CatchStart)
                    {
                        CatchStart = false;
                        CatchFinished = true;                }
                }
            }
             //鼠标双击事件,如果鼠标位于矩形内,则将矩形内的图片保存到剪贴板中
            private void Catch_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left&&CatchFinished)
                {
                    if (CatchRect.Contains(new Point(e.X, e.Y)))
                    {
                        Bitmap CatchedBmp = new Bitmap(CatchRect.Width, CatchRect.Height);//新建一个于矩形等大的空白图片
                        Graphics g = Graphics.FromImage(CatchedBmp);
                        g.DrawImage(originBmp, new Rectangle(0, 0, CatchRect.Width, CatchRect.Height), CatchRect, GraphicsUnit.Pixel);
                        //把orginBmp中的指定部分按照指定大小画在画板上
                        Clipboard.SetImage(CatchedBmp);//将图片保存到剪贴板
                        g.Dispose();
                        CatchFinished = false;
                        this.BackgroundImage = originBmp;
                        CatchedBmp.Dispose();
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                }
            }
        }
    }
    Catch .Designer.cs
    namespace test
    {
        partial class Catch
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.SuspendLayout();
                // 
                // Catch
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(656, 542);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.Name = "Catch";
                this.Text = "Catch";
                this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
                this.Load += new System.EventHandler(this.Catch_Load);
                this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Catch_MouseUp);
                this.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.Catch_MouseDoubleClick);
                this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Catch_MouseClick);
                this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Catch_MouseDown);
                this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Catch_MouseMove);
                this.ResumeLayout(false);        }        #endregion    }
    }
      

  4.   

    C# 实现类类于QQ的截图控件
    http://topic.csdn.net/u/20090911/14/5614a16e-ab08-4106-901e-e8dcc5cd1fa3.html?36476
      

  5.   

    感谢大家给予帮助~~~小弟学习ing!
      

  6.   

    angle R = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
    Image img = new Bitmap(R.Width, R.Height);
    Graphics G = Graphics.FromImage(img);
    G.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(R.Width, R.Height));。  
    IntPtr dc = G.GetHdc();   
    G.ReleaseHdc(dc);
    G.Dispose();
    img .Save("c:\\a.jpg");

    private static extern bool BitBlt(IntPtr hdcDest,int nXDest,int nYDest,int nWidth,int nHeight,IntPtr hdcSrc,int nXSrc,int nYSrc,Int32 dwrop);http://topic.csdn.net/u/20090911/14/5614a16e-ab08-4106-901e-e8dcc5cd1fa3.html
      

  7.   

    没想到能Copy,我都是用Api来实现的.
      

  8.   

    http://hi.baidu.com/落水神/blog/item/7d15f5df06f44dd78c102960.html
      

  9.   

    新发现:CopyFromScreen抓不到的悬浮窗等LayeredWindow窗口。
    用Api的也不一定能抓下来,解决方法