2016-11-28 44 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. 

        } 
      } 

希望这些帮助。