By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Saving image in picbox

P: n/a
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...
Mar 25 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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);
}

--
Happy Coding!
Morten Wennevik [C# MVP]
"vi*********@gmail.com" wrote:
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...
Mar 25 '08 #2

P: n/a
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");
}
Mar 27 '08 #3

P: n/a
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");
}

--
Happy Coding!
Morten Wennevik [C# MVP]
"vi*********@gmail.com" wrote:
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");
}
Mar 27 '08 #4

P: n/a
Hey it worked man :) :) :) Thanks a lot :) Do you know how to print
that picture? I'm using inkjet printer?????
Mar 31 '08 #5

P: n/a
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 :(
Apr 1 '08 #6

P: n/a
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 :( :(

Apr 1 '08 #7

P: n/a
The printing worked :) thanks ya...
Apr 1 '08 #8

P: n/a
Wennevik i did as you told. But the resulting bitmap had only a black
background. Tell me how to overcome it ya
Apr 7 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.