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

How to save pictureBox to a file ?

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

Thanx
Michal

Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
What you want to do is instead of drawing on the pictureBox you want to create
an Image, draw invisibly on that, then put the Image on the pictureBox.
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Image bmp = pictureBox1.Image;
Graphics g = Graphics.FromImage(bmp);

//Edit bmp...any drawing goes here using 'g'
g.FillRectangle(new SolidBrush(Color.White), 0, 0, pictureBox1.Width,
pictureBox1.Height);
for(int I=0; I < pictureBox1.Width; I+)
g.DrawLine(new Pen(Color.Black), pictureBox1.Width - I, 0, 0, I);

//Display bmp
pictureBox1.Image = bmp;

then to save you could save the pictureBox.Image or you could make bmp public
and save that directly either would work, the second probably would be better.

Hope that answers your question.
On 15/04/2004 us**@domain.invalid wrote:
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 ?

Thanx
Michal

Nov 16 '05 #2

P: n/a
This piece of code works fine for me either:

// Code:
private void buttonSave_Click(object sender, System.EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Bitmap files (*.bmp)|*.bmp|JPG files (*.jpg)|*.jpg|GIF
files (*.gif)|*.gif|All files (*.*)|*.*";
save.FilterIndex = 4;
save.RestoreDirectory = true;

if(save.ShowDialog() == DialogResult.OK)
{
myPictureBox.Image.Save(save.FileName);
}
}
Nov 16 '05 #3

P: n/a
Hello

That's strange, i have identical code and still the same error:

SaveFileDialog save = new SaveFileDialog();
save.Filter = "Bitmap files (*.bmp)|*.bmp|JPG files (*.jpg)|*.jpg|GIF
files (*.gif)|*.gif|All files (*.*)|*.*";
save.FilterIndex = 4;
save.RestoreDirectory = true;
if(save.ShowDialog() == DialogResult.OK)
{/*LINE 100: HERE IS THE PROBLEM*/
pictureBox1.Image.Save(save.FileName);
}

when i check in line 100 i receive that pictureBox1.Image==null,
but how is it possible ?? Durring saving i see my paintings
on that pictureBox.

Thanx
Michal

Nov 16 '05 #4

P: n/a


i found that only the second method described by Scatropolis works for
me fine. I wonder why...

Nov 16 '05 #5

P: n/a
hmm for some reason it didn't paste correctly.....

On 15/04/2004 "Scatropolis" <ch*****@frayed.net> wrote:
What you want to do is instead of drawing on the pictureBox you want to createan Image, draw invisibly on that, then put the Image on the pictureBox.
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Image bmp ictureBox1.Image;
Graphics g raphics.FromImage(bmp);

//Edit bmp...any drawing goes here using 'g'
g.FillRectangle(new SolidBrush(Color.White), 0, 0, pictureBox1.Width,
pictureBox1.Height);
for(int I I < pictureBox1.Width; I+)
g.DrawLine(new Pen(Color.Black), pictureBox1.Width - I, 0, 0, I);

//Display bmp
pictureBox1.Image mp;


this line should be

picturebox1.Image = bmp;
Nov 16 '05 #6

P: n/a
Ack it seems when I pasted it letters got deleted. trying to change everything

pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Image bmp = PictureBox1.Image;
Graphics g = Graphics.FromImage(bmp);

//Edit bmp...any drawing goes here using 'g'
g.FillRectangle(new SolidBrush(Color.White), 0, 0, pictureBox1.Width,
pictureBox1.Height);
for(int I I < pictureBox1.Width; I+)
g.DrawLine(new Pen(Color.Black), pictureBox1.Width - I, 0, 0, I);

//Display bmp
pictureBox1.Image = bmp;

there....that's kinda weird, sorry
Nov 16 '05 #7

P: n/a
When you draw on a pictureBox it is not saved into the image. I don't think
it's even saved anywhere. Which is why when it gets minimized or covered over
it's erased.

On 15/04/2004 us**@domain.invalid wrote:


i found that only the second method described by Scatropolis works for
me fine. I wonder why...

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.