文件流(从pdf转换器的jpeg)到字节[]

本文关键字:jpeg 到字节 转换器 pdf 文件 | 更新日期: 2023-09-27 18:15:12

使用aspose,我已经将pdf文档的第一页转换为jpeg(用作"文档"部分的缩略图到我的asp.net页面之一)。到目前为止,这是存储在FileStream中-但是我需要一个字节数组来分配给Image控件的数据值。谁能告诉我转换的正确方向?我四处找了一下,还是找不到解决办法。

文件流(从pdf转换器的jpeg)到字节[]

应该可以:

byte[] data = File.ReadAllBytes("path/to/file.jpg")

var memStream = new MemoryStream();
yourFileStream.CopyTo(memStream);
var bytes = memStream.ToArray();

你可以试试....

     /// <summary>
/// Function to get byte array from a file
/// </summary>
/// <param name="_FileName">File name to get byte array</param>
/// <returns>Byte Array</returns>
public byte[] FileToByteArray(string _FileName)
{
    byte[] _Buffer = null;
    try
    {
        // Open file for reading
        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        // attach filestream to binary reader
        System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
        // get total byte length of the file
        long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
        // read entire file into buffer
        _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);
        // close file reader
        _FileStream.Close();
        _FileStream.Dispose();
        _BinaryReader.Close();
    }
    catch (Exception _Exception)
    {
        // Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
    }
    return _Buffer;
}