2014-10-10 70 views
3

我正在使用Open Office SDK 2.0解析PowerPoint演示文稿。在程序中的某一點,我將一個流傳遞給一個將返回圖像的MD5的方法。然而,在它甚至到達我的MD5方法之前,流中似乎存在問題。我的流不斷拋出讀/寫超時異常

這裏是我的代碼:

// Get image information here. 
var blipRelId = blip.Embed; 
var imagePart = (ImagePart)slidePart.GetPartById(blipRelId); 
var imageFileName = imagePart.Uri.OriginalString; 
var imageStream = imagePart.GetStream(); 
var imageMd5 = Hasher.CalculateStreamHash(imageStream); 

在調試之前,我讓把它放到Hasher.CalculateStreamHash,我檢查的ImageStream屬性。隨即,我看到ReadTimeout和WriteTimeout都有類似的錯誤:

imageStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException 
imageStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException 

下面是我「米調試過程中看到的屬性的圖片,在情況下,它可以幫助: enter image description here

此代碼運行我想知道是否它的壓縮(PowerPoint演示文稿基本上只是一個壓縮文件)的事實是我看到這些超時錯誤的原因嗎?

更新:我試圖採取流,獲取圖像並將其轉換爲字節數組並將其發送給MD5方法作爲內存流,但我仍然在流的讀/寫超時屬性中出現相同的錯誤。下面的代碼,因爲它是現在:

// Get image information here. 
var blipRelId = blip.Embed; 
var imagePart = (ImagePart)slidePart.GetPartById(blipRelId); 
var imageFileName = imagePart.Uri.OriginalString; 
var imageStream = imagePart.GetStream(); 

// Convert image to memory stream 
var img = Image.FromStream(imageStream); 
var imageMemoryStream = new MemoryStream(this.imageToByteArray(img)); 
var imageMd5 = Hasher.CalculateStreamHash(imageMemoryStream); 

爲了清楚起見,這裏的簽名爲CalculateStreamHash方法:

public static string CalculateStreamHash([NotNull] Stream stream) 

回答

0

惡作劇完畢!我可以通過使用BufferedStream並添加重載的方法來接受了一個BufferedStream作爲參數,我的MD5的方法來解決這個問題:

// Get image information here. 
var blipRelId = blip.Embed; 
var imagePart = (ImagePart)slidePart.GetPartById(blipRelId); 
var imageFileName = imagePart.Uri.OriginalString; 

// Convert image to buffered stream 
var imageBufferedStream = new BufferedStream(imagePart.GetStream()); 
var imageMd5 = Hasher.CalculateStreamHash(imageBufferedStream); 

...和:

public static string CalculateStreamHash([NotNull] BufferedStream bufferedStream)