PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Trouble using XGraphics.DrawImage
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4083
Page 1 of 1

Author:  CCabarcos [ Thu Jan 16, 2020 4:49 pm ]
Post subject:  Trouble using XGraphics.DrawImage

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.

Author:  Thomas Hoevel [ Thu Jan 16, 2020 4:57 pm ]
Post subject:  Re: Trouble using XGraphics.DrawImage

CCabarcos wrote:
Hope someone might have an idea of what's going on.
Can you replicate the exception with the official library and the IssueSubmissionTemplate?
I have no idea which port you are using and which features might work differently with this port.

Author:  CCabarcos [ Thu Jan 16, 2020 5:53 pm ]
Post subject:  Re: Trouble using XGraphics.DrawImage

Thomas Hoevel wrote:
CCabarcos wrote:
Hope someone might have an idea of what's going on.
Can you replicate the exception with the official library and the IssueSubmissionTemplate?
I have no idea which port you are using and which features might work differently with this port.


Is there a .dll version I could download from somewhere? I need to integrate it into Unity, so I'm not really sure what to do with what I found available for download (the whole solution).

In case you heard about them, I've tried 2 different plugins:

- UPDF -> https://assetstore.unity.com/packages/t ... pdf-135573
- ZetPDF -> https://zetpdf.com/

Both fail in apparently the same way, the error using ZetPDF, was 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.GdipSaveImageToStream(System.Runtime.InteropServices.HandleRef,System.Runtime.InteropServices.ComTypes.IStream,System.Guid&,System.Runtime.InteropServices.HandleRef)
at System.Drawing.Image.Save (System.IO.Stream stream, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters encoderParams) [0x0007e] in <ab9e008c8c264c02bf011ee5162d1a92>:0
at System.Drawing.Image.Save (System.IO.Stream stream, System.Drawing.Imaging.ImageFormat format) [0x00029] in <ab9e008c8c264c02bf011ee5162d1a92>:0
at (wrapper remoting-invoke-with-check) System.Drawing.Image.Save(System.IO.Stream,System.Drawing.Imaging.ImageFormat)
at ZetPDF.Pdf.Advanced.PdfImage.InitializeJpeg () [0x0001b] in <4062b2ef83e042d387f35ff3cb9b2074>:0
at ZetPDF.Pdf.Advanced.PdfImage..ctor (ZetPDF.Pdf.PdfDocument document, ZetPDF.Drawing.XImage image) [0x000a9] in <4062b2ef83e042d387f35ff3cb9b2074>:0
at ZetPDF.Pdf.Advanced.PdfImageTable.GetImage (ZetPDF.Drawing.XImage image) [0x00028] in <4062b2ef83e042d387f35ff3cb9b2074>:0
at ZetPDF.Pdf.PdfPage.GetImageName (ZetPDF.Drawing.XImage image) [0x0000b] in <4062b2ef83e042d387f35ff3cb9b2074>:0
at ZetPDF.Drawing.Pdf.XGraphicsPdfRenderer.GetImageName (ZetPDF.Drawing.XImage image) [0x00008] in <4062b2ef83e042d387f35ff3cb9b2074>:0
at ZetPDF.Drawing.Pdf.XGraphicsPdfRenderer.Realize (ZetPDF.Drawing.XImage image) [0x00029] in <4062b2ef83e042d387f35ff3cb9b2074>:0
at ZetPDF.Drawing.Pdf.XGraphicsPdfRenderer.DrawImage (ZetPDF.Drawing.XImage image, System.Double x, System.Double y, System.Double width, System.Double height) [0x00000] in <4062b2ef83e042d387f35ff3cb9b2074>:0
at ZetPDF.Drawing.XGraphics.DrawImage (ZetPDF.Drawing.XImage image, System.Double x, System.Double y, System.Double width, System.Double height) [0x000e0] in <4062b2ef83e042d387f35ff3cb9b2074>:0
at ZetPDF.Drawing.XGraphics.DrawImage (ZetPDF.Drawing.XImage image, ZetPDF.Drawing.XRect rect) [0x0001e] in <4062b2ef83e042d387f35ff3cb9b2074>:0
at Test+<TestFn>d__4.MoveNext () [0x00168] in <cb1e7d17c38f40ae89435e4508118a7f>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00027] in <52d7b92b1d0e4868ac4c518247d361f7>:0

(Filename: <ab9e008c8c264c02bf011ee5162d1a92> Line: 0)


Thanks for your fast reply.

Author:  TH-Soft [ Thu Jan 16, 2020 6:43 pm ]
Post subject:  Re: Trouble using XGraphics.DrawImage

