Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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图像在转换到bin期间丢失_C#_Image - Fatal编程技术网

C# C图像在转换到bin期间丢失

C# C图像在转换到bin期间丢失,c#,image,C#,Image,因此,我使用以下脚本将图像转换为二进制文件,以便将其放在blob上: public static byte[] ImagemBin(string imagePath, int imagem_comp) { FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[imagem_comp];

因此,我使用以下脚本将图像转换为二进制文件,以便将其放在blob上:

public static byte[] ImagemBin(string imagePath, int imagem_comp)
    {

        FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[imagem_comp];

        int numBytesToRead = imagem_comp;
        int numBytesRead = 0;
        while (numBytesToRead > 0)
        {
            // Read may return anything from 0 to numBytesToRead.
            int n = fileStream.Read(buffer, numBytesRead, numBytesToRead);

            // Break when the end of the file is reached.
            if (n == 0)
                break;

            numBytesRead += n;
            numBytesToRead -= n;
        }
        numBytesToRead = buffer.Length;

       fileStream.Read(buffer, 0, numBytesToRead);

        fileStream.Close();
        return buffer;
    }
其中imagePath是图像在计算机中的位置,imagem_comp是图像的大小。但是,图像转换是不完整的,在几个字节后,它将唯一返回0的

那么,有什么帮助吗


提前谢谢你。

你应该解释一下你想要完成什么。比如imagem_comp应该是什么?我发现你的逻辑有以下错误

在每次迭代中覆盖缓冲区,而不做任何操作。 您正在从此行numBytesToRead-=n;上要读取的字节数中减去读取的字节数;。在一次读取之后,您将只要求读取0字节。 再次使用与循环外第一次迭代基本相同的数据覆盖缓冲区。 假设以上所有内容都已修复,则如果文件包含的字节数大于imagem_comp,则返回的缓冲区不包含所有数据。
正如@bmm6o在上述评论中所述,您应该能够将其替换为。

它似乎适用于此版本:

public static byte[] ImagemBin(string imagePath, int imagem_comp)
{

    FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
    byte[] buffer = new byte[imagem_comp];

    int numBytesToRead = imagem_comp;
    int numBytesRead = 0;
    while (numBytesToRead > 0)
    {
        // Read may return anything from 0 to numBytesToRead.
        int n = fileStream.Read(buffer, numBytesRead, numBytesToRead);

        // Break when the end of the file is reached.
        if (n == 0)
            break;

        numBytesRead += n;
        numBytesToRead -= n;
    }

    fileStream.Close();
    return buffer;
}
即,移除管线:

numBytesToRead = buffer.Length;


尽管如此,我同意其他人提到的;考虑使用RealAlelyByth.:- 为什么你在循环中阅读,然后又在循环外阅读?imagem_comp正确吗?这是从哪里来的?为什么要使用fileStream.Readbuffer、0、numBytesToRead?File.ReadAllBytes有什么问题?我必须创建自己的函数来执行此操作,bmm6oa和imagem_comp来自您选择图像的表单。一个FileInfo.lenght…如果调用函数时知道确切的文件大小,那么这样做是可行的,但逻辑基本上仍然是错误的,因为imagem_comp被视为缓冲区大小。1。ReadAllBytes的功能与我的函数完全相同,它不会上载完整的文件。2.我应该做我自己的功能来做到这一点。3.imagem_comp是图像的精确大小。尝试了File.ReadAllBytes,不知怎的,这就是存储在我的blob字段中的内容:System.Byte[]。我仍然必须做一个函数,将图像转换为二进制。ReadAllBytes确实创建了图像的内存二进制表示形式。如果FindAllBytes不起作用,那么您在其他地方做了不正确的事情;之前的任何操作都是从表单中获取更多信息,之后是mySql查询。如果您在数据库中获取System.Byte[],这是因为您插入的是对对象字符串的描述,而不是数据本身。因此,您将二进制文件错误地插入到查询中。以下是查询:string myQuery=INSERT into livros tituloLivros、dataedicaoLivros、isbnLivros、autoresLivros、Granolivros、editorasLivros、imagemLivros、nomeImagemLivros、Elazerivros、resumoLivros、pedidoLivros、disponivelLivros、SizeManagelivros值“+titulo+”,“+数据字符串+”、“+isbn+”、“+autor+”、“+慷慨解囊+”、“+editora+”、“+imagem+”、“+nomeimagem+”、“+lazer+”、“+resumo+”、“+pedidos+”、“+disponivel+”、“+imagem(comp+”;我是否需要更改其中的任何内容以使其正常工作?
fileStream.Read(buffer, 0, numBytesToRead);