473,890 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to save picturebox image into jpg file?

Hi, I have a picture box with circles and rectangles, and I wana save all
the images into a jpg file, i tried
pictureBox1.Ima ge.Save(@"c:\1. jpg");
but I got and error
"System.NullRef erenceException : Object reference not set to an instance of
an object."

Can anyone help? Thanks...

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #1
12 57362
Hi yaya,

Are you painting directly on a PictureBox? (which btw is not what a
PictureBox is meant for).
You need to somehow do the painting on a Bitmap, for instance when in your
paint procedure paint on the Bitmap instead and at the end use
DrawImageUnscal ed to paint the result onto some control, or put the bitmap
into the PictureBox.Imag e property. You can then use Image.Save(file name,
ImageFormat) to save the Bitmap to file. Use Graphics.FromIm age() to get
a paint canvas for your paint procedure.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
I'm drawing the shapes with the paint event of the picturebox,
Graphics g = e.Graphics;
g.DrawLine(myPe n,new Point(xStart,yS tart),new Point(xEnd,ySta rt));
Nov 16 '05 #3
Create a Bitmap the size of your drawing board or any size you prefer.

Bitmap bm = new Bitmap(pictureB ox1.Width, pictureBox1.Hei ght);

Inside your drawing procedure draw to the Bitmap instead of the PictureBox
In the end, draw the entire Bitmap to the PictureBox (ps, PictureBox is
meant for showing images, not to draw upon. Not that it matters, but it's
a bit like shooting sparrows with cannons. I prefer a simple Panel that I
can move around. You can draw on any Control, including directly onto a
Windows Form)

Graphics g = Graphics.FromIm age(bm);
g.DrawLine(myPe n,new Point(xStart,yS tart),new Point(xEnd,ySta rt));
Nov 16 '05 #4
Ya...it works...but how can I get a blank jpeg full with black color?
Thank again :D

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #5
Um, do you mean something like

g.FillRectangle (Brushes.Black, 0, 0, bm.Width, bm.Height);
On Sat, 12 Mar 2005 09:05:23 GMT, yaya via DotNetMonster.c om
<fo***@DotNetMo nster.com> wrote:
Ya...it works...but how can I get a blank jpeg full with black color?
Thank again :D


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
Nope..I din draw any rectangle with black color...

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #7
In that case I'm not sure I understand your question.
You can fill the Bitmap with black color using the FillRectangle method.

On Sun, 13 Mar 2005 08:31:48 GMT, yaya via DotNetMonster.c om
<fo***@DotNetMo nster.com> wrote:
Nope..I din draw any rectangle with black color...


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #8
Ops....sorry, I have misleading you...I meant that I got a blank black
picture in jpeg instead of the picture that shown in the picturebox...as I
din draw any retangle with black color... :p

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #9
I'm confused, do you draw on a PictureBox that is already showing a
Picture? If so, use that Image for Graphics.FromIm age, if not, please
tell us, in some detail, what you are doing now, and what you want to
accomplish.
On Sun, 13 Mar 2005 19:24:26 GMT, yaya via DotNetMonster.c om
<fo***@DotNetMo nster.com> wrote:
Ops....sorry, I have misleading you...I meant that I got a blank black
picture in jpeg instead of the picture that shown in the picturebox...as
I
din draw any retangle with black color... :p


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
31552
by: Pavan Arise | last post by:
Dear all.. I have a picturebox filled on a form. The picturebox has some graphics displayed on it.I was trying to save the picturebox, but continuesly failed to do so. I am clueless of why it is happening. I tried to save the image of the form & also the picturebox, but always, i see a blank file saved. Is there a way to save whatever is displayed on the picturebox, to a file?? Thanks for any suggestions and help. Regards,
6
61796
by: charsh | last post by:
Hi, I using the code below to draw a text in a pictureBox1. //Start--------------------------------------------------------------- private void button1_Click(object sender, System.EventArgs e) { Graphics g; g = pictureBox1.CreateGraphics(); g.FillRectangle(Brushes.White,0,0,160,160);
7
25516
by: user | last post by:
Hello I tried: pictureBox1.Image.Save("file.sav"); compiled, but when i run i receive errors: Additional information: Object reference not set to an instance of an object. Why ?
4
10244
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.
4
2015
by: TomA | last post by:
Hi All, I have a picturebox on a form containing the photo of a person. As you advance through the records, the photo updates. Rather than storing the images in an inefficient blob field in a table, I have the separate images stored in an image directory with primary key as the filename (3476.jpg). If the image exists, I display the image. All this works fine. I have a buttons that allow for the insertion and deletion of photos. ...
12
4061
by: Lee | last post by:
I have the following code using System; using System.Windows.Forms; using System.Drawing; using System.Collections; namespace FINN {
7
4925
by: Mads Aggerholm | last post by:
Hello Sirs, I am having a BMP loadet into a picturebox. This I'd like to save on my harddisk as a GIF: picturebox1.Image.Save("C:\\DATA\\PICTURES\\testpic.gif", ImageFormat.Gif); However, this GIF-file is not in very good quality.
4
5609
by: gerardianlewis | last post by:
Any help appreciated. (VB.NET under XP and Vista, SP1 installed) My code, inherited from a VB6 version of an app that ran under W98, loads an image from a file into a PictureBox. The user may want to move or delete the file that is being shown. The corresponding event- driven code for "delete this picture" uses: My.Computer.FileSystem.DeleteFile(sPath)
10
7437
by: eddie tan | last post by:
Hi, I have a picturebox with graphics drawn from different objects. In one object I used Pen P1; P1 = new Pen(Color.Blue, 3); Graphics g = null; g = Graphics.FromImage(image.Image); g.DrawPath(P1, Wirepath);
0
9976
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
9814
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
11214
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
10449
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
9616
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...
0
6034
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4659
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
4257
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.