PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Jun 27, 2017 8:43 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Mar 01, 2017 5:36 pm 
Offline

Joined: Wed Mar 01, 2017 4:14 pm
Posts: 4
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 02, 2017 9:09 pm 
Offline

Joined: Wed Mar 01, 2017 4:14 pm
Posts: 4
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!


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 04, 2017 12:20 pm 
Offline
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 241
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 06, 2017 4:09 pm 
Offline

Joined: Wed Mar 01, 2017 4:14 pm
Posts: 4
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!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group