Im wondering is it possible to get/set pixels of a 24 bit bitmap in the memory without using gdi/directx, I know how to get/set pixels in a bitmap file, but how would do that to the bits in the memory or dc?
You should look up CreateCompatibleDC, CreateDIBSection
and SelectObject. I can post some (ugly!) C source code if
you'd like me to, which works. Or should work. Well, it's used
in a commercial project, so it had better work ;-).
I can create the dc's and get the bitmap in there, but how would i view/edit the colorbits after selecting the bitmap into the memorydc? (edit the hbmp or hdc directly without gdi?) if you have the C source please post it =)
okay, you asked for it :) It is for 32bpp (though by changing
"a few things", you should be able to make it work for any
resolution). After you have created the drawbuf, you can access
drawbuf->buf directly. When done with your changes, blit the
bitmap to screen the usual way.
typedef struct _DrawBuf {
int w;
int h;
unsigned int* buf;
BITMAPINFO bmp;
HBITMAP hbmp;
HDC hdcbmp;
} DrawBuf;
static bool createDrawBuf(DrawBuf *db, int width, int height)
{
memset(db, 0, sizeof(*db));
db->w = width;
db->h = height;
db->bmp.bmiHeader.biBitCount = 32;
// no colorTable since we're doing 32bpp.
db->bmp.bmiHeader.biClrImportant = 0;
db->bmp.bmiHeader.biClrUsed = 0;
db->bmp.bmiHeader.biCompression = BI_RGB;
// biHeight must be negative so we get a top-down bitmap. Fucked up MS.
db->bmp.bmiHeader.biHeight = 0 - (db->h);
db->bmp.bmiHeader.biPlanes = 1;
db->bmp.bmiHeader.biSize = sizeof(db->bmp.bmiHeader);
db->bmp.bmiHeader.biSizeImage = db->w * db->h * 4;
db->bmp.bmiHeader.biWidth = db->w;
db->hdcbmp = CreateCompatibleDC(NULL);
if(db->hdcbmp == NULL) return false;
db->hbmp = CreateDIBSection(db->hdcbmp, &db->bmp, DIB_RGB_COLORS, (void **) &db->buf, NULL, 0);
if(db->hbmp == NULL) return false;
SelectObject(db->hdcbmp, db->hbmp);
return true;
}
static void killDrawBuf(DrawBuf *db)
{
DeleteObject(db->hbmp);
DeleteDC(db->hdcbmp);
memset(db, 0, sizeof(*db));
}
After you select the bitmap into the dc, is it possible to edit it from there (setpixel,getpixel) directly? :( For example if i was to edit a bitmaps data section i would select the data into edi and write at a offset the new color. How would i edit a bitmap selected into a memoryDC without using set/get pixel? :ashamed:
The way I do it is to draw in "gfxboard.buf", which is pretty
easy. And then use a bitblt to display on screen. This means
that you'll have to bitblt every time you have made changes
(not per pixel please, but after all changes are done).
This approach requires you to handle all (re)drawing of the
area where you want to show the image. If you'd rather have a
"normal" bitmap where windows handles the updating (good for a
bitmap you don't change very often), you should probably use
another technique.
I hate working with bitmaps under windows :)
Thanks alot the code works great!! :)