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

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)
1个回答

0
淘气完成!我通过使用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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接