Click here to Skip to main content
14,929,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on some project and part of it is to draw on image in picturebox, I have made the proper code and its working, But when save it gives me blank image.

What I have tried:

Graphics g;
Graphics graph;
Bitmap surface;

private void Button1_Click(object sender, EventArgs e)
{
g = pictureBox1.CreateGraphics();
surface = new Bitmap(pictureBox1.Width, pictureBox1.Height);
graph = Graphics.FromImage(surface);
g.DrawImage(new Bitmap(@"D:\temp\test.png"), new Point(1, 1));
g.DrawEllipse(new Pen(Color.Red), 100, 100, 50, 50);
}

// Free hand drawing by Mouse
public Point current = new Point();
public Point old = new Point();
public int width;
public Pen p = new Pen(Color.Blue, 5);

private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
current = e.Location;
g.DrawLine(p, old, current);
old = current;
}
}

private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
old = e.Location;
width = 1;
p.Width = width;
}


// Save button
String s = "Picture" + 1;
int i = 1;
private void Button3_Click(object sender, EventArgs e)
{
//pictureBox1.Dispose();
surface.Save(s, ImageFormat.Png);
s += i;
i++;
}
Posted
Updated 6-May-21 3:13am

1 solution

You aren't "draw on image in picturebox" - your code draws on the picture box itself:
C#
g = pictureBox1.CreateGraphics();

If you want to draw on an Image that you can save, set the Bitmap you created as the PictureBox.Image property, and when you want to draw on it, do this:
C#
using (Graphics g = Graphics.FromImage(myPictureBox.Image))
   {
   g.DrawLine(p, old, current);
   }
myPictureBox.Invalidate();

You can then save the Image when you need to:
C#
myPictureBox.Image.Save(path, ImageFormat.Png);


You should not create any Graphics context without Disposing of it immediately you are done with it: they are a scarce commodity in Windows and hanging on to them can cause problems!
   
Comments
hanymet 6-May-21 10:02am
   
I have change my code:

private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
current = e.Location;
g.DrawLine(p, old, current);
old = current;
}
}

with your answer:

private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawLine(p, old, current);
}
pictureBox1.Invalidate();
}
}

and when draw by mouse, it gives me this error:

System.ArgumentNullException
HResult=0x80004003
Message=Value cannot be null.
Parameter name: image
Source=System.Drawing
StackTrace:
at System.Drawing.Graphics.FromImage(Image image)
at WindowsFormsApp1_Paint_Temp.Form3.PictureBox1_MouseMove(Object sender, MouseEventArgs e) in C:\Users\HRS7_PC\source\repos\WindowsFormsApp1_Paint_Temp\WindowsFormsApp1_Paint_Temp\Form3.cs:line 56
at System.Windows.Forms.Control.OnMouseMove(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseMove(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApp1_Paint_Temp.Program.Main() in C:\Users\HRS7_PC\source\repos\WindowsFormsApp1_Paint_Temp\WindowsFormsApp1_Paint_Temp\Program.cs:line 19
OriginalGriff 6-May-21 10:26am
   
So ... you didn't bother with the "thinking" bit of it then ...
Why do you think you have a null reference now?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900