Click here to Skip to main content
Click here to Skip to main content

FastPixel - A much faster alternative to Bitmap.SetPixel

By , 15 Aug 2006
 

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)

About the Author

AndrewVos
Software Developer
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralVery usefull piece of codemembernmajor21 Apr '08 - 23:26 
QuestionWould like to use Fast Pixel but...why do I get this error?memberled1 Apr '08 - 12:23 
GeneralGreat code... but there may be an issuememberStimphy17 Jan '07 - 8:36 
GeneralCounterpart code in MFC/GDI... [modified]memberJun Du6 Jan '07 - 2:28 
GeneralA C# code translation of this utilitymemberratamoa19 Oct '06 - 10:43 
GeneralRe: A C# code translation of this utilitystaffChristian Graus19 Oct '06 - 10:55 
GeneralRe: A C# code translation of this utilitymemberAndrewVos19 Oct '06 - 23:31 
GeneralRe: A C# code translation of this utilitymemberAndrewVos19 Oct '06 - 23:40 
Question1 bit conversion?memberbeckerben23 Aug '06 - 2:59 
AnswerRe: 1 bit conversion? [modified]memberAlex@UEA24 Aug '06 - 0:04 
GeneralRe: 1 bit conversion?memberAlex@UEA24 Aug '06 - 0:19 
GeneralRe: 1 bit conversion?memberbeckerben24 Aug '06 - 2:00 
GeneralRe: 1 bit conversion?memberAlex@UEA24 Aug '06 - 3:43 
GeneralCorrection [modified]memberAlex Fr16 Aug '06 - 3:55 
GeneralRe: CorrectionmemberAndrewVos16 Aug '06 - 6:01 
GeneralRe: CorrectionmemberAlex Fr16 Aug '06 - 6:32 
GeneralEasy of usemembernorm .net15 Aug '06 - 23:17 
GeneralRe: Easy of usememberAndrewVos16 Aug '06 - 0:25 
GeneralRe: Ease of usememberDustin Metzgar16 Aug '06 - 2:46 
GeneralRe: Ease of usememberanomaly25 Oct '06 - 20:06 
GeneralRe: Easy of usememberaxelriet16 Aug '06 - 0:46 
GeneralRe: Easy of usemembernorm .net16 Aug '06 - 0:54 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 16 Aug 2006
Article Copyright 2006 by AndrewVos
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid