Hi there,
I'm working on a Unity / C# project, using a plugin that's based on PDFsharp. I came upon a problem that I haven't been able to solve so far.
I create the pdf doc and try to add a text and image to it, the text works fine, but the image does not work.
Code:
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
double textHeight = page.Height.Value / 8.0f;
XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
gfx.DrawString("This is a sample exported image.", font, XBrushes.Black, new XRect(0, 0, page.Width, textHeight), XStringFormats.Center);
Up to here, everything works fine, but as soon as I add an image too...
I create an XImage using either one of these (tried both methods in case the problem was the XImage):
Code:
XImage tempImage = XImageFromStream(myDataStream);
XImage tempImage = XImage.FromFile("C:/MyImage.jpg");
And as soon as I call DrawImage, I get and error:
Code:
gfx.DrawImage(tempImage, new XRect(.....));
What's pretty weird is that while I'm working on the Unity Editor, everything works just fine, the problem only appears when I try to run this code on a Windows Build.
The error is the following:
Quote:
NullReferenceException: Object reference not set to an instance of an object
at System.Drawing.ComIStreamMarshaler+ManagedToNativeWrapper..cctor () [0x00049] in <ab9e008c8c264c02bf011ee5162d1a92>:0
Rethrow as TypeInitializationException: The type initializer for 'ManagedToNativeWrapper' threw an exception.
at System.Drawing.ComIStreamMarshaler.MarshalManagedToNative (System.Object managedObj) [0x00000] in <ab9e008c8c264c02bf011ee5162d1a92>:0
at (wrapper managed-to-native) System.Drawing.GDIPlus.GdipLoadImageFromStream(System.Runtime.InteropServices.ComTypes.IStream,intptr&)
at System.Drawing.Image.InitFromStream (System.IO.Stream stream) [0x000c7] in <ab9e008c8c264c02bf011ee5162d1a92>:0
at System.Drawing.Image.LoadFromStream (System.IO.Stream stream, System.Boolean keepAlive) [0x00011] in <ab9e008c8c264c02bf011ee5162d1a92>:0
at System.Drawing.Image.FromStream (System.IO.Stream stream) [0x00000] in <ab9e008c8c264c02bf011ee5162d1a92>:0
at PdfSharp.Drawing.XImage..ctor (System.IO.Stream stream) [0x00034] in <166a6cb8bd8b4af8a1696a1823820029>:0
at PdfSharp.Drawing.XImage.FromStream (System.IO.Stream stream) [0x0001e] in <166a6cb8bd8b4af8a1696a1823820029>:0
at GeneralManager+<ExportPDF>d__27.MoveNext () [0x000d8] in <8bf44821173f4409bb33250b70b09848>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00027] in <52d7b92b1d0e4868ac4c518247d361f7>:0
(Filename: <ab9e008c8c264c02bf011ee5162d1a92> Line: 0)
Hope someone might have an idea of what's going on.
Thank you.