Click here to Skip to main content
Licence CPOL
First Posted 15 Aug 2006
Views 57,277
Downloads 574
Bookmarked 37 times

FastPixel - A much faster alternative to Bitmap.SetPixel

By | 15 Aug 2006 | Article
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)

About the Author

AndrewVos

Software Developer

United Kingdom United Kingdom

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralVery usefull piece of code Pinmembernmajor23:26 21 Apr '08  
QuestionWould like to use Fast Pixel but...why do I get this error? Pinmemberled12:23 1 Apr '08  
GeneralGreat code... but there may be an issue PinmemberStimphy8:36 17 Jan '07  
GeneralCounterpart code in MFC/GDI... [modified] PinmemberJun Du2:28 6 Jan '07  
GeneralA C# code translation of this utility Pinmemberratamoa10:43 19 Oct '06  
GeneralRe: A C# code translation of this utility PinstaffChristian Graus10:55 19 Oct '06  
GeneralRe: A C# code translation of this utility PinmemberAndrewVos23:31 19 Oct '06  
GeneralRe: A C# code translation of this utility PinmemberAndrewVos23:40 19 Oct '06  
Question1 bit conversion? Pinmemberbeckerben2:59 23 Aug '06  
AnswerRe: 1 bit conversion? [modified] PinmemberAlex@UEA0:04 24 Aug '06  
GeneralRe: 1 bit conversion? PinmemberAlex@UEA0:19 24 Aug '06  
GeneralRe: 1 bit conversion? Pinmemberbeckerben2:00 24 Aug '06  
GeneralRe: 1 bit conversion? PinmemberAlex@UEA3:43 24 Aug '06  
GeneralCorrection [modified] PinmemberAlex Fr3:55 16 Aug '06  
GeneralRe: Correction PinmemberAndrewVos6:01 16 Aug '06  
GeneralRe: Correction PinmemberAlex Fr6:32 16 Aug '06  
GeneralEasy of use Pinmembernorm .net23:17 15 Aug '06  
GeneralRe: Easy of use PinmemberAndrewVos0:25 16 Aug '06  
GeneralRe: Ease of use PinmemberDustin Metzgar2:46 16 Aug '06  
GeneralRe: Ease of use Pinmemberanomaly20:06 25 Oct '06  
GeneralRe: Easy of use Pinmemberaxelriet0:46 16 Aug '06  
GeneralRe: Easy of use Pinmembernorm .net0:54 16 Aug '06  

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.

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