Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

FastPixel - A much faster alternative to Bitmap.SetPixel

0.00/5 (No votes)
15 Aug 2006 1  
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

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here