I have an image in my images folder
我的图像文件夹中有一个图像
and I want to pass this image to an Image object so I can then convert it to a base 64 string. This will then be passed to the client and inserted into the src of a img element.
我想将此图像传递给Image对象,然后我可以将其转换为基本64字符串。然后将其传递给客户端并插入到img元素的src中。
The problem is that I don't know how to get the image from disk into the image object.
问题是我不知道如何将图像从磁盘放入图像对象。
Image img = Image.FromFile(@"..Images\no_image.jpg"); // doesn't work
string image = Convert.ToBase64String(imageToByteArray(img));
public byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
1 个解决方案
#1
4
If you work on a web project, you should use:
如果您在Web项目上工作,则应使用:
Image.FromFile(Server.MapPath("~/Images/no_image.jpg"))
else if it is a WinForm/WPF/Console application, use:
否则,如果它是WinForm / WPF / Console应用程序,请使用:
Image.FromFile(System.IO.Path.Combine(Environment.CurrentDirectory, "Images", "no_image.jpg"))
#1
4
If you work on a web project, you should use:
如果您在Web项目上工作,则应使用:
Image.FromFile(Server.MapPath("~/Images/no_image.jpg"))
else if it is a WinForm/WPF/Console application, use:
否则,如果它是WinForm / WPF / Console应用程序,请使用:
Image.FromFile(System.IO.Path.Combine(Environment.CurrentDirectory, "Images", "no_image.jpg"))