Note: If you have any suggestions on how to improve this code, you can contribute on the github repository.
When you are working with bitmaps in C#, you can use the GetPixel(x, y)
and SetPixel(x, y, color)
functions to get/set the pixel value. But they are very slow.
Here is the alternative way to work with bitmaps faster.
LockBitmap
With the LockBitmap
class, we can lock/unlock bitmap data.
public class LockBitmap
{
Bitmap source = null;
IntPtr Iptr = IntPtr.Zero;
BitmapData bitmapData = null;
public byte[] Pixels { get; set; }
public int Depth { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
public LockBitmap(Bitmap source)
{
this.source = source;
}
public void LockBits()
{
try
{
Width = source.Width;
Height = source.Height;
int PixelCount = Width * Height;
Rectangle rect = new Rectangle(0, 0, Width, Height);
Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);
if (Depth != 8 && Depth != 24 && Depth != 32)
{
throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
}
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
source.PixelFormat);
int step = Depth / 8;
Pixels = new byte[PixelCount * step];
Iptr = bitmapData.Scan0;
Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
}
catch (Exception ex)
{
throw ex;
}
}
public void UnlockBits()
{
try
{
Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);
source.UnlockBits(bitmapData);
}
catch (Exception ex)
{
throw ex;
}
}
public Color GetPixel(int x, int y)
{
Color clr = Color.Empty;
int cCount = Depth / 8;
int i = ((y * Width) + x) * cCount;
if (i > Pixels.Length - cCount)
throw new IndexOutOfRangeException();
if (Depth == 32) {
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
byte a = Pixels[i + 3]; clr = Color.FromArgb(a, r, g, b);
}
if (Depth == 24) {
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
clr = Color.FromArgb(r, g, b);
}
if (Depth == 8)
{
byte c = Pixels[i];
clr = Color.FromArgb(c, c, c);
}
return clr;
}
public void SetPixel(int x, int y, Color color)
{
int cCount = Depth / 8;
int i = ((y * Width) + x) * cCount;
if (Depth == 32) {
Pixels[i] = color.B;
Pixels[i + 1] = color.G;
Pixels[i + 2] = color.R;
Pixels[i + 3] = color.A;
}
if (Depth == 24) {
Pixels[i] = color.B;
Pixels[i + 1] = color.G;
Pixels[i + 2] = color.R;
}
if (Depth == 8)
{
Pixels[i] = color.B;
}
}
}
Benchmark
To test LockBitmap
's performance, we can use the Benchmark
class.
public class Benchmark
{
private static DateTime startDate = DateTime.MinValue;
private static DateTime endDate = DateTime.MinValue;
public static TimeSpan Span { get { return endDate.Subtract(startDate); } }
public static void Start() { startDate = DateTime.Now; }
public static void End() { endDate = DateTime.Now; }
public static double GetSeconds()
{
if (endDate == DateTime.MinValue) return 0.0;
else return Span.TotalSeconds;
}
}
Usage
Now, we can use the LockBitmap
class to work with images very fast.
public void ChangeColor()
{
Bitmap bmp = (Bitmap)Image.FromFile("d:\\source.png");
Benchmark.Start();
LockBitmap lockBitmap = new LockBitmap(bmp);
lockBitmap.LockBits();
Color compareClr = Color.FromArgb(255, 255, 255, 255);
for (int y = 0; y < lockBitmap.Height; y++)
{
for (int x = 0; x < lockBitmap.Width; x++)
{
if (lockBitmap.GetPixel(x, y) == compareClr)
{
lockBitmap.SetPixel(x, y, Color.Red);
}
}
}
lockBitmap.UnlockBits();
Benchmark.End();
double seconds = Benchmark.GetSeconds();
bmp.Save("d:\\result.png");
}