Click here to Skip to main content
11,709,033 members (65,120 online)
Click here to Skip to main content

FastPixel - A much faster alternative to Bitmap.SetPixel

, 15 Aug 2006 CPOL 72.1K 1.8K 40
Rate this:
Please Sign up or sign in to vote.
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, 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

You may also be interested in...

Comments and Discussions

 
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 
GeneralRe: A C# code translation of this utility Pin
Christian Graus19-Oct-06 10:55
staffChristian 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
Alex Fr16-Aug-06 3:55
memberAlex Fr16-Aug-06 3:55 
GeneralRe: Correction Pin
AndrewVos16-Aug-06 6:01
memberAndrewVos16-Aug-06 6:01 
GeneralRe: Correction Pin
Alex Fr16-Aug-06 6:32
memberAlex Fr16-Aug-06 6:32 
GeneralEasy of use Pin
norm .net15-Aug-06 23:17
membernorm .net15-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
axelriet16-Aug-06 0:46
memberaxelriet16-Aug-06 0:46 
GeneralRe: Easy of use Pin
norm .net16-Aug-06 0:54
membernorm .net16-Aug-06 0:54 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 16 Aug 2006
Article Copyright 2006 by AndrewVos
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid