2011-11-17 93 views
0

使用aspose,我已经将pdf文档的第一页转换为jpeg(用作'文档'部分中的缩略图,指向我的一个asp.net页)。到目前为止,这是存储在FileStream中 - 但我需要一个字节数组来分配给Image控件的数据值。任何人都可以指出我正确的方向来转换?我有一个很好的看看,我找不到解决方案。FileStream(pdf转换器中的jpeg)到Byte []

非常感谢。

回答

4

这应该工作:

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

+0

我不是实际保存JPG,虽然,我最好不要想要。谢谢。 编辑:实际上并没有一个ReadAllBytes方法。 –

+0

是的,有。不在'FileStream'上,而在'File'上。 – Polynomial

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

你可以试试这个....

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