C# 手动/自动保存图片

来源:互联网 发布:淘宝搞笑夸张好评 编辑:程序博客网 时间:2024/04/29 23:54
  1. //手动保存图片  
  2.         private void saveBtn_Click(object sender, System.EventArgs e)  
  3.         {  
  4.             bool isSave = true;  
  5.             SaveFileDialog saveImageDialog = new SaveFileDialog();  
  6.             saveImageDialog.Title = "图片保存";  
  7.             saveImageDialog.Filter= @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";  
  8.   
  9.             if(saveImageDialog.ShowDialog() == DialogResult.OK)  
  10.             {  
  11.                 string fileName = saveImageDialog.FileName.ToString();  
  12.        
  13.                 if(fileName != "" && fileName != null)  
  14.                 {  
  15.                     string fileExtName = fileName.Substring(fileName.LastIndexOf(".")+1).ToString();  
  16.   
  17.                     System.Drawing.Imaging.ImageFormat imgformat = null;       
  18.    
  19.                     if(fileExtName!="")  
  20.                     {  
  21.                         switch(fileExtName)   
  22.                         {   
  23.                             case "jpg":   
  24.                                 imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;   
  25.                                 break;   
  26.                             case "bmp":   
  27.                                 imgformat = System.Drawing.Imaging.ImageFormat.Bmp;   
  28.                                 break;   
  29.                             case "gif":   
  30.                                 imgformat = System.Drawing.Imaging.ImageFormat.Gif;   
  31.                                 break;   
  32.                             default:   
  33.                                 MessageBox.Show("只能存取为: jpg,bmp,gif 格式");   
  34.                                 isSave = false;  
  35.                                 break;   
  36.                         }   
  37.   
  38.                     }  
  39.   
  40.                     //默认保存为JPG格式  
  41.                     if(imgformat == null)  
  42.                     {  
  43.                         imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;  
  44.                     }  
  45.                       
  46.                     if(isSave)  
  47.                     {  
  48.                         try  
  49.                         {  
  50.                                 this.pictureBox1.Image.Save(fileName,imgformat);  
  51.                                 //MessageBox.Show("图片已经成功保存!");  
  52.                         }  
  53.                         catch  
  54.                         {  
  55.                             MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");  
  56.                         }  
  57.                     }  
  58.   
  59.                 }  
  60.   
  61.             }  
  62.         }  
  63.   
  64.         //自动保存图片  
  65.         private void Autosave()  
  66.         {  
  67.             string Opath =@"D:/VedioCapture/Photo";  
  68.             string photoname = DateTime.Now.Ticks.ToString();  
  69.             if (Opath.Substring(Opath.Length-1, 1) != @"/")  
  70.                 Opath = Opath + @"/";  
  71.             string path1 = Opath + DateTime.Now.ToShortDateString();  
  72.             if (! Directory.Exists(path1))            
  73.                 Directory.CreateDirectory(path1);  
  74.             //pictureBox1.Image.Save(path1 +"//" + photoname + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);  
  75.             //图像的缩小  
  76.             System.Drawing.Bitmap objPic,objNewPic;  
  77.             try  
  78.             {  
  79.                 objPic = new System.Drawing.Bitmap(pictureBox1.Image);  
  80.                 objNewPic=new System.Drawing.Bitmap(objPic,pictureBoxShow.Width,pictureBoxShow.Height);  
  81.                 //objNewPic=new System.Drawing.Bitmap(objPic,320,240);//图片保存的大小尺寸  
  82.                 objNewPic.Save(path1 +"//" + photoname + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);  
  83.             }  
  84.             catch(Exception exp){throw exp;}  
  85.             finally  
  86.             {  
  87.                 objPic=null;  
  88.                 objNewPic=null;  
  89.             }  
  90.         }