472,099 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,099 software developers and data experts.

Saving image in picbox

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
8 2168
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
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
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
Hey it worked man :) :) :) Thanks a lot :) Do you know how to print
that picture? I'm using inkjet printer?????
Mar 31 '08 #5
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
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
The printing worked :) thanks ya...
Apr 1 '08 #8
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.

Similar topics

4 posts views Thread by Carl | last post: by
1 post views Thread by halise irak via .NET 247 | last post: by
3 posts views Thread by Shailaja Kulkarni | last post: by
7 posts views Thread by Adam Maltby | last post: by
3 posts views Thread by PaulJS | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.