FileStream(来自 pdf 转换器的 jpeg)到 Byte[]

发布于 2024-12-16 16:57:06 字数 162 浏览 3 评论 0原文

使用 aspose,我已将 pdf 文档的第一页转换为 jpeg(用作我的一个 asp.net 页面的“文档”部分中的缩略图)。到目前为止,它存储在 FileStream 中 - 但我需要一个字节数组来分配给图像控件的数据值。谁能指出我转换这个的正确方向?我环顾四周,但找不到解决方案。

多谢。

Using aspose, I have converted the first page of a pdf document to a jpeg (to be used as a thumbnail in a 'Documents' section to one of my asp.net pages). This is, upto this point, stored in a FileStream - but I need a byte array to assign to the datavalue of an Image control. Can anyone point me in the right direction to converting this? I've had a good look around and I can't find the solution.

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

变身佩奇 2024-12-23 16:57:06

这应该有效:

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

This should work:

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

憧憬巴黎街头的黎明 2024-12-23 16:57:06
var memStream = new MemoryStream();
yourFileStream.CopyTo(memStream);
var bytes = memStream.ToArray();
var memStream = new MemoryStream();
yourFileStream.CopyTo(memStream);
var bytes = memStream.ToArray();
等你爱我 2024-12-23 16:57:06

你可以试试这个......

     /// <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;
}

you can try this....

     /// <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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文