erdomke / RtfPipe

Library for processing rich-text format (RTF) streams
MIT License
121 stars 44 forks source link

System.ArgumentException Message=Parameter is not valid. Source=System.Drawing #66

Closed MartinAmsinck closed 3 years ago

MartinAmsinck commented 3 years ago

Hi i get an exception when trying to run Rtf.ToHtml() with the attached rtf value rtf.txt

 StackTrace:
   at System.Drawing.Bitmap..ctor(Stream stream)

   at RtfPipe.RtfHtmlSettings.DataUri(Picture picture)

   at RtfPipe.Model.HtmlVisitor.Visit(Picture image)

   at RtfPipe.Model.HtmlVisitor.Visit(Element element)

   at RtfPipe.Model.HtmlVisitor.Visit(Element element)

   at RtfPipe.Model.HtmlVisitor.Visit(Element element)

   at RtfPipe.Model.HtmlVisitor.Visit(Element element)

   at RtfPipe.Model.HtmlVisitor.Visit(RtfHtml document)

   at RtfPipe.Rtf.ToHtml(RtfSource source, XmlWriter writer, RtfHtmlSettings settings)

   at RtfPipe.Rtf.ToHtml(RtfSource source, RtfHtmlSettings settings)

I'm running the latest version

MartinAmsinck commented 3 years ago

My bad - fixed with an upgrade to the newest version

MartinAmsinck commented 3 years ago

Found the issue(again), It is an azure issue. There's GDI+ restrictions on Azure.

System.drawing is being blocked by the system. (https://stackoverflow.com/questions/40924122/can-system-drawing-be-used-in-an-azure-web-site)

Any ideas for a workaround here?

janhjordie commented 3 years ago

I have same issue. Only a problem on Azure. As Martin mentions, Azure Web App's, and also Azure Functions, due to security restrictions, don't have access to the kernel GDI. So some functions in System.Drawing is not available.

The bold line contains the issue (RtfHtmlSettings.cs)

if NETFULL

  if (picture.Type is EmfBlip || picture.Type is WmMetafile)
  {
    using (var source = new MemoryStream(picture.Bytes))
    using (var dest = new MemoryStream())
    {
      **var bmp = new System.Drawing.Bitmap(source);**
      bmp.Save(dest, System.Drawing.Imaging.ImageFormat.Png);
      return "data:image/png;base64," + Convert.ToBase64String(dest.ToArray());
    }
  }

endif

erdomke commented 3 years ago

The workaround is to pass in an RtfHtmlSettings object where you provide your own ImageUriGetter callback. In that callback, you can return whatever URL you want for any given picture, perhaps using a different library of your choosing. I will try to document this better soon.