使用aspose,我已經將pdf文檔的第一頁轉換爲jpeg(用作'文檔'部分中的縮略圖,指向我的一個asp.net頁)。到目前爲止,這是存儲在FileStream中 - 但我需要一個字節數組來分配給Image控件的數據值。任何人都可以指出我正確的方向來轉換?我有一個很好的看看,我找不到解決方案。FileStream(pdf轉換器中的jpeg)到Byte []
非常感謝。
使用aspose,我已經將pdf文檔的第一頁轉換爲jpeg(用作'文檔'部分中的縮略圖,指向我的一個asp.net頁)。到目前爲止,這是存儲在FileStream中 - 但我需要一個字節數組來分配給Image控件的數據值。任何人都可以指出我正確的方向來轉換?我有一個很好的看看,我找不到解決方案。FileStream(pdf轉換器中的jpeg)到Byte []
非常感謝。
這應該工作:
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;
}
我不是實際保存JPG,雖然,我最好不要想要。謝謝。 編輯:實際上並沒有一個ReadAllBytes方法。 –
是的,有。不在'FileStream'上,而在'File'上。 – Polynomial