【问题标题】:My stream keeps throwing Read/Write Timeout exceptions我的流不断抛出读/写超时异常
【发布时间】:2014-10-10 13:14:24
【问题描述】:

我正在使用 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

这是我在调试期间看到的属性图片,以防万一:

此代码在 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)

【问题讨论】:

    标签: c# stream openxml openxml-sdk


    【解决方案1】:

    恶作剧成功了!我能够通过使用 BufferedStream 并向我的 MD5 方法添加一个接受 BufferedStream 作为参数的重载方法来克服这个问题:

    // 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 2011-04-11
      • 1970-01-01
      相关资源
      最近更新 更多