c++ trouble with making a bitmap from scratch

I am trying to make a bitmap from scratch. I have a BYTE array (with known size) of RGB values and I would like to generate an HBITMAP.

For further clarification, the array of bytes I am working with is purely RGB values.

I have made sure that all variables are set and proper, and I believe that the issue has to do with lpvBits. I have been doing as much research for this in the past few days I have been unable to find anything that makes sense to me.

For testing purposes the width = 6 and height = 1

Code:

HBITMAP RayTracing::getBitmap(void){
    BYTE * bytes = getPixels();
    void * lpvBits = (void *)bytes;
    HBITMAP hBMP = CreateBitmap(width, height, 1, 24, lpvBits);
    return hBMP;
}
BYTE * RayTracing::getPixels(void){
    Vec3 * vecs = display.getPixels();
    BYTE * bytes;
    bytes = new BYTE[(3 * width * height)];
    for (unsigned int i = 0; i < (width * height); i++){
        *bytes = static_cast<BYTE>(vecs->x);
        bytes++;
        *bytes = static_cast<BYTE>(vecs->y);
        bytes++;
        *bytes = static_cast<BYTE>(vecs->z);
        bytes++;
        vecs++;
    }
    return bytes;
}

Answers


You need to properly dword-align your array so each line is an even multiple of 4 bytes, and then skip those bytes when filling the array:

HBITMAP RayTracing::getBitmap(void)
{
    BYTE * bytes = getPixels();
    HBITMAP hBMP = CreateBitmap(width, height, 1, 24, bytes);
    delete[] bytes;
    return hBMP;
}

BYTE * RayTracing::getPixels(void)
{
    Vec3 * vecs = display.getPixels(); // <-- don't forget to free if needed
    int linesize = ((3 * width) + 3) & ~3; // <- 24bit pixels, width number of pixels, rounded to nearest dword boundary
    BYTE * bytes = new BYTE[linesize * height];
    for (unsigned int y = 0; y < height; y++)
    {
        BYTE *line = &bytes[linesize*y];
        Vec3 *vec = &vecs[width*y];
        for (unsigned int x = 0; x < width; x++)
        {
            *line++ = static_cast<BYTE>(vec->x);
            *line++ = static_cast<BYTE>(vec->y);
            *line++ = static_cast<BYTE>(vec->z);
            ++vec;
        }
    }
    return bytes;
}

Need Your Help

UILabel view disappear when the height greater than 8192

ios uilabel

Assigning large string to UILabel. And, adding this label into a scroll view.

GridSplitter: Scale cell that is not directly adjacent to GridSplitter

wpf layout grid gridsplitter

I have a WPF Grid with four columns containing only one GridSplitter. Two of the columns (0 and 3) are scalable, but one scalable column is not direclty adjacent to the GridSplitter (column 2).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.