1. 程式人生 > >VS2017中GDI+繪圖輕鬆入門(4)

VS2017中GDI+繪圖輕鬆入門(4)

前面我們實現了簡單的圖形繪製,以及粗製的爆粗,可能細心的同學在儲存一個已有的圖片(即覆蓋儲存)時會出現報錯,這個錯誤就是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函式中增加適當的功能即可。喜歡研究的同學,可以到這裡下載原始碼:
原始碼下載