dotnet / dotnet/winforms

System.Drawing.Bitmap.FromStream fails when called with ZipWrappingStream

Open
#8,824 6 comments 0 reactions 1 assignee Claimed by @safern View on GitHub
area-System.Drawing
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

I'm currently extracting images from a Word document. However, Bitmap's `FromStream` function is failing with a "Parameter Not Valid" exception when called with the stream from OpenXML's ImagePart (ZipWrappedStream).

```C#
// ... code the locates the correct blip node

// get the r:embed value (first attribute)
var blipId = imgNode.Attributes[0].Value;

// get the open xml image part
ImagePart img = (ImagePart)wordDoc.MainDocumentPart.GetPartById(blipId);

// get a stream containing the wmf data
// stream is type System.IO.Packaging.ZipWrappingStream
var streamFromOpenXML = img.GetStream();

// this fails with Argument Exception, Parameter Not Valid
var imgToSave = Bitmap.FromStream(streamFromOpenXML);

// however, this works as expected
var mstream = new MemoryStream();
streamFromOpenXML.CopyTo(mstream);
mstream.Seek(0, SeekOrigin.Begin);
var imgToSave = Bitmap.FromStream(mstream);
```

This is using .NET Core 3.1, using the System.Drawing.Common (v 4.7.0) package from NuGet.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.