C# 将图像转换为字节[]时出错
我需要将图像转换为字节[]:C# 将图像转换为字节[]时出错,c#,asp.net,C#,Asp.net,我需要将图像转换为字节[]: byte[] imageBytes = File.ReadAllBytes("user-defolt.png"); 但我有以下错误: 您必须提供要读取的文件的完整路径,否则它会在可执行文件所在的目录中查找该文件 byte[] imageBytes = File.ReadAllBytes(@"C:\Users\Desktop\file.png") // Some arbitary path. 使用物理路径: byte[] imageBytes = File
byte[] imageBytes = File.ReadAllBytes("user-defolt.png");
但我有以下错误:
您必须提供要读取的文件的完整路径,否则它会在可执行文件所在的目录中查找该文件
byte[] imageBytes = File.ReadAllBytes(@"C:\Users\Desktop\file.png") // Some arbitary path.
使用物理路径:
byte[] imageBytes = File.ReadAllBytes(@"C:\user-defolt.png");
或
试试这个。
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();
}
希望这有帮助 如果您查找异常,则文件路径有问题 使
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.getExecutionGassembly().Location)+“\some\u folder\”
这意味着,把所有的文件都放在“一些文件”里
希望这有帮助 那个文件在哪里?这个错误再清楚不过了。这是迄今为止我所看到的最微不足道的问题
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();
}