473,401 Members | 2,068 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,401 software developers and data experts.

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.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...

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #1
12 57220
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
DrawImageUnscaled to paint the result onto some control, or put the bitmap
into the PictureBox.Image property. You can then use Image.Save(filename,
ImageFormat) to save the Bitmap to file. Use Graphics.FromImage() 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(myPen,new Point(xStart,yStart),new Point(xEnd,yStart));
Nov 16 '05 #3
Create a Bitmap the size of your drawing board or any size you prefer.

Bitmap bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);

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.FromImage(bm);
g.DrawLine(myPen,new Point(xStart,yStart),new Point(xEnd,yStart));
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.com
<fo***@DotNetMonster.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.com
<fo***@DotNetMonster.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.FromImage, 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.com
<fo***@DotNetMonster.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
Sorry for the confusion, actually I have a blank picturebox initially, and
my program will draw some points and lines...and lastly, when the save
button is clicked, the picture(points and lines) drawn will save into a
jpeg file. It works and a jpeg file was saved, but the image of the file
was a blank black image instead of what the program has drawn on the
picturebox. Are you clear now? Thankx...

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #11
Well, you may have overlooked a point or two, so I'm repeating the
procedure.

All the drawing you are doing, do it directly on the Bitmap.

Whenever you need to refresh the screen or display the result of the
drawing, paste the Bitmap onto the PictureBox, using
Graphics.DrawImageUnscaled(Bitmap, x, y)

When you need to save the drawing to file, you just call Image.Save on the
Bitmap you use for drawing.

Bitmap bm;

public TestForm()
{
bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// If you aren't doing any drawing here you might just paste the Bitmap

// Graphics g = Graphics.FromImage(bm);
// g.DrawString("Hello World", Brushes.Red, this.Font, 5, 5);
// g.Dispose();

e.Graphics.DrawImageUnscaled(bm, 0, 0);
}

private void button1_Click(object sender, EventArgs e)
{
bm.Save("test.jpg", ImageFormat.Jpeg);
}

private void DrawSomeStuff()
{
using(Graphics g = Graphics.FromImage(bm))
{
g.DrawString("Hello World", Brushes.Red, this.Font, 5, 5);
}
using(Graphics h = pictureBox1.CreateGraphics())
{
h.DrawImageUnscaled(bm, 0, 0);
}
}

(PS! code may contain typos as it is not tested)

On Mon, 14 Mar 2005 14:27:56 GMT, yaya via DotNetMonster.com
<fo***@DotNetMonster.com> wrote:
Sorry for the confusion, actually I have a blank picturebox initially,
and
my program will draw some points and lines...and lastly, when the save
button is clicked, the picture(points and lines) drawn will save into a
jpeg file. It works and a jpeg file was saved, but the image of the file
was a blank black image instead of what the program has drawn on the
picturebox. Are you clear now? Thankx...


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #12
Thanks for the code...I will try it out...
Million thanks again... ^_^

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #13

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

Similar topics

8
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...
6
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)...
7
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
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...
4
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...
12
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
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,...
4
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...
10
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);...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.