Search code examples
vb.netimagewinformspicturebox

Draw zoomed pixelated image to picturebox


I have an png image 120x120. I want to take a part of it (10x10) and zoom it by a factor of x32 and show it to a picturebox pixelated.

what i have done:

bmp = New Bitmap(320, 320, PixelFormat.Format32bppArgb) 'create a bitmap x32
Dim g As Graphics = Graphics.FromImage(bmp)

'draw the part in that bitmap
g.DrawImage(My.Resources.MyImage, New Rectangle(0, 0, 320, 320), New Rectangle(50, 50, 10, 10), GraphicsUnit.Pixel)

PictureBox1.Image = bmp

g.Dispose()

The image is not pixelated. What can I do to fix it?


Solution

  • You have to specify in your graphics:

    g.InterpolationMode = InterpolationMode.NearestNeighbor
    

    and change the rectangles to:

    g.DrawImage(My.Resources.MyImage, New RectangleF(0, 0, 320, 320), New RectangleF(49.5, 49.5, 10, 10), GraphicsUnit.Pixel)
    

    so you will not lose half a pixel.