Hi all,
I am just wondering if someone can help me.
Basically, I have the following code that I am using to allow me to draw on a picturebox.
There are a few things that I need to be able to do and I can't work out exactly how to get them done.
1: I need the drawing to stay in the picturebox once the form is minimized and then restored.
2: I need to be able to save the drawing to an image file.
The DrawToBitmap parts of the code are just left in from when I tried to load the bitmap again with the Pain event for the picturebox.
Any help with this would be appreciated.
Thanks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
namespace Painter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool Drag = false;
int NewX;
int NewY;
int oldX;
int oldY;
Bitmap bmp;
private void Form1_Load(object sender, EventArgs e)
{
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width,pictureBox1.Height));
bmp.Save("c:\\out.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Drag = true;
NewX = e.X;
NewY = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
Pen myPen = new Pen(Color.Black, 2);
if (Drag == true)
{
g.DrawLine(myPen, oldX, oldY, e.X, e.Y);
oldX = e.X;
oldY = e.Y;
}
oldX = e.X;
oldY = e.Y;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Drag = false;
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
}
}
}