As part of a larger tool I'm working displaying pieces of a bitmap in a picturebox. In my current test case, I want to take a 200x200 section of the bitmap and display it in a 200x200 picturebox (clientsize area). The code runs, but I find I'm actually getting 600x600 pixels from the bitmap in the picturebox. This is all running on a 4K monitor, and I suspect some dpi-based scaling is the culprit. I will want to use that scaling to allow zoom, but I can't see why it's happening at all right now.
Here's the code - the src_rect and dest_rect are identical, both 200x200, and this variant of DrawImage shouldn't be scaling on its own:
Public Overrides Sub Draw_Data(Pbox As PictureBox, Src_Rect As Rectangle, Dst_Rect As Rectangle)
''Pbox is the destination picturbox:
Dim bm As New Bitmap(Pbox.ClientSize.Width, Pbox.ClientSize.Height)
Dim gr As Graphics = Graphics.FromImage(bm) 'So GR relates to drawing destination.
gr.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
gr.SmoothingMode = Drawing2D.SmoothingMode.None
gr.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
'' Bitmap is the underlying data, a subset of which should be shown:
gr.DrawImage(Bitmap, Dst_Rect, Src_Rect, GraphicsUnit.Pixel)
Pbox.Image = bm
End Sub
I've played with the various gr.* settings, as well as the picturebox properties, trying pretty much anything reasonable, but I always get the same result. If it's an issue of bitmap "resolution", that's odd because the originating bitmap is (at least to me, conceptually) just an array of pixels. It would seem that a mapping of a 200x200 subset of that to a 200x200 bitmap used in the picturebox would be one-to-one. Does anyone see what I might be missing? If there's some scaling I have to apply, I can certainly do it, but I'd have to have some way of at least measuring the kind of weird scaling that's going on before I can compensate for it.