通过 MemoryStream 将位图写入 OpenXML ImagePart
我有一个存储在 Bitmap 对象中的图像,我想将其粘贴到 OpenXML 文档中。我尝试使用 MemoryStream 作为中间步骤,如下所示:
ImagePart part = container.AddNewPart<ImagePart>("image/jpeg", imageId);
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Jpeg);
part.FeedData(ms);
}
但这总是会导致媒体文件夹中出现空文件,并且 PowerPoint 显示错误而不是图像。我知道 MemoryStream 具有正确的图像数据,因为我已将其毫无问题地写入文件。当我尝试从 FileStream 加载图像时,它工作得很好。
如何将此位图放入 OpenXML 文档中?
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 FileStream it works just fine.
How can I get this Bitmap into an OpenXML document?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我就快到了,我只需要在将位图保存到 MemoryStream 后将 MemoryStream 的位置重置到开头即可。
该行应添加在
Save
和FeedData
调用之间。I was almost there, I just needed to reset the MemoryStream's position to the beginning after saving the Bitmap to it.
That line should be added between the
Save
andFeedData
calls.