473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

PictureBox doesn't copy drawn content to Bitmap

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
1 2146
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: yaya via DotNetMonster.com | last post by:
Hi, I have a picture box with circles and rectangles, and I wana save all the images into a jpg file, i tried pictureBox1.Image.Save(@"c:\1.jpg"); but I got and error...
2
by: Bob Henry | last post by:
Hello, This ought to be easy, but I can't figure it out. In VisualBasic.Net 2003, I've drawn graphics using drawline etc... to a picturebox. I now want to copy this to the clipboard. I think...
0
by: akh | last post by:
I want to use de Drag and Drop ´s event to move a picture box from a form and within a Picture Box. But I have behaviour if the MyPBox As PictureBox as the Globale varible or not Thanks for...
8
by: nobody | last post by:
How do I save a PictureBox bitmap with changes drawn on the bitmap? This doesn't do it: PictureBox1.Image.Save("key30.bmp")
2
by: kalp suth via DotNetMonster.com | last post by:
I want to create arrows using lines on a picture in the picture box. On clicking the button "btnShowAll", the image is loaded and the lines drawn. "RGSShowAll()" calls "DrawObjs()" which does the...
4
by: Yash | last post by:
Have used DrawLine() method to draw some lines in the picturebox (sort of a barcode) in the picturebox paint event. This diagram has to be saved as an image in bmp or tiff format. But the problem...
1
by: ray well | last post by:
hi, i need to give the user the ability to extract a rectangular area of their choice from a graphic displayed in a picture box to the clipboard, so they can use it elsewhere. say the graphic...
7
by: kebalex | last post by:
Hi, I have an app (written in .NET 2.0) which updates a picturebox according to some user input (a slider control). when the user makes a change i loop through all of the pixels, do a...
12
by: Mads Aggerholm | last post by:
Hello Sirs I hope you can give me a hint to this little problem. The thing is, I have for a lang time enjoyed the comics on www.washintonpost.com, and I allways start the day by going to their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.