CCabarcos wrote:
Is there a .dll version I could download from somewhere?
See here:
https://www.nuget.org/profiles/PDFsharp-Team

NuGet packages are actually ZIP files with .DLLs inside.

Author:  CCabarcos [ Thu Jan 16, 2020 11:08 pm ]
Post subject:  Re: Trouble using XGraphics.DrawImage

TH-Soft wrote:
CCabarcos wrote:
Is there a .dll version I could download from somewhere?
See here:
https://www.nuget.org/profiles/PDFsharp-Team

NuGet packages are actually ZIP files with .DLLs inside.


Thank you very much for your fast reply once more. I now downloaded the .dlls from there, specifically, the lastest version: 1.51.5185-beta.

I'm not using the IssueSubmissionTemplates because I'm working on a Unity project, not a straight .net solution, so I don't think it would apply properly.

I created a simple Unity project just to test this problem with a single script, containing the following code:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;
using System.IO;
using PdfSharp.Drawing;
using PdfSharp.Pdf;


public class Test : MonoBehaviour
{
    bool _bExportingPDF = false;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            _bExportingPDF = true;
        }
    }

    //Gets called when everything in the frame has been set, so the screenshot is captured properly
    private void LateUpdate()
    {
        if (_bExportingPDF)
        {
            StartCoroutine(ExportPDF());
        }
    }

    IEnumerator ExportPDF()
    {
        yield return new WaitForEndOfFrame();

        _bExportingPDF = false;

        //Capture screenshot
        Texture2D screenshotTexture = ScreenCapture.CaptureScreenshotAsTexture(2);
        MemoryStream dataStream = new MemoryStream(screenshotTexture.EncodeToJPG());

        //Create PDF
        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);

        XImage tempImage = XImage.FromStream(dataStream);

        //Mantain image proportions
        double margin = page.Width / 10.0;
        double originalWidth = screenshotTexture.width;
        double originalHeight = screenshotTexture.height;
        double newWidth = page.Width.Value - (margin * 2.0);
        double newHeight = (newWidth / originalWidth) * originalHeight;
        XUnit newHeightUnit = new XUnit(newHeight);

        gfx.DrawImage(tempImage, new XRect(margin, textHeight, newWidth, newHeightUnit));

        string year = System.DateTime.Now.Year.ToString();
        string month = System.DateTime.Now.Month.ToString();
        string day = System.DateTime.Now.Day.ToString();
        string hour = System.DateTime.Now.Hour.ToString();
        string minute = System.DateTime.Now.Minute.ToString();
        string second = System.DateTime.Now.Second.ToString();
        string fullDate = year + "_" + month + "_" + day + "_" + hour + "_" + minute + "_" + second;
        string filename = fullDate + "_Export.pdf";

        document.Save(filename);
        Process.Start(filename);

        Destroy(screenshotTexture);
    }
}


And I'm getting this error:

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 <bd3f3af751f04f23ad39a8c42de34910>:0
at PdfSharp.Drawing.XImage.FromStream (System.IO.Stream stream) [0x0001e] in <bd3f3af751f04f23ad39a8c42de34910>:0
at Test+<ExportPDF>d__3.MoveNext () [0x000d8] in <998cadfa43ed424eb26496e775e099c0>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00027] in <52d7b92b1d0e4868ac4c518247d361f7>:0

(Filename: <ab9e008c8c264c02bf011ee5162d1a92> Line: 0)


It still seems XGraphics.DrawImage is what doesn't work properly, and once more, while in the Unity editor, everything works fine, it's only once I compile a build that I get the error.

Hope this might shed some more light on it... I'm still as lost as I initially was :S.

Author:  TH-Soft [ Fri Jan 17, 2020 7:15 am ]
Post subject:  Re: Trouble using XGraphics.DrawImage

CCabarcos wrote:
I'm not using the IssueSubmissionTemplates because I'm working on a Unity project, not a straight .net solution, so I don't think it would apply properly.
Your choice. We're working on a straight .NET library and will not investigate problems we cannot replicate.

Author:  CCabarcos [ Fri Jan 17, 2020 8:21 am ]
Post subject:  Re: Trouble using XGraphics.DrawImage

TH-Soft wrote:
CCabarcos wrote:
I'm not using the IssueSubmissionTemplates because I'm working on a Unity project, not a straight .net solution, so I don't think it would apply properly.
Your choice. We're working on a straight .NET library and will not investigate problems we cannot replicate.


I think you missunderstood me. I don't mind using the tools you kindly provide, of course, I'm just not sure about how I could provide a template that actually shows the problem.

Would a link to the project itself be ok? or what approach should I take?

Thank you.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/