如何在C#中使用savefiledialog保存图像和pdf文件 [英] How to save image and pdf file using savefiledialog in C#

查看:431
本文介绍了如何在C#中使用savefiledialog保存图像和pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想使用savefiledialog以相同的方法保存图像和pdf文件。这里的图片和pdf来自数据库。



我尝试过:



用于保存图像和pdf在数据库中我正在使用文件流。我可以使用相同的方法保存到文件夹

hello i want to save the image and pdf file in same method using the savefiledialog. here image and pdf are coming from database.

What I have tried:

for saving the image and pdf in database i am using file stream. can i use same for saving to folder as well

推荐答案

SaveFileDialog 只是从用户那里获取文件和目录信息,它对这些文件没有任何作用。您需要编写代码来执行实际的保存。您可以使用FileStream将数据复制到本地文件。
SaveFileDialog just gets file and directory information from the user, it does not do anything with those files. You need to write the code to do the actual saves. You can use FileStream to copy the data to local files.


我会写一些像这段代码一样简单的东西:





I would write something simple like this piece of code :


private void button2_Click(object sender, System.EventArgs e)
{
   // Displays a SaveFileDialog so the user can save the Image
   // assigned to Button2.
   SaveFileDialog saveFileDialog1 = new SaveFileDialog();
   saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
   saveFileDialog1.Title = "Save an Image File";
   saveFileDialog1.ShowDialog();

   // If the file name is not an empty string open it for saving.
   if(saveFileDialog1.FileName != "")
   {
      // Saves the Image via a FileStream created by the OpenFile method.
      System.IO.FileStream fs = 
         (System.IO.FileStream)saveFileDialog1.OpenFile();
      // Saves the Image in the appropriate ImageFormat based upon the
      // File type selected in the dialog box.
      // NOTE that the FilterIndex property is one-based.
      switch(saveFileDialog1.FilterIndex)
      {
         case 1 : 
         this.button2.Image.Save(fs, 
            System.Drawing.Imaging.ImageFormat.Jpeg);
         break;

         case 2 : 
         this.button2.Image.Save(fs, 
            System.Drawing.Imaging.ImageFormat.Bmp);
         break;

         case 3 : 
         this.button2.Image.Save(fs, 
            System.Drawing.Imaging.ImageFormat.Gif);
         break;
      }

   fs.Close();
   }
}


这篇关于如何在C#中使用savefiledialog保存图像和pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