开发者

Writing Bitmap to OpenXML ImagePart via MemoryStream

开发者 https://www.devze.com 2023-03-21 23:30 出处:网络
I have an image stored in a Bitmap object that I\'d like to stick into an OpenXML document.I\'ve tried using a MemoryStream as an intermediate step as follows:

I have an image stored in a Bitmap object that I'd like to stick into an OpenXML document. I've tried using a MemoryStream as an intermediate step as follows:

ImagePart part = container.AddNewPart<ImagePart>("image/jpeg", imageId);
using (MemoryStream ms = new MemoryStream())
{
    bitmap.Save(ms, ImageFormat.Jpeg);
    part.FeedData(ms);
}

but that always results in empty files in the media folder and PowerPoint displaying an error instead of the images. I know that the MemoryStream has the image data correctly as I've written it out to a file without issue. When I try to load an image from a FileStre开发者_高级运维am it works just fine.

How can I get this Bitmap into an OpenXML document?


I was almost there, I just needed to reset the MemoryStream's position to the beginning after saving the Bitmap to it.

ms.Position = 0;

That line should be added between the Save and FeedData calls.

0

精彩评论

暂无评论...
验证码 换一张
取 消