Hi Vicky,
It is a common mistake that the PictureBox is meant for drawing, which it
isn't. It is meant to display the image inserted into the Image property, so
Image.Save would only save an existing image.
Anyhow, to save what you draw in the Paint event, extract the drawing code
to a separate method taking the Graphics object from PaintEventArgs as a
parameter.
When you want to save the drawing, simply create a Graphics object from a
blank Bitmap object and call the drawing method with this Graphics object as
a parameter.
Something like:
OnPaint(PaintEventArgs e)
{
Draw(e.Graphics);
}
SaveImage()
{
Bitmap bmp = new Bitmap(100, 100);
using(Graphics g = Graphics.FromImage(bmp))
{
Draw(g);
}
bmp.Save(filename);
}

Signature
Happy Coding!
Morten Wennevik [C# MVP]
> I used a picture box to draw lines and rectangle using its graphics
> object in paint event. Now i need to save those lines i have drawn and
> print them. I need to know how to save them. I tried image.save. But i
> didn't work...
vicky87.eie@gmail.com - 27 Mar 2008 07:08 GMT
Thanks a lot. You mentioned that picbox is actually meant to display
images. Now i'm trying to design a software a bit similar to MSPAINT.
What would you suggest for the drawing area??
Peter Duniho - 27 Mar 2008 08:19 GMT
> Thanks a lot. You mentioned that picbox is actually meant to display
> images. Now i'm trying to design a software a bit similar to MSPAINT.
> What would you suggest for the drawing area??
As with any OOP situation, the question is "what class contains
functionality that is useful to you?"
If you are intending to handle all graphical display with custom code,
it's likely that the answer to that question is the base Control class,
from which all of the .NET Forms controls are derived (either directly or
indirectly).
If you want your own control to do something that's already implemented in
some other control, then perhaps that other control is more appropriate.
But nothing you've posted so far suggests that's the case.
Pete
Morten Wennevik [C# MVP] - 27 Mar 2008 09:09 GMT
Hi Vicky,
As Peter said, inheriting from Control would probably be sufficient for your
needs. Or if you rather want to use an existing control, I sometimes use the
Panel control, probably due to its name rather than its functionality
(borderstyle, scrollable, container). Usually I create my own control,
inheriting from UserControl.
You would do the same kind of drawing on any other control as you already
are doing on the PictureBox, so you don't need to change this if you already
started using the PictureBox, but for future reference this isn't what the
PictureBox is meant for. That said, there aren't any other controls that are
better suited either, just more lightweight.

Signature
Happy Coding!
Morten Wennevik [C# MVP]
> Thanks a lot. You mentioned that picbox is actually meant to display
> images. Now i'm trying to design a software a bit similar to MSPAINT.
> What would you suggest for the drawing area??
vicky87.eie@gmail.com - 27 Mar 2008 10:10 GMT
I tried as you said. It created a bmp file. But when i open it there
was no image. It was just an empty bmp file :( When i tried to open it
with paint it said format not supported :(
this is my code
public void drawwire(Graphics gr)
{
Pen p1 = new Pen(System.Drawing.Color.Black, 6);
Color customColor = Color.Black;
SolidBrush b1 = new SolidBrush(customColor);
Point begin = new Point(), end = new Point();
for (int count1 = 1; count1 <= wire_cont; count1++)
{
if (modify.item == 1)
{
p1.Color = Color.Black;
b1.Color = Color.Black;
if (modify.no == count1)
{
p1.Color = Color.Red;
b1.Color = Color.Red;
}
}
if (wire[count1] != null)
{
begin = wire[count1].getBeginning();
end = wire[count1].getEnding();
gr.FillRectangle(b1, begin.X - 4, begin.Y - 4, 8,
8);
gr.FillRectangle(b1, end.X - 4, end.Y - 4, 8, 8);
gr.DrawLine(p1, begin, end);
}
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
Bitmap print = new Bitmap(100, 100);
using (Graphics pri = Graphics.FromImage(print))
{
drawwire(pri);
}
print.Save("E:\\print.bmp");
}
Morten Wennevik [C# MVP] - 27 Mar 2008 12:34 GMT
Hi Vicky,
Your code is correct as far as I can see, although I cannot reproduce your
code as the modify and wire data is unknown. The error would indicate you
are not producing a regular bitmap so if you are doing additional stuff to
the bitmap I would check that code.
Below is some reference code that should produce a 100x100 bitmap with a red
80x80 rectangle in the middle. This rectangle is also painted in a Panel
control using the exact same drawing code.
public Form1()
{
InitializeComponent();
panel1.Paint += new PaintEventHandler(panel1_Paint);
}
void panel1_Paint(object sender, PaintEventArgs e)
{
drawwire(e.Graphics);
}
public void drawwire(Graphics gr)
{
gr.FillRectangle(Brushes.Red, 10, 10, 80, 80);
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap print = new Bitmap(100, 100);
using (Graphics pri = Graphics.FromImage(print))
{
drawwire(pri);
}
print.Save("C:\\print.bmp");
}

Signature
Happy Coding!
Morten Wennevik [C# MVP]
> I tried as you said. It created a bmp file. But when i open it there
> was no image. It was just an empty bmp file :( When i tried to open it
[quoted text clipped - 42 lines]
> print.Save("E:\\print.bmp");
> }
vicky87.eie@gmail.com - 31 Mar 2008 16:24 GMT
Hey it worked man :) :) :) Thanks a lot :) Do you know how to print
that picture? I'm using inkjet printer?????
vicky87.eie@gmail.com - 01 Apr 2008 05:52 GMT
I tried to open the BMP file using MSPAINT. But it's giving error that
"Paint cannot read this file. This is not a valid BMP file or this
format is not currently supported". What to do :(
Morten Wennevik [C# MVP] - 01 Apr 2008 06:55 GMT
Hi Vicky,
The default printing format may not be BMP despite the filename being .bmp,
although in my case Paint accepted the image. You can try specifying the
imageformat when saving
print.Save("C:\\print.bmp", ImageFormat.Bmp);
As for printing, you can use the same code for printing as you are already
using for creating the bitmap as you draw to a PrintPage Graphics object
instead of panel.
private void button2_Click(object sender, EventArgs e)
{
PrintDocument doc = new PrintDocument();
PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
doc.PrinterSettings = dialog.PrinterSettings;
doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
doc.Print();
}
}
void doc_PrintPage(object sender, PrintPageEventArgs e)
{
drawwire(e.Graphics);
}
You can drop the PrintDialog part if you just want to draw to the default
printer.

Signature
Happy Coding!
Morten Wennevik [C# MVP]
> I tried to open the BMP file using MSPAINT. But it's giving error that
> "Paint cannot read this file. This is not a valid BMP file or this
> format is not currently supported". What to do :(
vicky87.eie@gmail.com - 01 Apr 2008 09:15 GMT
You told me that this one will create a bmp file.
print.Save("C:\\print.bmp",
ImageFormat.Bmp);
Ya it created one. But the content in pic box was nt
found. It just had a black background any nothig else :( :(
vicky87.eie@gmail.com - 01 Apr 2008 11:57 GMT
The printing worked :) thanks ya...
Vicky - 07 Apr 2008 07:40 GMT
Wennevik i did as you told. But the resulting bitmap had only a black
background. Tell me how to overcome it ya