C#:将PictureBox上的图像保存到计算机上的文件夹中

时间:2021-07-13 00:23:06

I want to save a picture from a PictureBox to a default path. How can I do this?

我想将图片从PictureBox保存到默认路径。我怎样才能做到这一点?

Here is what I have so far:

这是我到目前为止:

    private void bntSave_Click(object sender, EventArgs e)
    {
       SaveImageCapture(imgCapture.Image);
    }

    public static void SaveImageCapture(System.Drawing.Image image)
    {

        SaveFileDialog s = new SaveFileDialog();
        s.FileName = "Image";// Default file name
        s.DefaultExt = ".Jpg";// Default file extension
        s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension

        // Show save file dialog box
        // Process save file dialog box results
        if (s.ShowDialog() == DialogResult.OK)
        {
            // Save Image
            string filename = s.FileName;
            FileStream fstream = new FileStream(filename, FileMode.Create);
            image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
            fstream.Close();

        }

    }

1 个解决方案

#1


0  

Use the FileDialog InitialDirectory property. Modify your SaveImageCapture method similar to below.

使用FileDialog InitialDirectory属性。修改您的SaveImageCapture方法,类似于下面。

public static void SaveImageCapture(System.Drawing.Image image)
{
    SaveFileDialog s = new SaveFileDialog();
    s.FileName = "Image";// Default file name
    s.DefaultExt = ".Jpg";// Default file extension
    s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension

    // Below are two examples of setting the initial (default) folder - choose one

    // 1. example of setting the default folder to a special folder
    s.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

    // 2 example of setting the default folder to an absolute path
    //s.InitialDirectory = ("C:\\Temp");

    // setting the RestoreDirectory property to true causes the
    // dialog to restore the current directory before closing
    s.RestoreDirectory = true;
    // Show save file dialog box
    // Process save file dialog box results
    if (s.ShowDialog() == DialogResult.OK)
    {
        // Save Image
        string filename = s.FileName;
        // the using statement causes the FileStream's dispose method to be
        // called when the object goes out of scope
        using (System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.Create))
        {
            image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
            fstream.Close();
        }
    }
}

#1


0  

Use the FileDialog InitialDirectory property. Modify your SaveImageCapture method similar to below.

使用FileDialog InitialDirectory属性。修改您的SaveImageCapture方法,类似于下面。

public static void SaveImageCapture(System.Drawing.Image image)
{
    SaveFileDialog s = new SaveFileDialog();
    s.FileName = "Image";// Default file name
    s.DefaultExt = ".Jpg";// Default file extension
    s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension

    // Below are two examples of setting the initial (default) folder - choose one

    // 1. example of setting the default folder to a special folder
    s.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

    // 2 example of setting the default folder to an absolute path
    //s.InitialDirectory = ("C:\\Temp");

    // setting the RestoreDirectory property to true causes the
    // dialog to restore the current directory before closing
    s.RestoreDirectory = true;
    // Show save file dialog box
    // Process save file dialog box results
    if (s.ShowDialog() == DialogResult.OK)
    {
        // Save Image
        string filename = s.FileName;
        // the using statement causes the FileStream's dispose method to be
        // called when the object goes out of scope
        using (System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.Create))
        {
            image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
            fstream.Close();
        }
    }
}