|
@@ -3761,25 +3761,117 @@ namespace OpenXmlPowerTools
|
|
|
var contentType = imagePart.ContentType;
|
|
|
if (!ImageContentTypes.Contains(contentType))
|
|
|
return null;
|
|
|
-
|
|
|
- using (var partStream = imagePart.GetStream())
|
|
|
- using (var bitmap = new Bitmap(partStream))
|
|
|
+ try {
|
|
|
+ if (contentType == "image/x-wmf")
|
|
|
{
|
|
|
- if (extentCx != null && extentCy != null)
|
|
|
+ return ProcessWmf(imagePart.GetStream(), contentType, element, hyperlinkUri, imageHandler);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ using (var partStream = imagePart.GetStream())
|
|
|
+ using (var bitmap = new Bitmap(partStream))
|
|
|
{
|
|
|
- var imageInfo = new ImageInfo()
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ return imgElement2;
|
|
|
+ }
|
|
|
+
|
|
|
+ var imageInfo2 = 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 imgElement = imageHandler(imageInfo2);
|
|
|
+ if (hyperlinkUri != null)
|
|
|
+ {
|
|
|
+ return new XElement(XhtmlNoNamespace.a,
|
|
|
+ new XAttribute(XhtmlNoNamespace.href, hyperlinkUri),
|
|
|
+ imgElement);
|
|
|
+ }
|
|
|
+ return imgElement;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ Stream stream = imagePart.GetStream();
|
|
|
+ XElement img = null;
|
|
|
+ byte[] bytes = new byte[stream.Length];
|
|
|
+ stream.Read(bytes, 0, bytes.Length);
|
|
|
+ // 设置当前流的位置为流的开始
|
|
|
+ stream.Seek(0, SeekOrigin.Begin);
|
|
|
+ string url = System.Convert.ToBase64String(bytes);
|
|
|
+ img = new XElement(Xhtml.img,
|
|
|
+ new XAttribute(NoNamespace.src, "data:" + contentType + ";base64," + url),
|
|
|
+ new XAttribute(NoNamespace.alt, "wrong image"));
|
|
|
+ return img;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static XElement ProcessWmf(Stream stream,string contentType, XElement element, string hyperlinkUri ,Func<ImageInfo, XElement> imageHandler)
|
|
|
+ {
|
|
|
+
|
|
|
+ XElement img = null;
|
|
|
+ bool compatible = false;
|
|
|
+ bool replaceSymbolFont = false;
|
|
|
+ WMFConverter.Wmf.WmfParser parser = new WMFConverter.Wmf.WmfParser();
|
|
|
+ WMFConverter.Svg.SvgGdi gdi = new WMFConverter.Svg.SvgGdi(compatible);
|
|
|
+ gdi.ReplaceSymbolFont = replaceSymbolFont;
|
|
|
+ parser.Parse(stream, gdi);
|
|
|
+ string mathxml = gdi.Document.InnerXml;
|
|
|
+ using (var partStream = stream)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var imageInfo = new ImageInfo()
|
|
|
+ {
|
|
|
+ ContentType = contentType,
|
|
|
+ DrawingElement = element,
|
|
|
+ AltText = "",
|
|
|
+ Mathxml = mathxml
|
|
|
+ };
|
|
|
+
|
|
|
+ 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);
|
|
|
var imgElement2 = imageHandler(imageInfo);
|
|
|
+ if (widthInPoints != null && heightInPoints != null)
|
|
|
+ {
|
|
|
+ imageInfo.ImgStyleAttribute = new XAttribute("style",
|
|
|
+ string.Format(NumberFormatInfo.InvariantInfo,
|
|
|
+ "width: {0}pt; height: {1}pt ; vertical-align: bottom;", widthInPoints, heightInPoints));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ imageInfo.ImgStyleAttribute = new XAttribute("style",
|
|
|
+ string.Format(NumberFormatInfo.InvariantInfo,
|
|
|
+ "vertical-align: middle;"));
|
|
|
+ }
|
|
|
+
|
|
|
if (hyperlinkUri != null)
|
|
|
{
|
|
|
return new XElement(XhtmlNoNamespace.a,
|
|
@@ -3788,22 +3880,15 @@ namespace OpenXmlPowerTools
|
|
|
}
|
|
|
return imgElement2;
|
|
|
}
|
|
|
-
|
|
|
- var imageInfo2 = new ImageInfo()
|
|
|
+ catch (OutOfMemoryException)
|
|
|
{
|
|
|
- Bitmap = bitmap,
|
|
|
- ContentType = contentType,
|
|
|
- DrawingElement = element,
|
|
|
- AltText = altText,
|
|
|
- };
|
|
|
- var imgElement = imageHandler(imageInfo2);
|
|
|
- if (hyperlinkUri != null)
|
|
|
+ // the Bitmap class can throw OutOfMemoryException, which means the bitmap is messed up, so punt.
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ catch (ArgumentException e)
|
|
|
{
|
|
|
- return new XElement(XhtmlNoNamespace.a,
|
|
|
- new XAttribute(XhtmlNoNamespace.href, hyperlinkUri),
|
|
|
- imgElement);
|
|
|
+ throw new Exception(e.StackTrace);
|
|
|
}
|
|
|
- return imgElement;
|
|
|
}
|
|
|
}
|
|
|
private static XElement ProcessPictureOrObject(WordprocessingDocument wordDoc,
|
|
@@ -3895,22 +3980,7 @@ namespace OpenXmlPowerTools
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static XElement ProcessWmf(ImageInfo imageInfo, string mathxml)
|
|
|
- {
|
|
|
-
|
|
|
- var buffer = Encoding.Default.GetBytes(mathxml);
|
|
|
- string base64 = System.Convert.ToBase64String(buffer);
|
|
|
- string imageSource =
|
|
|
- string.Format("data:{0};base64,{1}", "image/svg+xml", base64);
|
|
|
- XElement img = new XElement(Xhtml.img,
|
|
|
- new XAttribute(NoNamespace.src, imageSource),
|
|
|
- imageInfo.ImgStyleAttribute= new XAttribute("style",
|
|
|
- string.Format(NumberFormatInfo.InvariantInfo,
|
|
|
- "vertical-align: middle;")),
|
|
|
- imageInfo.AltText != null ?
|
|
|
- new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
|
|
|
- return img;
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
private static XElement ProcessObject(WordprocessingDocument wordDoc,
|
|
|
XElement element, Func<ImageInfo, XElement> imageHandler)
|