我的流不断抛出读/写超时异常

时间:2014-10-10 13:14:24

标签: c# stream openxml openxml-sdk

我正在使用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演示文稿上运行。我想知道它是否已压缩(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)

1 个答案:

答案 0 :(得分:0)

Mischief管理好了!我能够通过使用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)