/* ------------------------------------------------------------------------------- * Constructor: Form1 * * Use: Constructs a new Dirived Form object. * * Parameters: none * -------------------------------------------------------------------------------*/ public Form1() { InitializeComponent(); //Generate a new graphics object for displaying images g = DrawPanel.CreateGraphics(); //Creates a new bitmap that is the same size as the drawing field, and sets it to all white Canvas = new Bitmap(DrawPanel.Size.Width, DrawPanel.Size.Height); DrawPanel.DrawToBitmap(Canvas, new Rectangle(0, 0, DrawPanel.Size.Width, DrawPanel.Size.Height)); //Generate a new graphics object for saving images bitmapg = Graphics.FromImage(Canvas); //Initialize Variables. DrawCurrentColor = Color.Black; DrawBackgroundColor = Color.White; BackgroundButton.BackColor = Color.White; RecentFiles.Add(SaveHold1); RecentFiles.Add(SaveHold2); RecentFiles.Add(SaveHold3); RecentFiles.Add(SaveHold4); RecentFiles.Add(SaveHold5); }
/* ------------------------------------------------------------------------------- * Function: newToolStripMenuItem_Click * * Use: Clears the currently Drawn Image * * Parameters: Sender: Refrence to the object that called this function * EventArgs: The auguments passed from the calling object * * Returns: nothing * -------------------------------------------------------------------------------*/ private void newToolStripMenuItem_Click(object sender, EventArgs e) { //If anything is drawn, or if the user hasn't saved, ask if they would like to if (AnythingDrawn) { if (MessageBox.Show("Would you like to save your progress?", "Save?", MessageBoxButtons.YesNo) == DialogResult.Yes) { saveToolStripMenuItem_Click(sender, e); } } //save Current filename for most recent files if (CurrentSaveName != null) { if (PositionInRecentFileArray == 5) { PositionInRecentFileArray = 0; } RecentFiles[PositionInRecentFileArray].Text = CurrentSaveName; RecentFiles[PositionInRecentFileArray].Enabled = true; PositionInRecentFileArray++; CurrentSaveName = null; } //Reset anythingdrawn flag & defaults AnythingDrawn = false; DrawBackgroundColor = Color.White; BackgroundButton.BackColor = Color.White; //reset resources Canvas.Dispose(); Canvas = new Bitmap(DrawPanel.Size.Width, DrawPanel.Size.Height); bitmapg.Dispose(); //clear stacks while (RedoStack.Any()) { RedoStack.Pop().Dispose(); } while (UndoStack.Any()) { UndoStack.Pop().Dispose(); } //reset buttons RedoButton.Enabled = false; UndoButton.Enabled = false; //this works because the "CreateGraphics" is not persistent & DrawPanel will always be 'blank' DrawPanel.DrawToBitmap(Canvas, new Rectangle(0, 0, DrawPanel.Size.Width, DrawPanel.Size.Height)); bitmapg = Graphics.FromImage(Canvas); //Redraw our canvas on drawpanel g.DrawImage(Canvas, Point.Empty); }
// save file and update recent image after saveFile dialog private void SaveAsFileOk(object sender, CancelEventArgs e) { var width = DrawPanel.Width; var height = DrawPanel.Height; Bitmap bm = new Bitmap(width, height); DrawPanel.DrawToBitmap(bm, new Rectangle(0, 0, width, height)); bm.Save(SaveFile.FileName, ImageFormat.Png); MessageBox.Show("Your file has been saved!"); fileName = SaveFile.FileName; UpdateRecent(SaveFile.FileName); }
// save the image and update Recent image tool strip menu item private void Save(object sender, EventArgs e) { if (string.IsNullOrEmpty(fileName)) { SaveAs(sender, e); //if string is empty it means it's a new project and we need to call save as } else { var width = DrawPanel.Width; var height = DrawPanel.Height; Bitmap bm = new Bitmap(width, height); DrawPanel.DrawToBitmap(bm, new Rectangle(0, 0, width, height)); if (File.Exists(fileName)) // if file name exists, delete the file { File.Delete(fileName); } bm.Save(fileName, ImageFormat.Png); MessageBox.Show("Your file has been saved!"); UpdateRecent(fileName); } }