将图像转换为字节[]

时间:2011-09-27 17:29:45

标签: asp.net-mvc-3 c#-4.0

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

我会 非常感谢任何帮助

谢谢。

1 个答案:

答案 0 :(得分:12)

byte[] buffer = File.ReadAllBytes("foo.png");

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

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