0

我想浏览图片并显示在图片框中,图片应该保存在一个文件夹中,可能在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;
        }

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

4

4 回答 4

2

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

img.Save(@"d:\temp\" + imagename);
于 2012-12-08T06:06:14.263 回答
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;
        }
}
于 2012-12-08T06:19:45.223 回答
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;
}
于 2012-12-08T06:28:09.827 回答
0

您可以使用此代码:

Image bitmap = Image.FromFile("C:\\MyFile.bmp");
bitmap.Save("C:\\MyFile2.bmp");
于 2012-12-08T06:28:20.827 回答