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

How to save picturebox image into jpg file?

P: n/a
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
Share this Question
Share on Google+
12 Replies


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

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

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

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

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

P: n/a
Nope..I din draw any rectangle with black color...

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

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

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

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

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

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

P: n/a
Thanks for the code...I will try it out...
Million thanks again... ^_^

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

This discussion thread is closed

Replies have been disabled for this discussion.