2014-01-22 41 views
-1

我需要图像转换为字节]错误转换图像时的byte []

byte[] imageBytes = File.ReadAllBytes("user-defolt.png"); 

但我有以下错误:

enter image description here

+3

该文件位于何处?错误不可能比这更清晰。 –

+2

这是我在SO –

回答

1

使用物理路径:

byte[] imageBytes = File.ReadAllBytes(@"C:\user-defolt.png"); 

byte[] imageBytes = File.ReadAllBytes(Server.MapPath("user-defolt.png")); 
1

您必须提供您想要读取的文件的完整路径,其他方式则是查找您的可执行文件所在目录中的文件。

byte[] imageBytes = File.ReadAllBytes(@"C:\Users\Desktop\file.png") // Some arbitary path. 
+0

上看到的最微不足道的问题为什么它被低估? –

0


尝试了这一点。

public byte[] imageToByteArray(System.Drawing.Image imageIn) 
    { 
     MemoryStream ms = new MemoryStream(); 
     imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //Save any type of format you want, like jpeg, png,gif etc 
     return ms.ToArray(); 
    } 

希望这有助于!

0

如果你看异常,有一个文件路径的问题。

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\some_folder\" 这意味着,获得所有文件 “some_filder”

希望这有助于!