PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Nov 01, 2024 12:35 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun May 23, 2021 5:15 pm 
Offline

Joined: Sun May 23, 2021 11:18 am
Posts: 2
God afternoon to all,
this is my first post here. I've posted the same question also in StackOverflow but I think that it is more pertinent here.

In a my VB.net 2010 Win Form Application, I create an array of strings referred to the file names of some images (jpg, png).
From that array I create a pdf with a For ... Next loop
Here is my test code. The array contains two filenames, they are enough

Code:
         'TWO images.jpg  PathName in array
        Dim fArray(1) As String
        fArray(0) = "C:\Users\Administrator\Desktop\myTestInp\195202_000.jpg"
        fArray(1) = "C:\Users\Administrator\Desktop\myTestInp\195202_001.jpg"
     
       'Define Pdf
       Dim pdf As New PdfDocument

 'Loop the fArray
        For i As Integer = 0 To 1

            'CREATE BitmapInp from fArray(i)
            Using BitmapInp As Bitmap = New Bitmap(fArray(i))
                'From BitmapInp:
                'DRAW a BitmapOut and Save it using as filename "C:\Users\Administrator\Desktop\myTestOut\xxxx.jpg" at any step.
                'I do not use a variable in Save for emphasize my question.
                'NOTE: the overwriting does not raise errors.
                Using BitmapOut As New Bitmap(BitmapInp.Width, BitmapInp.Height)
                    Using grBitmapOut = Graphics.FromImage(BitmapOut)
                        grBitmapOut.DrawImage(BitmapInp, 0, 0, BitmapInp.Width, BitmapInp.Height)
                    End Using
                    'Do other stuff on BitmapOut and then ...
                    BitmapOut.Save("C:\Users\Administrator\Desktop\myTestOut\xxxx.jpg", ImageFormat.Jpeg)
                End Using
            End Using

            'LOAD BitmapOut as xImg from "C:\Users\Administrator\Desktop\myTestOut\xxxx.jpg"
            Using xImg = XImage.FromFile("C:\Users\Administrator\Desktop\myTestOut\xxxx.jpg")
                Dim page = pdf.AddPage()
                Using grPdf = PdfSharp.Drawing.XGraphics.FromPdfPage(page)
                    page.Width = xImg.PixelWidth * 72 / xImg.HorizontalResolution
                    page.Height = xImg.PixelHeight * 72 / xImg.HorizontalResolution
                    xImg.Interpolate = False
                    grPdf.DrawImage(xImg, 0, 0, page.Width, page.Height)
                End Using
            End Using

        Next
        'END of loop and SAVE(pdf)
        pdf.Save("C:\Users\Administrator\Desktop\myTestOut\Dummy.Pdf")


Looking and inspecting the outputs, I note that:
1) xxxx.jpg "contains" correctly the second image;
2) Pdf contains (obviously) two pages but, this is the Oddity, they are the same as the first image in input!

I do not understand this behaviour ... mumble ...
Why and how can, in the above situation, Ximage.FromFile to get always the first saved BitmapOut?
Thank for any suggestion.
Paolo

PS.
I know that the question resolves easily using at each step a new filename for the output bitmap and using that name for creating xImg. Equivalent is the use of a memory stream (instead of C:\Users\Administrator\... etc, to be defined at each loop.
But I'm curious to know :D


Top
 Profile  
Reply with quote  
PostPosted: Mon May 24, 2021 4:40 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1000
Location: CCAA
Hi!
I didn't check the source code of PDFsharp/MigraDoc, but maybe the filename is used as a "unique key" to prevent incorporating multiple copies of the same image into one PDF file.
If the same image is used on multiple pages, only one copy of the image data is included in the file.
PDFsharp tries to prevent multiple copies - and IIRC the filename is the key.

And yes, using different file names should resolve the issue.

The link to SO:
https://stackoverflow.com/a/67675871/162529

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


Top
 Profile  
Reply with quote  
PostPosted: Mon May 24, 2021 6:49 pm 
Offline

Joined: Sun May 23, 2021 11:18 am
Posts: 2
Hi Thomas,
your answer explains perfectly my issue. Thank you so much :)


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 29 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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group