Saving image in picbox

V

vicky87.eie

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...
 
M

Morten Wennevik [C# MVP]

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);
}
 
V

vicky87.eie

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??
 
M

Morten Wennevik [C# MVP]

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.
 
V

vicky87.eie

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");
}
 
M

Morten Wennevik [C# MVP]

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");
}
 
V

vicky87.eie

Hey it worked man :) :) :) Thanks a lot :) Do you know how to print
that picture? I'm using inkjet printer?????
 
V

vicky87.eie

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 :(
 
M

Morten Wennevik [C# MVP]

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.
 
V

vicky87.eie

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 :( :(
 
V

Vicky

Wennevik i did as you told. But the resulting bitmap had only a black
background. Tell me how to overcome it ya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top