How to save pictureBox to a file ?

  • Thread starter Thread starter user
  • Start date Start date
U

user

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
 
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.
 
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);
}
}
 
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
 
i found that only the second method described by Scatropolis works for
me fine. I wonder why...
 
hmm for some reason it didn't paste correctly.....

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top