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
![Very Happy :D](./images/smilies/icon_biggrin.gif)