您的位置:首页 > 编程语言 > C#

GDI+ C# 在图片上画矩形

2013-03-22 17:52 585 查看
//定义全局变量 

  private int pointStartX, pointStartY, pointEndX, pointEndY  

  private Bitmap bitmapSource = null;

 //初始化中          

     string strPath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\22.jpg";

            bitmapSource = new Bitmap(strPath);

 //在MouseDown事件中记下起始点          

     pointStartX = e.X;

            pointStartY = e.Y;

 //C#中利用GDI+ ,在MouseMove事件中绘制矩形   

     int iWidth = e.X - pointStartX;

            int iHeight = e.Y - pointStartY;

            if (e.Button == MouseButtons.Left)

            {

                // 每次鼠标移动都拷贝原图bitmapSource,去除之前的留下的矩形

                Bitmap bitmap = new Bitmap(bitmapSource, 500, 500);

                Pen pen = new Pen(Color.Red);

                Graphics gh = Graphics.FromImage(bitmap);

                Rectangle rectNew = new Rectangle(pointStartX, pointStartY, iWidth, iHeight);

                // 画矩形

                gh.DrawRectangle(pen, rectNew);

                // 显示在画板上

                this.CreateGraphics().DrawImage(bitmap, 0, 0, 500, 500);

            }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# GDI+
相关文章推荐