question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New ImagePart is created as .bin instead of real extension

See original GitHub issue

Describe the bug I have asked the question on Stackoverflow: https://stackoverflow.com/questions/75100804/embedding-image-as-new-imagepart-creates-a-bin-file-using-open-xml-sdk

I don’t expect to get any answer there, and I am also increasingly curious whether, this is an implementation error but an issue in the SDK.

Issue is, I create new embedded image in Excel and it is saved to /xl/media/image.[WhatEverExtensionItIs] If I create a new ImagePart with the SDK and feed it data like this:

ImagePart new_ImagePart = worksheetPart.AddImagePart(ImagePartType.Tiff);
stream.Position = 0;
new_ImagePart.FeedData(stream);

Then the file will be saved as /xl/media/image.bin.

This behaviour it should not do. It should write the extension as .tiff, since this is the ImagePartType, that I specify. Or secondly it should read the stream and identify it as .tiff through i.e. a magic number.

If you unzip the package and change the image extension to real extension, then the image will open perfectly and I have used a TIFF validator to confirm the image is well-formed.

What do you think of this? Am I doing anything wrong in my implementation, or can we change the SDK to write real extension like Excel does?

Screenshots If applicable, add screenshots to help explain your problem.

To Reproduce Steps to reproduce the behavior:

  1. Use the supplied code to stream any TIFF image (or change ImagePartType to any other image type) to a new ImagePart
  2. Unzip the spreadsheet and go to /xl/media/ to find the file as a .bin
  3. Change extension to .tiff and the image will open perfectly

Observed behavior ImagePart creates a new image with .bin extension

Expected behavior ImagePart should create a new image with extension based on ImagePartType.

Desktop (please complete the following information):

  • OS: Windows 11
  • Office version: 2212
  • .NET Target: .Net latest version
  • DocumentFormat.OpenXml Version: 2.19

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
psyvisioncommented, Jul 3, 2023

+1 can confirm the same issue of a .bin in /media/image.bin using v2.20 when saving a word document:

var codes = ImageCodecInfo.GetImageDecoders();
var mimeType = codes.First(c => c.FormatID == image.RawFormat.Guid).MimeType;

var imagePart = mainPart.AddImagePart(mimeType); // image/jpeg

using var stream = new MemoryStream();
image.Save(stream, image.RawFormat);
stream.Position = 0;
imagePart.FeedData(stream);
1reaction
Asbjoedtcommented, Apr 8, 2023

Confirmed fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Embedding Image as new ImagePart creates a .bin file ...
I got all of it working EXCEPT the new file created from the memorystream is saved as image.bin instead of image.tiff even though...
Read more >
[Solved]-Creating an ImagePart isn't saving the Relationship ...
The reason the releationship for your image is not saved is because you do not Dispose() or Close() your word document. So, just...
Read more >
TM4C1294KCPDT: Generated bi file is too large - Arm-based ...
I am generating bin file by using following options in build-> post build steps ... states across resets, customers will leverage the EEPROM...
Read more >
Coloring cells in excel sheet using openXML in C
I have made some changes in Open XML File Format using extension list. I'm able to use files without any issue as the...
Read more >
How to Send Email with Embedded Images Using Java
In this article, I show the basics of using JavaMail, how to send HTML messages, and how to use MIME to create a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found