我試圖從內存流中加載圖像(pdf和Word文檔),所以我可以在它們添加到pdf之前對它們進行操作。無論何時我嘗試加載位圖或GIF,我都會遇到可怕的GDI +錯誤之一。從MemoryStream加載位圖時出錯
這個調用...
System.Drawing.Bitmap myImage = System.Drawing.Image.FromStream(docStream);
生成此錯誤...
System.Runtime.InteropServices.ExternalException occurred
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
這個電話...
System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(docStream);
生成此錯誤...
System.ArgumentException occurred
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Bitmap..ctor(Stream stream)
該代碼適用於png,jpg和tif,但不適用於bmp或GIF。
它基本上是:
MemoryStream docStream = GetMemoryStream(filePath);
// The next line errors...
System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(docStream);
private MemoryStream GetMemoryStream(string file)
{
FileStream fs = null;
Byte[] bytes;
int len;
fs = File.OpenRead(file);
len = (int)fs.Length - 1;
bytes = new Byte[len];
fs.Read(bytes, 0, len);
return new MemoryStream(bytes);
}
我沒有在任何地方關閉的MemoryStream併除非PNG和JPG格式不會受到closed MemoryStream我不認爲這是個問題。當我使用十六進制編輯器查看圖像文件時,它看起來不是encoding不正確。以防萬一,我也嘗試了不同的文件大小。
在這一點上,我幾乎不知所措。任何幫助將不勝感激。
謝謝。
這是我通過轉換器運行的VB代碼。 :(笨,謝謝。 – 2010-10-12 23:49:24