将图像转换为字节[]

J86*_*J86 2 c#-4.0 asp.net-mvc-3

我有一个图像,位于我的MVC3应用程序的某个文件夹中.在我的控制器中,如何将该图像转换为字节数组.

非常感谢任何帮助.

谢谢.

Dar*_*rov 12

byte[] buffer = File.ReadAllBytes("foo.png");
Run Code Online (Sandbox Code Playgroud)

并且因为它在控制器内部,您可能想要计算相对于根的路径:

string imageFile = Path.Combine(Server.MapPath("~/App_Data"), "foo.png");
byte[] buffer = File.ReadAllBytes(imageFile);
Run Code Online (Sandbox Code Playgroud)