Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将图像转换为字节[]时出错_C#_Asp.net - Fatal编程技术网

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();
    }