You don't need to do this. The approach you're using is called "Nearest Neighbour" interpolation; you can set this property and then call DrawImage
and you're all set:
using(Bitmap source = ...)
using(Bitmap destination = new Bitmap(1000, 1000))
using(Graphics g = Graphics.FromImage(newImg)) {
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage( source, new Rectangle( 0, 0, destination.Width, destination.Height ) );
}
Also, if you're using GetPixel
and SetPixel
there is no need to use LockBits
. In fact, in your example you call LockBits
and then immediately call UnlockBits
, but still read from the BitmapData
's pointer, that is dangerous and may result in data-corruption if the GC moves memory around while your code is executing.