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

Forcing a image to fit A4 size
https://forum.pdfsharp.net/viewtopic.php?f=2&t=765
Page 1 of 1

Author:  Patroni [ Wed Jun 17, 2009 2:13 pm ]
Post subject:  Forcing a image to fit A4 size

Hey guys.

So I have this problem. I made a method that takes an image as a stream and then makes a pdf with the image.
This image comes from a scanner. When I scann the document he is beeing resize to 1700x2347 and i don't know why. This is
a problem because when I pass the image to the pdf page it doesn't fit because A4 page as something as 1248x1760...
Is there a way that I can force the size of the image, something like myimage.weight and myimage.width?

My code is this

Code:
Public Function SetImage(ByVal img As Byte(), ByVal path As String, ByVal nome1 As String) As Boolean

        If img IsNot Nothing Then
            Try
                ''''criar o pdf
                Dim myPDF As PdfDocument
                myPDF = New PdfDocument()
                ''''
                Dim newImage As System.Drawing.Image

                Using ms = New MemoryStream(img, 0, img.Length)
                    ms.Write(img, 0, img.Length)

                    newImage = System.Drawing.Image.FromStream(ms, True)
                    newImage.Save(path & Guid.NewGuid().ToString & ".jpg")

                    'newImage.Save(path & "teste.jpg")

                    ''''passar imagem para pdf
                    Dim xgr As XGraphics
                    myPDF.Pages.Add(New PdfPage())
                    xgr = XGraphics.FromPdfPage(myPDF.Pages(0))
                    'Dim myImage As XImage = newImage
                    xgr.DrawImage(newImage, 0, 0)
                    xgr = Nothing
                    ''''

                    ''''salvar pdf
                    myPDF.Save("C:\Inetpub\wwwroot\scanner2\Files\teste.pdf")
                    myPDF = Nothing
                    ''''

                End Using

                Return True
            Catch ex As Exception

                Return False
            End Try
        Else
            Return False
        End If
    End Function

Author:  Soldier-B [ Wed Jun 17, 2009 8:30 pm ]
Post subject:  Re: Forcing a image to fit A4 size

Code:
Dim tooBig As New Bitmap("filepath here")
Dim justRight As New Bitmap(desiredWidth, desiredHeight)

Using g As Graphics = Graphics.FromImage(justRight)
     g.DrawImage(tooBig, 0, 0, justRight.Width, justRight.Height)
End Using

tooBig.Dispose()

This will essentially resize your image. All you have to do is replace desiredWidth and desiredHeight with (you guessed it) the width and height you wish the image to be. This code doesn't account for maintaining aspect ratios so if your original image and new image have different aspect ratios then the resulting image will be distorted.

- B

Author:  Patroni [ Thu Jun 18, 2009 9:31 am ]
Post subject:  Re: Forcing a image to fit A4 size

Hey

Thanks for reply!

I've tryied allready and still I have problem. I'll try explain in detail so you guys can check if i have something wrong.
I'm scanning a A4 document at 200dpi. Then I call this method so I can get the document in pdf format. The problems is that the pdf
that I'm getting doesn't fit the image because somehow the image as gotten to big to fit in A4 document. Weird enough is that if i call this method and use
a image that was allready saved in to for exemple C:\ (using a load image instead of using one that was scanned right away) the pdf I get it's ok...nothing wrong...no big image no nothing! So if I scan and change to pdf right away I get problem if I scan, save to disk and then load image and change to pdf I get everything ok...
Like I said i tryed to use with and height to resize image but the result was the same.
Can you guys notice something in code that is wrong or maybe it's scanner problem?

Help in advance

Author:  Soldier-B [ Thu Jun 18, 2009 2:02 pm ]
Post subject:  Re: Forcing a image to fit A4 size

Ok, here's another suggestion. Try putting a break point on this line of code: "xgr.DrawImage(newImage, 0, 0)". When the break point gets hit check the newImage object to see what dpi it is before you render it to the page. Specifically you'll be looking for newImage.HorizontalResolution and newImage.VerticalResolution. If your scanned image is supposed to be 200dpi then both of the properties should be 200. If those values don't match then you'll need to use the Bitmap object instead of the Image object in order to reset the dpi, like so: bitmapObject.SetResolution(dpiX, dpiY).

Author:  Patroni [ Tue Jun 23, 2009 11:42 am ]
Post subject:  Re: Forcing a image to fit A4 size

Thanks for the tip Soldier

Actually now it's working i change the code to something like

Code:
Dim myImage = New System.Drawing.Bitmap(newImage)
                    myImage.SetResolution(200, 200)

                    xgr.DrawImage(myImage, 0, 0)
                    xgr = Nothing


thanks once again

Author:  Soldier-B [ Tue Jun 23, 2009 7:24 pm ]
Post subject:  Re: Forcing a image to fit A4 size

Patroni wrote:
Thanks for the tip Soldier

Actually now it's working i change the code to something like

Code:
Dim myImage = New System.Drawing.Bitmap(newImage)
                    myImage.SetResolution(200, 200)

                    xgr.DrawImage(myImage, 0, 0)
                    xgr = Nothing


thanks once again


Glad I could help.

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