Click here to Skip to main content
15,846,571 members
Articles / Multimedia / GDI+

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 113.6K   3K   43   24
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)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSmall Correction Pin
v_wheeler13-Apr-18 13:54
professionalv_wheeler13-Apr-18 13:54 
QuestionFaster Still Pin
v_wheeler13-Apr-18 13:19
professionalv_wheeler13-Apr-18 13:19 
GeneralVery usefull piece of code Pin
nmajor22-Apr-08 0:26
nmajor22-Apr-08 0:26 
QuestionWould like to use Fast Pixel but...why do I get this error? Pin
led1-Apr-08 13:23
led1-Apr-08 13:23 
Hello: I'm trying to use Fast Pixel and it seems to work for some of my bitmpas...while giving me this error for others:

Index was outside the boundws of the array(see ''it bombs on this line: ' to see exact line below). I understand that the array isn't big enough but i don't understand why it works for some of my bitmaps and not others.
When I save my bitmaps, I save them as 24 bit rgb color 300 dpi. I don't know much about resolutions and what format is best to save them as.

Any thoughts are truly appreciated.

Public Function GetPixel(ByVal x As Integer, ByVal y As Integer) As Color
If Not Me.locked Then
Throw New Exception("Bitmap not locked.")
Return Nothing
End If

If Me.IsAlphaBitmap Then
Dim index As Integer = ((y * Me.Width + x) * 4)
Dim b As Integer = Me.rgbValues(index)
Dim g As Integer = Me.rgbValues(index + 1)
Dim r As Integer = Me.rgbValues(index + 2)
Dim a As Integer = Me.rgbValues(index + 3)
Return Color.FromArgb(a, r, g, b)
Else
Dim index As Integer = ((y * Me.Width + x) * 3)
'it bombs on this line: Dim b As Integer = Me.rgbValues(index)
Dim g As Integer = Me.rgbValues(index + 1)
Dim r As Integer = Me.rgbValues(index + 2)
Return Color.FromArgb(r, g, b)
End If
End Function
GeneralGreat code... but there may be an issue Pin
Stimphy17-Jan-07 9:36
Stimphy17-Jan-07 9:36 
GeneralCounterpart code in MFC/GDI... [modified] Pin
Jun Du6-Jan-07 3:28
Jun Du6-Jan-07 3:28 
GeneralA C# code translation of this utility Pin
ratamoa19-Oct-06 11:43
ratamoa19-Oct-06 11:43 
GeneralRe: A C# code translation of this utility Pin
Christian Graus19-Oct-06 11:55
protectorChristian Graus19-Oct-06 11:55 
GeneralRe: A C# code translation of this utility Pin
AndrewVos20-Oct-06 0:31
AndrewVos20-Oct-06 0:31 
GeneralRe: A C# code translation of this utility Pin
AndrewVos20-Oct-06 0:40
AndrewVos20-Oct-06 0:40 
Question1 bit conversion? Pin
beckerben23-Aug-06 3:59
beckerben23-Aug-06 3:59 
AnswerRe: 1 bit conversion? [modified] Pin
Alex@UEA24-Aug-06 1:04
Alex@UEA24-Aug-06 1:04 
GeneralRe: 1 bit conversion? Pin
Alex@UEA24-Aug-06 1:19
Alex@UEA24-Aug-06 1:19 
GeneralRe: 1 bit conversion? Pin
beckerben24-Aug-06 3:00
beckerben24-Aug-06 3:00 
GeneralRe: 1 bit conversion? Pin
Alex@UEA24-Aug-06 4:43
Alex@UEA24-Aug-06 4:43 
GeneralCorrection [modified] Pin
567890123416-Aug-06 4:55
567890123416-Aug-06 4:55 
GeneralRe: Correction Pin
AndrewVos16-Aug-06 7:01
AndrewVos16-Aug-06 7:01 
GeneralRe: Correction Pin
567890123416-Aug-06 7:32
567890123416-Aug-06 7:32 
GeneralEasy of use Pin
NormDroid16-Aug-06 0:17
professionalNormDroid16-Aug-06 0:17 
GeneralRe: Easy of use Pin
AndrewVos16-Aug-06 1:25
AndrewVos16-Aug-06 1:25 
GeneralRe: Ease of use Pin
Dustin Metzgar16-Aug-06 3:46
Dustin Metzgar16-Aug-06 3:46 
GeneralRe: Ease of use Pin
anomaly25-Oct-06 21:06
anomaly25-Oct-06 21:06 
GeneralRe: Easy of use Pin
Axel Rietschin16-Aug-06 1:46
professionalAxel Rietschin16-Aug-06 1:46 
GeneralRe: Easy of use Pin
NormDroid16-Aug-06 1:54
professionalNormDroid16-Aug-06 1: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.