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

PictureBox doesn't copy drawn content to Bitmap

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

Expand|Select|Wrap|Line Numbers
  1. pictureBox1.BackColor = Color.AntiqueWhite;
  2.  
  3. Graphics gfx = pictureBox1.CreateGraphics();
  4. gfx.SmoothingMode =
  5. System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  6. Pen pn = Pens.Black;
  7. Pen pnAlt = Pens.Blue;
  8. gfx.DrawLine(pn, 0, 0, 80, 80);
  9. gfx.DrawPie(pn, 80, 80, 100, 100, 0, 14);
  10. gfx.DrawPie(pnAlt, 80, 80, 100, 100, 15, 14);
  11. gfx.Flush();
  12. gfx.Dispose();
  13.  
  14. Bitmap bmp = new Bitmap(pictureBox1.Width,
  15. pictureBox1.Height);
  16. pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0,
  17. pictureBox1.Width, pictureBox1.Height));
  18. bmp.Save("img1.jpg",
  19. System.Drawing.Imaging.ImageFormat.Jpeg);
  20. break;
  21.  
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
Feb 6 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
-----------------------------
"Nitin" <k.*******@gmail.comwrote in message
news:29**********************************@i12g2000 prf.googlegroups.com...
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:

Expand|Select|Wrap|Line Numbers
  1.                    pictureBox1.BackColor = Color.AntiqueWhite;
  2.                    Graphics gfx = pictureBox1.CreateGraphics();
  3.                    gfx.SmoothingMode =
  4. System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  5.                    Pen pn = Pens.Black;
  6.                    Pen pnAlt = Pens.Blue;
  7.                    gfx.DrawLine(pn, 0, 0, 80, 80);
  8.                    gfx.DrawPie(pn, 80, 80, 100, 100, 0, 14);
  9.                    gfx.DrawPie(pnAlt, 80, 80, 100, 100, 15, 14);
  10.                    gfx.Flush();
  11.                    gfx.Dispose();
  12.                    Bitmap bmp = new Bitmap(pictureBox1.Width,
  13. pictureBox1.Height);
  14.                    pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0,
  15. pictureBox1.Width, pictureBox1.Height));
  16.                    bmp.Save("img1.jpg",
  17. System.Drawing.Imaging.ImageFormat.Jpeg);
  18.                    break;
  19.  

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
Feb 15 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.