PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Issue drawing xImage when the image is a resource file
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3548
Page 1 of 1

Author:  hchurch [ Wed Mar 01, 2017 5:36 pm ]
Post subject:  Issue drawing xImage when the image is a resource file

Hi,

After switching over to PdfSharp version 1.5 beta, I started having an issue when trying to draw a .png image that is a project resource.
The project is in vb.net.

Code:
Dim logoimage As XImage = XImage.FromGdiPlusImage(My.Resources.mylogo)
gfx.DrawImage(logoimage, 60, 37, 40, 19)



I saw the comments about some people using Image.FromStream(memory) instead but was unable to get the memory stream working.
Any help would be much appreciated. I just need to know how to reference the resource file via a stream.

Thanks,
Heath

Author:  hchurch [ Thu Mar 02, 2017 9:09 pm ]
Post subject:  Re: Issue drawing xImage when the image is a resource file

Hi All,

I managed to figure out the issue. In order to load a resource file/image into an xImage, you must first save the resource image as a temporary file and THEN use the new temporary file's path as the stream of the xImage.

See example below:
Code:
            Dim logopath As String = Path.GetTempPath()
           'Determines what the path to the temp folder is and what the path to the image is (if it actually exists - see next step).
            logopath = logopath & "mylogo.png"
           'Checks to see if the file already exists in the Temp folder, if not, then it will create it.
            If File.Exists(logopath) = False Then
                My.Resources.mylogo.Save(logopath)
            End If
            'Creates the xImage and sets the image's source to be the logo image that we just saved to the Temp folder
            Dim logoimage As XImage = XImage.FromFile(logopath)
            gfx.DrawImage(logoimage, 60, 37, 40, 19)


Hope this helps any of my friends out there coding in vb.net trying to print resource files/images onto a pdf!

Author:  TH-Soft [ Sat Mar 04, 2017 12:20 pm ]
Post subject:  Re: Issue drawing xImage when the image is a resource file

Hi!
hchurch wrote:
In order to load a resource file/image into an xImage, you must first save the resource image as a temporary file and THEN use the new temporary file's path as the stream of the xImage.
I haven't tried it yet, but I bet there is a simpler way.

You should be able to read any resource as a stream and get the XImage directly from that stream.
I don't know whether you have to change the compile type of the image resource (to e.g. Embedded Resource).
You can use a tool like DotNetPeek to see the names of the resources in your assembly.

You can use a MemoryStream to avoid creating a temporary file if you stick to your current approach.

Author:  hchurch [ Mon Mar 06, 2017 4:09 pm ]
Post subject:  Re: Issue drawing xImage when the image is a resource file

I've looked around quite a bit and I can't find any other PDFsharp users here or on Stack Overflow that have used a memory stream to load images onto the PDF in vb.net. Can you just give me an example of what this would look like code-wise?

Thanks, Thomas!

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