Click here to Skip to main content
15,890,382 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 114.4K   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 12:54
professionalv_wheeler13-Apr-18 12:54 
QuestionFaster Still Pin
v_wheeler13-Apr-18 12:19
professionalv_wheeler13-Apr-18 12:19 
GeneralVery usefull piece of code Pin
nmajor21-Apr-08 23:26
nmajor21-Apr-08 23:26 
QuestionWould like to use Fast Pixel but...why do I get this error? Pin
led1-Apr-08 12:23
led1-Apr-08 12:23 
GeneralGreat code... but there may be an issue Pin
Stimphy17-Jan-07 8:36
Stimphy17-Jan-07 8:36 
GeneralCounterpart code in MFC/GDI... [modified] Pin
Jun Du6-Jan-07 2:28
Jun Du6-Jan-07 2:28 
GeneralA C# code translation of this utility Pin
ratamoa19-Oct-06 10:43
ratamoa19-Oct-06 10:43 
GeneralRe: A C# code translation of this utility Pin
Christian Graus19-Oct-06 10:55
protectorChristian Graus19-Oct-06 10:55 
GeneralRe: A C# code translation of this utility Pin
AndrewVos19-Oct-06 23:31
AndrewVos19-Oct-06 23:31 
GeneralRe: A C# code translation of this utility Pin
AndrewVos19-Oct-06 23:40
AndrewVos19-Oct-06 23:40 
Question1 bit conversion? Pin
beckerben23-Aug-06 2:59
beckerben23-Aug-06 2:59 
AnswerRe: 1 bit conversion? [modified] Pin
Alex@UEA24-Aug-06 0:04
Alex@UEA24-Aug-06 0:04 
Yep,

I've done something very similar to that in VB.NET, it was over 6 months ago which means I have little memory of actual hows and whys.
As I recall it decides which pixels are 'whiteish' and colours them green and which pixels are 'blackish' and colours them red (or maybe vice versa), oh and it keeps count, but it should be pretty simple to modify to your purposes. No promises as to quality of the code, I am sure there are others who can do that for you, but it did the job.

Alex

PS. it strikes me that I almost certainly modified code written by someone else to do this and I wouldn't wish to steal their thunder. If anyone recognises who the original source might be please comment to credit them.

Public Class blackish_whiteish

    Public Shared BlackPix As Integer
    Public Shared WhitePix As Integer

    Public Shared Function ProcessImage(ByRef b As Bitmap) As Bitmap
        Return bwProcessImage(b)
    End Function

    Private Shared Function bwProcessImage(ByRef b As Bitmap) As Bitmap

        'initialise 
        BlackPix = 0
        WhitePix = 0

        'create array to store pixel data
        Dim resArray(b.Width) As Double

        'lock bits
        Dim bmData As BitmapData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), _
        System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb)

        'width of bitmap row in memory
        Dim stride As Integer = bmData.Stride

        'pointers to the start of the bitmap data
        Dim scan0 As IntPtr = bmData.Scan0

        ' array to hold the colors
        Dim pixels(stride * b.Height - 1) As Byte
        Marshal.Copy(scan0, pixels, 0, pixels.Length)

        'store position of current pixel
        Dim position As Integer

        ' store the current pixels RGB:
        Dim R0, G0, B0 As Integer

        'sort out black, white, blackish, whiteish

        For x As Integer = 0 To b.Width - 1
            'zero the array
            resArray(x) = 0
            For y As Integer = 0 To b.Height - 1
                'position of current pixel's first byte:
                position = (y * stride) + (x * 4)

                B0 = pixels(position)
                G0 = pixels(position + 1)
                R0 = pixels(position + 2)

                If (B0 + G0 + R0 = 765) Or (B0 + G0 + R0 = 0) Then
                    'black or white
                    If (B0 + G0 + R0 = 0) Then
                        BlackPix = BlackPix + 1
                        resArray(x) = resArray(x) + 1
                    Else
                        WhitePix = WhitePix + 1
                    End If
                Else
                    If (B0 + G0 + R0 > 382) Then
                        pixels(position) = Convert.ToByte(0)
                        pixels(position + 1) = Convert.ToByte(0)
                        pixels(position + 2) = Convert.ToByte(255)
                        WhitePix = WhitePix + 1
                    Else

                        pixels(position) = Convert.ToByte(0)
                        pixels(position + 1) = Convert.ToByte(255)
                        pixels(position + 2) = Convert.ToByte(0)
                        BlackPix = BlackPix + 1
                        resArray(x) = resArray(x) + 1
                    End If
                End If
            Next 'height

        Next 'width
        Marshal.Copy(pixels, 0, scan0, pixels.Length)
        histoArray = resArray
        b.UnlockBits(bmData)

        Return b

    End Function





-- modified at 6:08 Thursday 24th August, 2006
GeneralRe: 1 bit conversion? Pin
Alex@UEA24-Aug-06 0:19
Alex@UEA24-Aug-06 0:19 
GeneralRe: 1 bit conversion? Pin
beckerben24-Aug-06 2:00
beckerben24-Aug-06 2:00 
GeneralRe: 1 bit conversion? Pin
Alex@UEA24-Aug-06 3:43
Alex@UEA24-Aug-06 3:43 
GeneralCorrection [modified] Pin
567890123416-Aug-06 3:55
567890123416-Aug-06 3:55 
GeneralRe: Correction Pin
AndrewVos16-Aug-06 6:01
AndrewVos16-Aug-06 6:01 
GeneralRe: Correction Pin
567890123416-Aug-06 6:32
567890123416-Aug-06 6:32 
GeneralEasy of use Pin
NormDroid15-Aug-06 23:17
professionalNormDroid15-Aug-06 23:17 
GeneralRe: Easy of use Pin
AndrewVos16-Aug-06 0:25
AndrewVos16-Aug-06 0:25 
GeneralRe: Ease of use Pin
Dustin Metzgar16-Aug-06 2:46
Dustin Metzgar16-Aug-06 2:46 
GeneralRe: Ease of use Pin
anomaly25-Oct-06 20:06
anomaly25-Oct-06 20:06 
GeneralRe: Easy of use Pin
Axel Rietschin16-Aug-06 0:46
professionalAxel Rietschin16-Aug-06 0:46 
GeneralRe: Easy of use Pin
NormDroid16-Aug-06 0:54
professionalNormDroid16-Aug-06 0: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.