Simple paint app - problem saving
-
I have a picture box, on which I detect mousemove events, and have the user draw on the picture box. Currently, I can save the image, but it's just plain white, without the actual drawing, any ideas where Im going wrong? [CODE:]
private Graphics m_objGraphics;
private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
{
Rectangle rectEllipse = new Rectangle();if (e.Button != MouseButtons.Left) return; rectEllipse.X = e.X - 1; rectEllipse.Y = e.Y - 1; rectEllipse.Width = 3; rectEllipse.Height = 3; //pb\_white\_board.Update(); m\_objGraphics.DrawEllipse(System.Drawing.Pens.Black, rectEllipse); Brush blackBrush = Brushes.Black; m\_objGraphics.FillEllipse(blackBrush, rectEllipse); }
private void btn_save_whiteboard_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap(pb_white_board.Width, pb_white_board.Height);
pb_white_board.DrawToBitmap(bitmap, new Rectangle(0, 0, pb_white_board.Width, pb_white_board.Height));
m_objGraphics.Save("image1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}Thanks, Ben.
-
I have a picture box, on which I detect mousemove events, and have the user draw on the picture box. Currently, I can save the image, but it's just plain white, without the actual drawing, any ideas where Im going wrong? [CODE:]
private Graphics m_objGraphics;
private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
{
Rectangle rectEllipse = new Rectangle();if (e.Button != MouseButtons.Left) return; rectEllipse.X = e.X - 1; rectEllipse.Y = e.Y - 1; rectEllipse.Width = 3; rectEllipse.Height = 3; //pb\_white\_board.Update(); m\_objGraphics.DrawEllipse(System.Drawing.Pens.Black, rectEllipse); Brush blackBrush = Brushes.Black; m\_objGraphics.FillEllipse(blackBrush, rectEllipse); }
private void btn_save_whiteboard_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap(pb_white_board.Width, pb_white_board.Height);
pb_white_board.DrawToBitmap(bitmap, new Rectangle(0, 0, pb_white_board.Width, pb_white_board.Height));
m_objGraphics.Save("image1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}Thanks, Ben.
Two things... 1 - You are not dwawing on a PictureBox and 2 - You are trying to fetch drawn image from the PictureBox where painting has not taken place... Cheers ;)
M Aamir Maniar aamirOnline.com
-
I have a picture box, on which I detect mousemove events, and have the user draw on the picture box. Currently, I can save the image, but it's just plain white, without the actual drawing, any ideas where Im going wrong? [CODE:]
private Graphics m_objGraphics;
private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
{
Rectangle rectEllipse = new Rectangle();if (e.Button != MouseButtons.Left) return; rectEllipse.X = e.X - 1; rectEllipse.Y = e.Y - 1; rectEllipse.Width = 3; rectEllipse.Height = 3; //pb\_white\_board.Update(); m\_objGraphics.DrawEllipse(System.Drawing.Pens.Black, rectEllipse); Brush blackBrush = Brushes.Black; m\_objGraphics.FillEllipse(blackBrush, rectEllipse); }
private void btn_save_whiteboard_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap(pb_white_board.Width, pb_white_board.Height);
pb_white_board.DrawToBitmap(bitmap, new Rectangle(0, 0, pb_white_board.Width, pb_white_board.Height));
m_objGraphics.Save("image1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}Thanks, Ben.
Where is
m_objGraphics
created? I guess that you're creating the Graphics object somewhere during initialization of your code and then use it later, which is a big no-no! To do it correctly you have to call methods on a Graphics object you get from the Image displayed in the PictureBox:private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;Rectangle rectEllipse = new Rectangle(e.X - 1, e.Y - 1, 3, 3);
using (Graphics g = Graphics.FromImage(pb_white_board.Image))
{
g.DrawEllipse(System.Drawing.Pens.Black, rectEllipse);
g.FillEllipse(System.Drawing.Brushes.Black, rectEllipse);
}
// Invalidate the picturebox so that you see the modified image
pb_white_board.Invalidate();
}Regards, mav -- Black holes are the places where God divided by 0...
-
Where is
m_objGraphics
created? I guess that you're creating the Graphics object somewhere during initialization of your code and then use it later, which is a big no-no! To do it correctly you have to call methods on a Graphics object you get from the Image displayed in the PictureBox:private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;Rectangle rectEllipse = new Rectangle(e.X - 1, e.Y - 1, 3, 3);
using (Graphics g = Graphics.FromImage(pb_white_board.Image))
{
g.DrawEllipse(System.Drawing.Pens.Black, rectEllipse);
g.FillEllipse(System.Drawing.Brushes.Black, rectEllipse);
}
// Invalidate the picturebox so that you see the modified image
pb_white_board.Invalidate();
}Regards, mav -- Black holes are the places where God divided by 0...
Thanks, works a treat now :D