Click here to Skip to main content
14,927,366 members
Articles / Multimedia / GDI+
Article
Posted 15 Aug 2006

Stats

102K views
2.8K downloads
43 bookmarked

FastPixel - A much faster alternative to Bitmap.SetPixel

Rate me:
Please Sign up or sign in to vote.
4.14/5 (23 votes)
15 Aug 2006CPOL
Ever wanted something faster than SetPixel? Well, you've found it.

Sample Image - screenShot.jpg

Introduction

I have been working on some graphics code lately, and have been quite irritated with SetPixel and its performance overheads. There is an alternative though: lock the bitmap and interact directly with an array of bytes each representing R, G, or B (and sometimes A, depending on whether it's an alpha bitmap or not).

Usage

VB
Dim fp As New FastPixel(image)
fp.Lock()
fp.SetPixel(x, y, Color.Green)
fp.Unlock(True)

What I've learnt

  • Using Bitmap.Width or Bitmap.Height during an intense operation is very slow, because GdipGetImageWidth (or GdipGetImageHeight) is used to retrieve the value, and it isn't cached.
  • Using this class introduces speeds over 20 times faster than using SetPixel, on average.

Conclusion

Use the class!

Sorry about the length of this article, but there isn't much to explain. It's quite basic, and I just wanted to get it out there so people could use it :>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

AndrewVos
Software Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionSmall Correction Pin
v_wheeler13-Apr-18 12:54
professionalv_wheeler13-Apr-18 12:54 
QuestionFaster Still Pin
v_wheeler13-Apr-18 12:19
professionalv_wheeler13-Apr-18 12:19 
GeneralVery usefull piece of code Pin
nmajor21-Apr-08 23:26
Membernmajor21-Apr-08 23:26 
QuestionWould like to use Fast Pixel but...why do I get this error? Pin
led1-Apr-08 12:23
Memberled1-Apr-08 12:23 
GeneralGreat code... but there may be an issue Pin
Stimphy17-Jan-07 8:36
MemberStimphy17-Jan-07 8:36 
GeneralCounterpart code in MFC/GDI... [modified] Pin
Jun Du6-Jan-07 2:28
MemberJun Du6-Jan-07 2:28 
GeneralA C# code translation of this utility Pin
ratamoa19-Oct-06 10:43
Memberratamoa19-Oct-06 10:43 
using System;
using System.Drawing;
using System.Drawing.Imaging;

class FastPixel {
private byte[] rgbValues;
private BitmapData bmpData;
private IntPtr bmpPtr;
private bool locked = false;

private bool _isAlpha = false;
private Bitmap _bitmap;
private int _width;
private int _height;

public int Width {
get {
return this._width;
}
}

public int Height {
get {
return this._height;
}
}

public bool IsAlphaBitmap {
get {
return this._isAlpha;
}
}

public Bitmap Bitmap {
get {
return this._bitmap;
}
}

public FastPixel(Bitmap bitmap) {
if (bitmap.PixelFormat == (bitmap.PixelFormat | PixelFormat.Indexed))
throw new Exception("Cannot lock an Indexed image.");

this._bitmap = bitmap;
this._isAlpha = (this.Bitmap.PixelFormat == (this.Bitmap.PixelFormat | PixelFormat.Alpha));
this._width = bitmap.Width;
this._height = bitmap.Height;
}

public void Lock() {
if (this.locked)
throw new Exception("Bitmap already locked.");

Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
this.bmpData = this.Bitmap.LockBits(rect, ImageLockMode.ReadWrite, this.Bitmap.PixelFormat);
this.bmpPtr = this.bmpData.Scan0;

if (this.IsAlphaBitmap) {
int bytes = (this.Width * this.Height) * 4;
this.rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(this.bmpPtr, rgbValues, 0, this.rgbValues.Length);
} else {
int bytes = (this.Width * this.Height) * 3;
this.rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(this.bmpPtr, rgbValues, 0, this.rgbValues.Length);
}
this.locked = true;
}

public void Unlock(bool setPixels) {
if (!this.locked)
throw new Exception("Bitmap not locked.");

// Copy the RGB values back to the bitmap;
if (setPixels)
System.Runtime.InteropServices.Marshal.Copy(this.rgbValues, 0, this.bmpPtr, this.rgbValues.Length);

// Unlock the bits.;
this.Bitmap.UnlockBits(bmpData);
this.locked = false;
}

public void Clear(Color colour) {
if (!this.locked)
throw new Exception("Bitmap not locked.");

if (this.IsAlphaBitmap) {
for (int index = 0; index < this.rgbValues.Length; index += 4) {
this.rgbValues[index] = colour.B;
this.rgbValues[index + 1] = colour.G;
this.rgbValues[index + 2] = colour.R;
this.rgbValues[index + 3] = colour.A;
}
} else {
for (int index = 0; index < this.rgbValues.Length; index += 3) {
this.rgbValues[index] = colour.B;
this.rgbValues[index + 1] = colour.G;
this.rgbValues[index + 2] = colour.R;
}
}
}

public void SetPixel(Point location, Color colour) {
this.SetPixel(location.X, location.Y, colour);
}

public void SetPixel(int x, int y, Color colour) {
if (!this.locked)
throw new Exception("Bitmap not locked.");

if (this.IsAlphaBitmap) {
int index = ((y * this.Width + x) * 4);
this.rgbValues[index] = colour.B;
this.rgbValues[index + 1] = colour.G;
this.rgbValues[index + 2] = colour.R;
this.rgbValues[index + 3] = colour.A;
} else {
int index = ((y * this.Width + x) * 3);
this.rgbValues[index] = colour.B;
this.rgbValues[index + 1] = colour.G;
this.rgbValues[index + 2] = colour.R;
}
}

public Color GetPixel(Point location) {
return this.GetPixel(location.X, location.Y);
}

public Color GetPixel(int x, int y) {
if (!this.locked)
throw new Exception("Bitmap not locked.");

if (this.IsAlphaBitmap) {
int index = ((y * this.Width + x) * 4);
int b = this.rgbValues[index];
int g = this.rgbValues[index + 1];
int r = this.rgbValues[index + 2];
int a = this.rgbValues[index + 3];
return Color.FromArgb(a, r, g, b);
} else {
int index = ((y * this.Width + x) * 3);
int b = this.rgbValues[index];
int g = this.rgbValues[index + 1];
int r = this.rgbValues[index + 2];
return Color.FromArgb(r, g, b);
}
}
}
GeneralRe: A C# code translation of this utility Pin
Christian Graus19-Oct-06 10:55
protectorChristian Graus19-Oct-06 10:55 
GeneralRe: A C# code translation of this utility Pin
AndrewVos19-Oct-06 23:31
MemberAndrewVos19-Oct-06 23:31 
GeneralRe: A C# code translation of this utility Pin
AndrewVos19-Oct-06 23:40
MemberAndrewVos19-Oct-06 23:40 
Question1 bit conversion? Pin
beckerben23-Aug-06 2:59
Memberbeckerben23-Aug-06 2:59 
AnswerRe: 1 bit conversion? [modified] Pin
Alex@UEA24-Aug-06 0:04
MemberAlex@UEA24-Aug-06 0:04 
GeneralRe: 1 bit conversion? Pin
Alex@UEA24-Aug-06 0:19
MemberAlex@UEA24-Aug-06 0:19 
GeneralRe: 1 bit conversion? Pin
beckerben24-Aug-06 2:00
Memberbeckerben24-Aug-06 2:00 
GeneralRe: 1 bit conversion? Pin
Alex@UEA24-Aug-06 3:43
MemberAlex@UEA24-Aug-06 3:43 
GeneralCorrection [modified] Pin
567890123416-Aug-06 3:55
Member567890123416-Aug-06 3:55 
GeneralRe: Correction Pin
AndrewVos16-Aug-06 6:01
MemberAndrewVos16-Aug-06 6:01 
GeneralRe: Correction Pin
567890123416-Aug-06 6:32
Member567890123416-Aug-06 6:32 
GeneralEasy of use Pin
NormDroid15-Aug-06 23:17
professionalNormDroid15-Aug-06 23:17 
GeneralRe: Easy of use Pin
AndrewVos16-Aug-06 0:25
MemberAndrewVos16-Aug-06 0:25 
GeneralRe: Ease of use Pin
Dustin Metzgar16-Aug-06 2:46
MemberDustin Metzgar16-Aug-06 2:46 
GeneralRe: Ease of use Pin
anomaly25-Oct-06 20:06
Memberanomaly25-Oct-06 20:06 
GeneralRe: Easy of use Pin
Axel Rietschin16-Aug-06 0:46
professionalAxel Rietschin16-Aug-06 0:46 
GeneralRe: Easy of use Pin
NormDroid16-Aug-06 0:54
professionalNormDroid16-Aug-06 0:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.