VS2017中GDI+绘图轻松入门(4)

版权声明:本博客所有原创文章未经准许不得转载或保存转发,本人保留版权法律追诉权。 https://blog.csdn.net/haigear/article/details/85225694

前面我们实现了简单的图形绘制,以及粗制的爆粗,可能细心的同学在保存一个已有的图片(即覆盖保存)时会出现报错,这个错误就是GDI+一般性错误。
这里,我们将这个错误解释一下,由于微软设定了一种机制,那就是不准许其他对象对图形原始构建对象进行读写操作。所以,这里我们采用了另一个Bitmap对象来进行保存操作,则可以有效避免这个错误。代码中有详细注释:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace drawForm
{
    public partial class Form1 : Form
    {
        Graphics gp;
        Bitmap saveBitmap, readBitmap,markBitmap;
        string filePath;
        public Form1()
        {
            InitializeComponent();
            markBitmap = new Bitmap("mark.png");
        }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
       
    }

    void genPic()
    {
            if (File.Exists(filePath))
            readBitmap = new Bitmap(filePath);
        else
            readBitmap = new Bitmap(pictureBox1.Width,pictureBox1.Height);
        //用saveBitmap接过readBitmap的内容
        saveBitmap = new Bitmap(readBitmap);
        //释放readBitmap
        readBitmap.Dispose();
        gp = Graphics.FromImage(saveBitmap);
        drawGraphics(gp);
        pictureBox1.Image = saveBitmap;
    }

    private void genPicBtn_Click(object sender, EventArgs e)
    {
        showPic();
    }

    void  showPic()
    {
        OpenFileDialog dlg = new OpenFileDialog();
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            filePath = dlg.FileName;
            readBitmap = new Bitmap(filePath);
            saveBitmap = new Bitmap(readBitmap);
            pictureBox1.Image = saveBitmap;
            readBitmap.Dispose();
        }
    }

    void drawGraphics(Graphics grfc)
    {
       //定义了一个蓝色,宽度为的画笔
        Pen pen;
        pen = new Pen(Color.Green, 2);
        //在画板上画直线,起始坐标为(pictureBox1.Width / 4, pictureBox1.Height/4),终点坐标为(pictureBox1.Width*3 / 4, pictureBox1.Height*3 / 4)
        grfc.DrawLine(pen, pictureBox1.Width / 4, pictureBox1.Height / 4, pictureBox1.Width * 3 / 4, pictureBox1.Height * 3 / 4);
        //在画板上画矩形,起始坐标为(pictureBox1.Width / 4, pictureBox1.Height/4),宽为,高为
        grfc.DrawRectangle(pen, pictureBox1.Width / 4, pictureBox1.Height / 4, pictureBox1.Width / 2, pictureBox1.Height / 2);
        pen = new Pen(Color.Blue, 1);
        //在画板上画椭圆,起始坐标为(pictureBox1.Width / 4, pictureBox1.Height / 4),外接矩形的宽为160,高为160
        grfc.DrawEllipse(pen, pictureBox1.Width / 4, pictureBox1.Height / 4, 160, 160);
        //绘制文字,起点为160,160;
        grfc.DrawString("Drawing is here!", new Font("宋体", 14), new SolidBrush(Color.Red), new PointF(160, 160));
        //绘制水印
        grfc.DrawImage(markBitmap, new PointF(saveBitmap.Width * 3 / 4, saveBitmap.Height * 3 / 4));

    }
    
    private void saveBtn_Click(object sender, EventArgs e)
    {
        if(!File.Exists(filePath))
        {
            SaveFileDialog sdlg = new SaveFileDialog();
            sdlg.Filter= "JPG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp";
            if (sdlg.ShowDialog()== DialogResult.OK)
                filePath = sdlg.FileName ;
        }
         if(filePath.Length>0)
        saveBitmap.Save(filePath);
    }

    private void drawBtn_Click(object sender, EventArgs e)
    {
        genPic();           
    }
}
}

这段程序,我们使用到了,三个按钮,分别完成已有图片的加载,新建图片,保存绘画图片的功能。对于需要继续增加功能的童鞋,只需要在函数 drawGraphics函数中增加适当的功能即可。喜欢研究的同学,可以到这里下载源代码:
源代码下载

猜你喜欢

转载自blog.csdn.net/haigear/article/details/85225694