/// <summary> /// Save the current drawing to a PNG image /// </summary> /// <param name="filename">The filepath where the PNG should be saved</param> private void saveToPNG(string filename) { saveImg = new Bitmap((int)g.VisibleClipBounds.Width, (int)g.VisibleClipBounds.Height); Graphics tempg = Graphics.FromImage(saveImg); if (settings.saveBgPNG) { tempg.Clear(this.BackColor); } tempg.SmoothingMode = (settings.antiAlias) ? SmoothingMode.AntiAlias : SmoothingMode.None; for (int i = 0; i < path.Count; i++) { pathEvent point = path[i]; if (point.eventType == 0) { pen.Color = Color.FromArgb(point.penColor); // We just needed the pen color from this first point continue; } lastPoint = path[i - 1].coor; thisPoint = point.coor; pen.Width = point.penSize; if (point.eventType == 2) { tempg.DrawLine(pen, lastPoint, thisPoint); } } saveImg.Save(filename); }
private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && doDraw == false && !isDrawing) { short waitTime = 0; if (mouseUp) { TimeSpan ts = (DateTime.Now - start); waitTime = (short)ts.TotalMilliseconds; if (waitTime > 1500) { waitTime = 1500; } } start = DateTime.Now; pathEvent point = new pathEvent() { eventType = 0, coor = e.Location, wait = waitTime, penSize = (byte)pen.Width, penColor = pen.Color.ToArgb() }; path.Add(point); listBox1.Items.Add("0: " + e.X + "x" + e.Y + ", " + waitTime + ", " + pen.Width); listBox1.SelectedIndex = listBox1.Items.Count - 1; thisPoint = e.Location; doDraw = true; } }
private void Form1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && doDraw == true) { pathEvent point = new pathEvent() { eventType = 1, coor = e.Location, wait = 0, penSize = (byte)pen.Width }; path.Add(point); listBox1.Items.Add("1: " + e.X + "x" + e.Y + ", 0, " + pen.Width); listBox1.SelectedIndex = listBox1.Items.Count - 1; doDraw = false; mouseUp = true; } }
private void Form1_MouseMove(object sender, MouseEventArgs e) { if (doDraw) { lastPoint = thisPoint; thisPoint = new Point(e.X, e.Y); g.DrawLine(pen, lastPoint, thisPoint); TimeSpan ts = (DateTime.Now - start); start = DateTime.Now; pathEvent spot = new pathEvent() { eventType = 2, coor = e.Location, wait = (short)ts.TotalMilliseconds, penSize = (byte)pen.Width }; path.Add(spot); listBox1.Items.Add("2: " + e.X + "x" + e.Y + ", " + ts.TotalMilliseconds + ", " + pen.Width); listBox1.SelectedIndex = listBox1.Items.Count - 1; if (!saveToolStripMenuItem.Enabled) { saveToolStripMenuItem.Enabled = true; } if (!saveToPNGToolStripMenuItem.Enabled) { saveToPNGToolStripMenuItem.Enabled = true; } if (!redrawToolStripMenuItem.Enabled) { redrawToolStripMenuItem.Enabled = true; } if (!clearToolStripMenuItem.Enabled) { clearToolStripMenuItem.Enabled = true; } } }
/// <summary> /// Redraws the current drawing /// </summary> /// <param name="backColor">Background color for the drawing</param> /// <param name="fastDraw">True to do an instant draw, false to wait between points</param> private void reDraw(int backColor, bool fastDraw) { stopDrawingToolStripMenuItem.Visible = true; redrawToolStripMenuItem.Visible = false; this.ContextMenuStrip = null; stop = false; g.Clear(Color.FromArgb(backColor)); for (int i = 0; i < path.Count; i++) { if (!stop) { pathEvent point = path[i]; if (point.eventType == 0) { pen.Color = Color.FromArgb(point.penColor); // We just needed the pen color from this first point if (!fastDraw) { System.Threading.Thread.Sleep(point.wait); } continue; } lastPoint = path[i - 1].coor; thisPoint = point.coor; pen.Width = point.penSize; tslblPenSize.Text = "Pen Size: " + pen.Width; listBox1.SelectedIndex = i; if (point.eventType == 2) { g.DrawLine(pen, lastPoint, thisPoint); } if (!fastDraw) { System.Threading.Thread.Sleep(point.wait); } Application.DoEvents(); } } stopDrawingToolStripMenuItem.Visible = false; redrawToolStripMenuItem.Visible = true; this.ContextMenuStrip = contextMenuStrip1; stop = true; if (!saveToolStripMenuItem.Enabled) { saveToolStripMenuItem.Enabled = true; } if (!saveToPNGToolStripMenuItem.Enabled) { saveToPNGToolStripMenuItem.Enabled = true; } if (!redrawToolStripMenuItem.Enabled) { redrawToolStripMenuItem.Enabled = true; } if (!clearToolStripMenuItem.Enabled) { clearToolStripMenuItem.Enabled = true; } }
/// <summary> /// Opens a DRAW file /// </summary> /// <param name="filename">The filepath of the DRAW file to be opened</param> private void openDrawing(string filename) { path.Clear(); listBox1.Items.Clear(); FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); // Read header if (br.ReadInt32() != 1463898692) // Confirm DRAW header { MessageBox.Show("Error: Not a valid DRAW file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } byte version = br.ReadByte(); // Read the version if (br.ReadByte() != 12) // read the entry length { MessageBox.Show("Error: Invalid entry length", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } short numEntries = br.ReadInt16(); // self explanatory int backColor = br.ReadInt32(); settings.backColor = backColor; settings.Save(); this.BackColor = Color.FromArgb(backColor); // Read entries listBox1.BeginUpdate(); for (short i = 0; i < numEntries; i++) { byte eventType = br.ReadByte(); pathEvent point = new pathEvent() { eventType = eventType, coor = new Point(br.ReadInt16(), br.ReadInt16()), wait = br.ReadInt16(), penSize = br.ReadByte(), penColor = (eventType == 0) ? br.ReadInt32() : pen.Color.ToArgb() }; if (eventType != 0) { br.BaseStream.Position += 4; } path.Add(point); listBox1.Items.Add(point.coor.X + "x" + point.coor.Y + ", " + point.wait + ", " + point.penSize); listBox1.SelectedIndex = listBox1.Items.Count - 1; } listBox1.EndUpdate(); br.Close(); fs.Close(); isDrawing = true; reDraw(backColor, settings.fastRedraw); isDrawing = false; }