PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 8:18 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Jun 17, 2009 2:13 pm 
Offline

Joined: Fri Jun 05, 2009 3:00 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 17, 2009 8:30 pm 
Offline
User avatar

Joined: Tue Oct 14, 2008 6:15 pm
Posts: 32
Location: USA
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 18, 2009 9:31 am 
Offline

Joined: Fri Jun 05, 2009 3:00 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 18, 2009 2:02 pm 
Offline
User avatar

Joined: Tue Oct 14, 2008 6:15 pm
Posts: 32
Location: USA
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).


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 23, 2009 11:42 am 
Offline

Joined: Fri Jun 05, 2009 3:00 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 23, 2009 7:24 pm 
Offline
User avatar

Joined: Tue Oct 14, 2008 6:15 pm
Posts: 32
Location: USA
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.


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 376 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