Reference to type 'Bitmap' claims it is defined in 'System.Drawing', but it could not be found

I am trying to convert DOCX file to HTML using Open XML power tools in .Net Core Project. Everything was smooth until i encounter images in the document file. I tried the below code for exporting the images using open XML power tools

using (WordprocessingDocument wDoc = 
   WordprocessingDocument.Open(memoryStream, true))
{

WmlToHtmlConverterSettings settings = new WmlToHtmlConverterSettings()
{

    ImageHandler = imageInfo =>
    {

        imageInfo.Bitmap.Save([...]);

        ImageFormat format = imageInfo.Bitmap.RawFormat;

    }
}

XElement htmlElement = WmlToHtmlConverter.ConvertToHtml(wDoc, settings);

}

when I use imageInfo.Bitmap.Save(ms, imageFormat);' in the ImageHandler. I did try to add the System.Drawing.Common package from nuget, but that did not resolve the error.

Upvotes: 4

Views: 881

Answers (1)

stefan.seeland
stefan.seeland

Reputation: 3280

Your stripped down sample should work. You maybe watch an issue of resolving system.drawing.common. Check your build output, mine shows

warning NU1701: Package 'OpenXmlPowerTools 4.5.3.2' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.

This code works with .net core 3.1 on windows and its behavior will differ depending on your used target/os:

      private static void Main(string[] args)
        {
            byte[] byteArray = File.ReadAllBytes(args[0]);
            using MemoryStream memoryStream = new MemoryStream();
            memoryStream.Write(byteArray, 0, byteArray.Length);
            using WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true);
            HtmlConverterSettings htmlConverterSettings = new HtmlConverterSettings
            {
                ImageHandler = ImageHandler
            };

            WmlToHtmlConverterSettings settings = new WmlToHtmlConverterSettings(htmlConverterSettings);
            XElement htmlElement = WmlToHtmlConverter.ConvertToHtml(wDoc, settings);
            XDocument html = new XDocument(new XDocumentType("html", null, null, null), htmlElement);
            string htmlString = html.ToString(SaveOptions.DisableFormatting);
            File.WriteAllText(args[1], htmlString, Encoding.UTF8);
        }

        private static XElement ImageHandler(ImageInfo imageInfo)
        {
            using MemoryStream memoryStream = new MemoryStream();
            imageInfo.Bitmap.Save(memoryStream, imageInfo.Bitmap.RawFormat);
            string base64 = Convert.ToBase64String(memoryStream.ToArray());

            ImageFormat format = imageInfo.Bitmap.RawFormat;
            ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders().First(imageCodecInfo => imageCodecInfo.FormatID == format.Guid);
            string mimeType = codec.MimeType;

            string imageSource = $"data:{mimeType};base64,{base64}";

            return new XElement(Xhtml.img, new XAttribute(NoNamespace.src, imageSource), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
        }

Think about switching to some fork of OpenXmlPowerTools that supports .net > 4.x and OS beside of Windows.

Upvotes: -1

Related Questions