2016-11-28 45 views
0

我不知道爲什麼這給了我一個錯誤,我打開圖片,然後我pu在流中,然後我用另一個名字保存流圖片,很簡單打開的圖像投入流並保存它

 string pic = openFileDialog1.FileName; 

     string filePath; 

     filePath = pic; 

     Bitmap bmp = null; 



     // Create from a stream so we don't keep a lock on the file. 
     using (var stream = File.OpenRead(filePath)) 
     { 
      bmp = (Bitmap)Bitmap.FromStream(stream); 
     } 


     bmp.Save(pic + "sdf.jpg"); 
+0

什麼是例外?什麼是異常消息? –

+3

來自[MSDN](https://msdn.microsoft.com/en-us/library/z7ha67kw(v = vs.110).aspx):_您必須保持該流在Bitmap的生命週期中處於打開狀態._ –

回答

0

如下您可以修改代碼,

  if (openFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       string pic = openFileDialog1.FileName; 
       string filePath; 
       string newfilepath;//here you should assign your newfilepath 
       filePath = pic; 
       Bitmap bmp = null; 

       using (var stream = File.OpenRead(filePath)) 
        { 
        bmp = (Bitmap)Bitmap.FromStream(stream); 
        bmp.Save(newfilepath, System.Drawing.Imaging.ImageFormat.Jpeg);//here you can change the format i.e. bmp,jpg,png etc. 

        } 
      } 

希望這些幫助。