PictureBox doesn't copy drawn content to Bitmap

N

Nitin

Hi!

I've been trying to draw in a PictureBox and save the resulting image
to a Bitmap object that I save to a file on disk. Unfortunately, I
can't seem to get the elements that I'm drawing to appear in the file.
To make sure that I'm getting something that actually is from the
PictureBox, I set the background color of the PictureBox and that's
all that appears in the file (I use Windows Preview).

Below is the code extract that I used for the program:

Code:
pictureBox1.BackColor = Color.AntiqueWhite;

Graphics gfx = pictureBox1.CreateGraphics();
gfx.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Pen pn = Pens.Black;
Pen pnAlt = Pens.Blue;
gfx.DrawLine(pn, 0, 0, 80, 80);
gfx.DrawPie(pn, 80, 80, 100, 100, 0, 14);
gfx.DrawPie(pnAlt, 80, 80, 100, 100, 15, 14);
gfx.Flush();
gfx.Dispose();

Bitmap bmp = new Bitmap(pictureBox1.Width,
pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0,
pictureBox1.Width, pictureBox1.Height));
bmp.Save("img1.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);
break;

The PictureBox is on a form and is visible with the drawing when I
execute the code.

I'd appreciate any help with finding the bug.

-NiTiN
 
R

RobinS

Why are you drawing in a picture box? WHy can't you just draw it on the
form? In fact, you don't have to draw it visibly anywhere; you can create a
bitmap in memory and draw on it, and then save it somewhere.

RobinS.
GoldMail, Inc.
 

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