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

Oddity? using XImage.FromFile("x.jpg") in a loop
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4255
Page 1 of 1

Author:  ilgk48 [ Sun May 23, 2021 5:15 pm ]
Post subject:  Oddity? using XImage.FromFile("x.jpg") in a loop

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

Author:  TH-Soft [ Mon May 24, 2021 4:40 pm ]
Post subject:  Re: Oddity? using XImage.FromFile("x.jpg") in a loop

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

Author:  ilgk48 [ Mon May 24, 2021 6:49 pm ]
Post subject:  Re: Oddity? using XImage.FromFile("x.jpg") in a loop

Hi Thomas,
your answer explains perfectly my issue. Thank you so much :)

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