Skip to content

Return raw image bytes for non-windows platforms in the ImageHandler #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: vNext
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 64 additions & 40 deletions OpenXmlPowerTools/WmlToHtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
Expand Down Expand Up @@ -118,16 +119,33 @@ public static XElement ConvertToHtml(WordprocessingDocument wDoc, HtmlConverterS

[SuppressMessage("ReSharper", "NotAccessedField.Global")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public class ImageInfo
public class ImageInfo : IDisposable
{
public Bitmap Bitmap;
public ImageInfo()
{
m_bitmap = new Lazy<Bitmap>(() => new Bitmap(new MemoryStream(ImageBytes)));
}

private readonly Lazy<Bitmap> m_bitmap;
public Bitmap Bitmap => m_bitmap.Value;

public byte[] ImageBytes;

public XAttribute ImgStyleAttribute;
public string ContentType;
public XElement DrawingElement;
public string AltText;

public const int EmusPerInch = 914400;
public const int EmusPerCm = 360000;

public void Dispose()
{
if (m_bitmap.IsValueCreated)
{
m_bitmap.Value.Dispose();
}
}
}

public static class WmlToHtmlConverter
Expand Down Expand Up @@ -3078,48 +3096,54 @@ private static XElement ProcessDrawing(WordprocessingDocument wordDoc,
if (!ImageContentTypes.Contains(contentType))
return null;


var memoryStream = new MemoryStream();
using (var partStream = imagePart.GetStream())
using (var bitmap = new Bitmap(partStream))
{
partStream.CopyTo(memoryStream);
var imageBytes = memoryStream.ToArray();

if (extentCx != null && extentCy != null)
{
var imageInfo = new ImageInfo()
{
Bitmap = bitmap,
ImgStyleAttribute = new XAttribute("style",
string.Format(NumberFormatInfo.InvariantInfo,
"width: {0}in; height: {1}in",
(float)extentCx / (float)ImageInfo.EmusPerInch,
(float)extentCy / (float)ImageInfo.EmusPerInch)),
ContentType = contentType,
DrawingElement = element,
AltText = altText,
};
var imgElement2 = imageHandler(imageInfo);
if (hyperlinkUri != null)
{
return new XElement(XhtmlNoNamespace.a,
new XAttribute(XhtmlNoNamespace.href, hyperlinkUri),
imgElement2);
using (var imageInfo = new ImageInfo
{
ImageBytes = imageBytes,
ImgStyleAttribute = new XAttribute("style",
string.Format(NumberFormatInfo.InvariantInfo,
"width: {0}in; height: {1}in",
(float)extentCx / (float)ImageInfo.EmusPerInch,
(float)extentCy / (float)ImageInfo.EmusPerInch)),
ContentType = contentType,
DrawingElement = element,
AltText = altText,
})
{
var imgElement2 = imageHandler(imageInfo);
if (hyperlinkUri != null)
{
return new XElement(XhtmlNoNamespace.a, new XAttribute(XhtmlNoNamespace.href, hyperlinkUri), imgElement2);
}
return imgElement2;
}
return imgElement2;
}

var imageInfo2 = new ImageInfo()
using (var imageInfo2 = new ImageInfo()
{
Bitmap = bitmap,
ImageBytes = imageBytes,
ContentType = contentType,
DrawingElement = element,
AltText = altText,
};
var imgElement = imageHandler(imageInfo2);
if (hyperlinkUri != null)
})
{
return new XElement(XhtmlNoNamespace.a,
new XAttribute(XhtmlNoNamespace.href, hyperlinkUri),
imgElement);
var imgElement = imageHandler(imageInfo2);
if (hyperlinkUri != null)
{
return new XElement(XhtmlNoNamespace.a,
new XAttribute(XhtmlNoNamespace.href, hyperlinkUri),
imgElement);
}
return imgElement;
}
return imgElement;
}
}

Expand All @@ -3145,18 +3169,18 @@ private static XElement ProcessPictureOrObject(WordprocessingDocument wordDoc,
{
try
{
using (var bitmap = new Bitmap(partStream))
var memoryStream = new MemoryStream();
partStream.CopyTo(memoryStream);
using (var imageInfo = new ImageInfo()
{
ImageBytes = memoryStream.ToArray(),
ContentType = contentType,
DrawingElement = element
})
{
var imageInfo = new ImageInfo()
{
Bitmap = bitmap,
ContentType = contentType,
DrawingElement = element
};

var style = (string)element.Elements(VML.shape).Attributes("style").FirstOrDefault();
if (style == null) return imageHandler(imageInfo);

var tokens = style.Split(';');
var widthInPoints = WidthInPoints(tokens);
var heightInPoints = HeightInPoints(tokens);
Expand Down