473,770 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2208
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.*******@gmai l.comwrote in message
news:29******** *************** ***********@i12 g2000prf.google groups.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
57331
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 "System.NullReferenceException: Object reference not set to an instance of an object." Can anyone help? Thanks... --
2
2456
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 if I can convert the image to a bitmap this will work, but I can't figure out how to do this or to do it more directly. I'd appreciate any help I can get.
0
3884
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 your help, Here is the code.
8
10783
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
10341
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 actual drawing work. But after displaying all the arrows in a flick, the arrows disappers. I have debugged and came to know that the "Paint" event of the picturebox is refreshing the image, so the work done on the CreateGraphics created object is...
4
10235
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 is that these lines are not recognized as the image of the picturebox. It shows picturebox.image = nothing. How do we convert these lines drawn as the image of the picturebox so that it can be saved.
1
2500
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 has an image of a house with a door in it and the user is interested in the door. the rectangle is selected by placing the mouse on the upper left corner of the door, pressing down on the left mouse button, and draging it to the lower right...
7
6045
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 calculation and update the picture. i currently use the GDI SetPixel method on each pixel of the Pictureboxes image. This is proving far to slow, about 1.5 seconds on average. This app needs to display the update as fast as possible. Has anyone got any...
12
2917
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 homepage. One day, I thought: Why not just make a little program in C#, that just shows me the .gif-picture of the strip? And so I did. Or, more correctly, I tried to do.
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.