Click here to Skip to main content
15,877,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all, i'm in trouble with LockBits gdi+ function, it dosen't seems work correctly.

This is my very simple test code:

BitmapData dataHigh;
BitmapData dataImg;
Bitmap *image = new Bitmap(480,520,PixelFormat24bppRGB);
Bitmap *imgSx = new Bitmap(480,480,PixelFormat24bppRGB);

imgSx->LockBits(Rect(10,10,10,10),ImageLockModeRead | ImageLockModeWrite,PixelFormat24bppRGB,&dataHigh);

image->LockBits(Rect(10,10,10,10),ImageLockModeRead | ImageLockModeWrite,PixelFormat24bppRGB,&dataImg);



I would fill with blank only the selection portion of image, but the result if a line not a square, Seem that the buffer pointed from Scan0 contains alla image data and not only portion selected with Rect.

I forced the number of byte in memset and memcpy because the Stride value of both BitmapData are always 1440 like entire image.

Can be a bug of LockBits function?

Thank you in advance!


1 solution

Stride is 1440, it means one row of Scan0 consists of 480( 1440 / 3) pixels information.

To memset a portion of bitmap, I suggest to do like this,

BYTE* pbyFirstLine = (BYTE*) dataHigh.Scan0;

    int nX_i = 10;
    int nY_i = 10;
    int nW_i = 10; // Width of Bitmap
    int nH_i = 10; // Height of bitmap

    for(int nY = nY_i; nY < nY_i + nH_i; nY++)
        // Seek in X and Y direction.
        BYTE* pbySeek = pbyFirstLine + ( nX_i * 3 ) + nY * dataHigh.Stride;
        memset(pbySeek, 0xFF, 3 * nW_i );

Scan0 always return first pixel of Bitmap, This may be a bug of Gdi :)
According to msdn documentation,
Scan0: Gets or sets the address of the first pixel data in the bitmap. This can also be thought of as the first scan line in the bitmap.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900