123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Drawing.Printing;
- using System.Diagnostics;
- using GraEditor.DrawTools;
- using GraEditor.Elements;
- namespace GraEditor
- {
- public class Canvas
- {
- //Construct graList to save the drawing actions
- protected Canvas()
- {
- this.graList = new ArrayList(); // define an arraylist with name gralist for saving all the drawing actions
- }
- //Initialize the canvas. Set name,size and background color of canvas
- public void Init(string name, Size size, Color backColor)
- {
- this.name = name;//Set name of canvas
- this.Size = size;//Set size of canvas
- this.backColor = backColor;//Set background color of canvas
- this.graList.Clear();//Clear all the elements in graList
- //this.mainView.AutoScrollMinSize = this.Size;//this.Size;//Set the size of mainView as canvas size
- this.isSavedOnce = false;
- this.IsModified = false;
- }
- private static Canvas _single;
- public static Canvas Instance
- {
- //Get canvas instance
- get
- {
- if (_single == null)
- _single = new Canvas();
- return _single;
- }
- }
- public event EventHandler Modified;
- protected void OnModified()
- {
- if (Modified != null)
- Modified(this, EventArgs.Empty);
- }
- //Dirty flag
- private bool isModified;
- public bool IsModified
- {
- get { return this.isModified; }
- set
- {
- this.isModified = value;
- OnModified();
- }
- }
- private bool isSavedOnce;
- public bool IsSavedOnce
- {
- get { return this.isSavedOnce; }
- set { this.isSavedOnce = value; }
- }
- private MainView mainView;//private Mainview variable of canvas
- public MainView MainView
- {
- get { return mainView; }
- set { mainView = value; }
- }
- public void Update()//Refresh canvas
- {
- this.mainView.UpdateView();
- }
- private string name;//canvas name
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- private Size size;//canvas size
- public Size Size
- {
- get { return size; }
- set
- {
- size = value;
- this.mainView.AutoScrollMinSize = this.size;//this.Size;//Set the size of mainView as canvas size
- }
- }
- private Color backColor;//canvas color i.e. background color of the image
- public Color BackColor
- {
- get { return backColor; }
- set
- {
- backColor = value;
- this.Update();
- }
- }
- private ArrayList graList;//Save drawing action on canvas
- public ArrayList GraList
- {
- get { return graList; }
- }
- public void AddGra(GraElement gra)
- {
- this.graList.Add(gra);
- this.IsModified = true;
- this.Update();
- }
- public void DelGra(GraElement gra)
- {
- this.graList.Remove(gra);
- this.IsModified = true;
- this.Update();
- }
- //Draw function of canvas
- public void Draw(Graphics g)
- {
- foreach (GraElement gra in graList)//Draw all the element in the graList
- gra.Draw(g);
- }
- public bool Open()
- {
- OpenFileDialog dlg = new OpenFileDialog(); // new open dialog box is instantiated
- dlg.Filter = "位图文件(*.bmp;*.jpg;*.gif;*.png)|*.bmp;*.jpg;*.gif;*.png";
- dlg.FilterIndex = 1;
- dlg.RestoreDirectory = true;
- bool bRtn = false;
- if (dlg.ShowDialog() == DialogResult.OK)
- {
- try
- {
- bRtn = this.OpenFile(dlg.FileName);
- }
- catch (Exception ex)
- {
- MessageBox.Show("文件打开错误 - " + ex.Message);
- }
- }
- return bRtn;
- }
- private FileStream canvasStream;
- public bool OpenFile(string fileName)
- {
- FileInfo fi = new FileInfo(fileName);
- if (!fi.Exists)
- {
- MessageBox.Show("选择的文不存在!", "Open File");
- return false;
- }
-
- try
- {
- canvasStream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
- }
- catch (Exception ex)
- {
- MessageBox.Show("文件打开错误 - " + ex.Message);
- }
-
- Bitmap bmp = new Bitmap(canvasStream);
- //fs.Close();
- if (bmp != null)
- {
- Canvas.Instance.Init(fileName, bmp.Size, Color.White);
- ImageGra imageGra = new ImageGra(bmp, new Rectangle(new Point(0, 0), bmp.Size));
- Canvas.Instance.GraList.Clear();
- Canvas.Instance.GraList.Add(imageGra);//Add to GraList
- Canvas.Instance.Update();//Update canvas
- CurrentBgImage = new Bitmap(bmp);
- }
- return true;
- }
- Bitmap currentBgImage = null;
- public Bitmap CurrentBgImage
- {
- get { return currentBgImage; }
- set {
- if (currentBgImage != null) {
- currentBgImage.Dispose();
- }
- currentBgImage = value; }
- }
- /// <summary>
- /// 重置 恢复打开时的原始状态
- /// </summary>
- public void ReloadCanvas()
- {
- if (currentBgImage != null)
- {
- Bitmap bmp = new Bitmap(currentBgImage);
- ImageGra imageGra = new ImageGra(bmp, new Rectangle(new Point(0, 0), bmp.Size));
- Canvas.Instance.GraList.Clear();
- Canvas.Instance.GraList.Add(imageGra);//Add to GraList
- Canvas.Instance.Update();//Update canvas
- }
- else {
- Canvas.Instance.GraList.Clear();
- Canvas.Instance.Update();//Update canvas
- }
- }
- private ImageFormat GetImageFormatByFileName(string fileName)
- {
- string ext = Path.GetExtension(fileName);
- ImageFormat format = ImageFormat.Bmp;
- switch (ext)
- {
- case ".jpg":
- format = ImageFormat.Jpeg;
- break;
- case ".gif":
- format = ImageFormat.Gif;
- break;
- default:
- format = ImageFormat.Bmp;
- break;
- }
- return format;
- }
- /// <summary>
- /// 获取新绘制的图片
- /// </summary>
- /// <returns></returns>
- public Bitmap GetNewBitmap()
- {
- Size s = Canvas.Instance.Size;
- Bitmap bmp = new Bitmap(s.Width, s.Height);
- Graphics gBmp = Graphics.FromImage(bmp);
- gBmp.Clear(Canvas.Instance.BackColor);
- this.Draw(gBmp);
- return bmp;
- }
- public void Save()
- {
- //if(this.canvasStream == null)
- // return;
- if (this.name == null ||
- this.name.Length == 0)
- {
- MessageBox.Show("文件名无效!", "警告");
- return;
- }
- Bitmap bmp = GetNewBitmap();
- try
- {
- if (this.canvasStream != null)
- this.canvasStream.Close();
- ImageFormat format = GetImageFormatByFileName(Canvas.Instance.Name);
- bmp.Save(Canvas.Instance.Name, format);
- bmp.Dispose();
- this.IsModified = false;
- this.IsSavedOnce = true;
- // this.canvasStream = File.Open(this.name, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
- }
- catch (Exception ex)
- {
- MessageBox.Show("文件保存错误 - " + ex.Message);
- }
- }
- public bool Close()
- {
- if (canvasStream != null)
- {
- canvasStream.Close();
- canvasStream = null;
- }
- return true;
- }
- }
- //Redo & Undo operation
- public class GraEditStackMgr
- {
- private GraEditStackMgr()//construct a redo list
- {
- this.graRedoList = new ArrayList();
- }
- private static GraEditStackMgr instance;
- public static GraEditStackMgr Instance
- {
- get
- {
- if (instance == null)
- instance = new GraEditStackMgr();
- return instance;
- }
- }
- private ArrayList graRedoList;//Redo list
- public ArrayList GraRedoList
- {
- get { return graRedoList; }
- }
- public void Clear()
- {
- this.graRedoList.Clear();
- }
- //Redo operation
- public void Redo()
- {
- if (this.graRedoList.Count == 0)//no undo happen
- return;
- int lastIndex = this.graRedoList.Count - 1;//Get the last undo action
- //Save last undo action to graLast
- GraElement graLast = this.graRedoList[lastIndex] as GraElement;
- this.graRedoList.RemoveAt(lastIndex);//Remove the last undo action from redo list
- //Canvas.Instance.GraList.Add(graLast);//Save the last undo action to GraList
- //Canvas.Instance.Update();//Update canvas
- Canvas.Instance.AddGra(graLast);
- }
- //Undo operation
- public void Undo()
- {
- ArrayList graList = Canvas.Instance.GraList;//Get the GraList of Canvas
- if (graList.Count == 0)//No drawing action happen
- return;
- int lastIndex = graList.Count - 1;//Last drawing action position in graList
- //Save the last drawing action to graLast
- GraElement graLast = graList[lastIndex] as GraElement;
- graList.RemoveAt(lastIndex);//Remove the last drawing action
- this.graRedoList.Add(graLast);//Add the last drawing action to redo list
- Canvas.Instance.Update();//Update canvas
- Canvas.Instance.IsModified = true;
- }
- }
- }
|