用C#做一个截图工具

             因项目需要,要实现一个类似于QQ上传图片的那个截图功能。所以研究了一番。

             创建一个位图,得到选择位图的图片。 

              

 public Bitmap GetSelectionImage()
{
Bitmap bitmap = new Bitmap(554, 322);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
Brush brush = null;
brush = new SolidBrush(Color.Black);
int w = mBackgroundImage.Width;
int h = mBackgroundImage.Height;
int x = (Width - w) / 2;
int y = (Height - h) / 2;
mDragRect.Offset(-x, -y);
g.DrawImage(mBackgroundImage, new Rectangle(0, 0, 554, 322), mDragRect, GraphicsUnit.Pixel);
g.Dispose();
}
}
       然后要能随着鼠标的移动,框也要跟着动,模拟鼠标移动事件。 

                    

        private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mPrevX = e.Location.X; 
                mPrevY = e.Location.Y;
                int x = e.Location.X;
                int y = e.Location.Y;
                if (mDragRect.Contains(x, y))
                {
                    mDrag = true;
                }
            }
        }

                
        private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (mDrag)
            {
                mOffsetX = e.Location.X - mPrevX;
                mOffsetY = e.Location.Y - mPrevY;
                mPrevX = e.Location.X;
                mPrevY = e.Location.Y;

                mDragRect.Offset(mOffsetX, mOffsetY);

                if (mDragRect.Left < 0)
                {
                    mDragRect.X = 0;
                }

                if (mDragRect.Top < 0)
                {
                    mDragRect.Y = 0;
                }

                if (mDragRect.Left >= Width - 554)
                {
                    mDragRect.X = Width - 555;
                }

                if (mDragRect.Top >= Height - 322)
                {
                    mDragRect.Y = Height - 333;
                }
              
                this.Invalidate();
            }
        }

                   
        private void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            mDrag = false;
        }

                   把图画到背景上 

                    

        public void SetBackgroundImageFile(string image)
        {
            mBackgroundImage = Image.FromFile(image);
            mFileName = System.IO.Path.GetFileName(image);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            BufferedGraphics bg = mContext.Allocate(this.CreateGraphics(), this.ClientRectangle);

            DrawBackground(bg);

            DrawDragSelection(bg);

            bg.Render(e.Graphics);
            bg.Dispose();
        }

        private void DrawBackground(BufferedGraphics bg)
        {
            Graphics g = bg.Graphics;
            g.Clear(mBackgroundClr);
            //g.Clear(Color.FromArgb(255,255,255));
            g.Clear(mForegroundColor);

            if (mBackgroundImage != null)
            {
                int w = mBackgroundImage.Width;
                int h = mBackgroundImage.Height;
                int x = (Width - w) / 2;
                int y = (Height - h) / 2;
                g.DrawImage(mBackgroundImage, x, y);
            }
        }

             效果图如下  :

                             

                       

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值