Search code examples
.netvb.netprintingcoordinate-transformation

.NET printing coordinate conversion


I can't get a simple answer to this. I have pixel coordinates, I want to print an image in a (landscape) page at those coords.

In my printing event I do:

Dim mypoint As New PointF(1, 1192)
e.Graphics.DrawImage(My.Resources.littleSquare, mypoint)

This obviously doesn't work: I specify pixels but the driver expects inches(?) or what?

Tried to: e.Graphics.PageUnit = GraphicsUnit.Inch with no luck.

I'd like a conversion method like:

Dim mypoint As New PointF(convertPixelsIntoInches(1), convertPixelsIntoInches(1192))
e.Graphics.DrawImage(My.Resources.littleSquare, mypoint)

Private Function convertPixelsIntoInches(ByVal pixels As Integer) As Single
    Return ??
End Function

Any hints? Thanks.


Solution

  • I think I got it.

    My pixel coordinates are not fixed, but relative to a 300dpi canvas, thus I have to do a double DPI conversion, like this:

    e.Graphics.PageUnit = GraphicsUnit.Pixel
    dpiX = e.Graphics.DpiX
    dpiY = e.Graphics.DpiY
    
    Dim mypoint As New PointF(convertPixelsIntoInchesX(1501), convertPixelsIntoInchesY(1192))
    e.Graphics.DrawImage(My.Resources.myblacksquare, mypoint)
    
    Private Function convertPixelsIntoInchesX(ByVal pixel As Integer) As Single
       Return CSng(pixel * dpiX / 300)
    End Function
    
    Private Function convertPixelsIntoInchesY(ByVal pixel As Integer) As Single
            Return CSng(pixel * dpiY / 300)
    End Function