C#画图工具

因为需要用个画图软件所以用C#写了一个,但是发现不和易语言一样,没有画布这个控件,只能调用GDI类来实现绘图,本以为很困难,实际上很简单。具体效果如下:

画画需要一块布和一支笔,因此,创造一块布:

private void Form1_Paint(object sender, PaintEventArgs e)
  {
         Graphics g = e.Graphics;
  }

创造一支笔:

private Pen p;

画画的操作就是鼠标按下,让笔去画画:

        /// 
        /// 铅笔方法
        /// 
        /// 鼠标参数
        public void DrawDot(MouseEventArgs e)
        {
            if (startDraw)
            {
                newgraphics = Graphics.FromImage(finishingImg);
                PointF currentPointF = new PointF(e.X, e.Y);
                newgraphics.DrawLine(p, startPointF, currentPointF);
                startPointF = currentPointF;
                newgraphics.Dispose();
                DrawTools_Graphics.DrawImage(finishingImg, 0, 0);
            }
        }

橡皮操作类似:

        /// 
        /// 橡皮方法
        /// 
        /// 鼠标参数
        public void Eraser(MouseEventArgs e)
        {
            if (startDraw)
            {
                newgraphics = Graphics.FromImage(finishingImg);
                newgraphics.FillRectangle(new SolidBrush(Color.White), e.X, e.Y, 20, 20);
                newgraphics.Dispose();
                DrawTools_Graphics.DrawImage(finishingImg, 0, 0);
            }
        }
文件存储:

        private void button15_Click(object sender, EventArgs e)
        {
            if (sFileName != null)
            {

                if (MessageBox.Show("是否要保存文件?", "系统提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    dt.OrginalImg.Save(sFileName);
                }
            }
            else
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "JPG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    dt.OrginalImg.Save(sfd.FileName);
                    sFileName = sfd.FileName;
                }
            }
        }

项目源码地址:http://download.csdn.net/download/qq_35957011/10014565
颜色,粗细等只要改变笔的属性就好了。具体效果如下:

C#画图工具_第1张图片




你可能感兴趣的:(C#)