On a form I have a PictureBox, a button to load image in the picturebox and couple of more buttons to do some operations on the image loaded into the picturebox.
I load a bitmap image into the picturebox and then I want to perform some operation on pixel ranges rgb(150,150,150) to rgb(192,222,255) of the loaded image.
- Is it possible to do this using
SetPixelmethod? - Is there any way to specify a range of RGB values in C#?
Simple way would be something like this:
With
ColorWithinRangedefined like this:For large bitmap sizes, however,
GetPixelandSetPixelbecome very slow. So, after you have implemented your algorithm, if it feels slow, you can use theBitmap.LockBitsmethod to pin the bitmap (prevent GC from moving it around memory) and allow yourself fast unsafe access to individual bytes.