Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've done the OpenFileDialog and it works but I struggle with doing the SaveFileDialog.

I have to do a "Paint-similar application", I've done a pen, brush, and drawing figures, but there is problem when I try to save my drawing. Im drowing on picutreBox.

What I have tried:

Open File Code:
C#
<pre>
private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Open an image";
            openFileDialog1.Filter = "JPEG|*.jpeg|GIF|*.gif|BMP|*.bmp|TIFF|*.tiff|PNG|*.png|JPG|*.jpg";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    file = Image.FromFile(openFileDialog1.FileName);
                    pictureBox1.Image = file;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            } 
        }


SaveFileDialog:
<pre lang="c#">
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

            saveFileDialog1.Title = "Save image";
            saveFileDialog1.Filter = "BMP|.bmp|JPEG|*.jpeg";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    file.Save(saveFileDialog1.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Posted
Updated 1-Mar-18 1:36am
Comments
CHill60 1-Mar-18 9:41am    
Where is the code that intialises the saveFileDialog1 object?
Has the variable file been initalised before calling the save method? If not try something like
file = pictureBox1.Image;
before attempting the save
Member 13504137 1-Mar-18 9:52am    
I've added saveFileDialog to my From from Toolbox. About "file":

public Form1()
{
InitializeComponent();
Graphics g = pictureBox1.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
file = pictureBox1.Image;
}

public partial class Form1 : Form
{
Image file;
...
}
and now I've tried:

saveFileDialog1.Title = "Saving image";
saveFileDialog1.Filter = "JPEG|*.jpeg";
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
file = pictureBox1.Image;
try
{
Graphics g = pictureBox1.CreateGraphics();
file.Save(saveFileDialog1.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
CHill60 1-Mar-18 9:56am    
And is there actually an image in pictureBox1 at that point - use your debugger to find out. I have no idea why you have added the CreateGraphics bit.
Member 13504137 1-Mar-18 10:00am    
No, I do run my form, then draw something with pen on pictureBox1, then go to "Save" in my menuStrip and it opens the save file dialog then I enter a filename and when I click "Save" it shows me the Exception message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900