Question SetPixel API Function

Pirahnaplant

Well-known member
Joined
Mar 29, 2009
Messages
75
Programming Experience
3-5
I am trying to use the SetPixel API function to modify a bitmap. The problem is that it doesn't do anything; the image just stays completely transparent. Here is some sample code that shows what I'm trying to do.

VB.NET:
        Dim img As New Bitmap(64, 64)
        Dim hdc As IntPtr = Graphics.FromImage(img).GetHdc
        Dim redColor As Long = &HFF0000L
        For y As Long = 0 To 63
            For x As Long = 0 To 63
                SetPixel(hdc, x, y, redColor)
            Next
        Next
 
If it's any comfort Bitmap.SetPixel is actually calling that API, a lot of .Net is wrappers for common API functions. If you need more speed than that look into using Bitmap.LockBits to work with the unmanaged pixel data, there is an introduction for example here Using the LockBits method to access image data.
 

Latest posts

Back
Top