如何浏览并保存文件夹中的图像?

时间:2012-12-08 06:03:49

标签: c#

我想浏览图片并在图片框中显示,图片应保存在一个文件夹中,该文件夹可能位于C:或D:驱动器中,我使用以下编码进行浏览并在图片框中显示

OpenFileDialog open = new OpenFileDialog();
        open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
        if (open.ShowDialog() == DialogResult.OK)
        {
            Image img = new Bitmap(open.FileName);
            string imagename = open.SafeFileName;
            Txt_countrylogo.Text = imagename;
            pictureBox2.Image = img.GetThumbnailImage(340, 165, null, new IntPtr());
            open.RestoreDirectory = true;
        }

现在我需要帮助将图像保存在文件夹中,plz提出了一些想法。

4 个答案:

答案 0 :(得分:2)

您可以使用Image类的Image.Save方法来保存图像。

img.Save(@"d:\temp\" + imagename);

答案 1 :(得分:2)

您可以使用SaveFileDialog

  var fd = new SaveFileDialog();
  fd.Filter = "Bmp(*.BMP;)|*.BMP;| Jpg(*Jpg)|*.jpg";
  fd.AddExtension = true;
  if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  {
      switch (Path.GetExtension(fd.FileName).ToUpper())
      {
          case ".BMP":
               pictureBox2.Image.Save(fd.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
               break;
          case ".JPG":
               pictureBox2.Image.Save(fd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
               break;
          case ".PNG":
               pictureBox2.Image.Save(fd.FileName, System.Drawing.Imaging.ImageFormat.Png);
               break;
          default:
               break;
        }
}

答案 2 :(得分:0)

OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
    Image img = new Bitmap(open.FileName);
    System.IO.File.Copy(open.FileName, open.FileName.Split('.')[0]+"_Copy."+open.FileName.Split('.')[1]);
    //this is an example, you give it the name you want
    string imagename = open.SafeFileName;
    Txt_countrylogo.Text = imagename;
    pictureBox2.Image = img.GetThumbnailImage(340, 165, null, new IntPtr());
    open.RestoreDirectory = true;
}

答案 3 :(得分:0)

您可以使用此代码:

Image bitmap = Image.FromFile("C:\\MyFile.bmp");
bitmap.Save("C:\\MyFile2.bmp");