Resolved VS2008 Create Bitmap to certain PPI

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I'm trying to create an image for printing and I know what the dimensions of the image (Bitmap object) is supposed to be in Inches but the Bitmap uses pixels so how would I define it's size to the number of pixels per inch?

I do know what the printer's DPI is too, I'm able to get the horizontal DPI and the vertical DPI (which is 99.99% of the time always the same)
 
Now that I have time to look into this again, how does that help me get the number of pixels? I need to get width & height number of pixels so I can dim the bitmap object in the first place.

Say I need the bitmap to be printed to be 1" by 1.25", how many pixels is in 1" is what I need to know so I can do this:
VB.NET:
Private Function GetImage(ByVal PrinterX_DPI As Single, ByVal PrinterY_DPI As Single) As Bitmap
    Dim myImage As New Bitmap(NumberOfPixelsInOneInch, NumberOfPixelsInOnePointTwoFiveInches)
    Using g As Graphics = Graphics.FromImage(myImage)
        If OutputVertically Then
            g.TranslateTransform(Output.Width, 0I) 'change origin to top-right page corner
            g.RotateTransform(90I) 'rotate
        End If

        'Draw the needed text

        g.Save()
    End Using
    Return myImage
End Function

My PrintDocument takes the image and places it on the page as needed (Left, Center or Right) and the image should already be rotated (orientation) correctly, I can't rotate the whole page which is why I need to have a function that simply outputs the image already rotated or not.
VB.NET:
    Private Sub EnvelopePrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles EnvelopePrintDocument.PrintPage
        Dim ImageToPrint As Bitmap
        With e.PageSettings.PrinterSettings.DefaultPageSettings.PrinterResolution
            ImageToPrint = GetImage(.X, .Y)
        End With
        Dim imgPlacement As New Point(0I, 0I)
        Select Case GetImgAlignment(g_ImgSettings.AlignmentID)
            Case "Center"
                imgPlacement.X = CInt(e.PageBounds.Right / 2I) - CInt(ImageToPrint.Width / 2I)
            Case "Right"
                imgPlacement.X = e.PageBounds.Right - EnvImage.Width
        End Select
        e.Graphics.DrawImage(ImageToPrint, imgPlacement)
        e.HasMorePages = False
    End Sub

I'm just stuck on getting the physical image size in pixels right now, I can probably figure everything else out once I have the correct image & it's correct size done.
 
Turns out, the units are 100th of an inch, so I just take the 1.25" x 1" and multiply each by 100 to get 125 x 100 and I'm good to go :)
 
Back
Top