绘制8bpp灰度位图(非托管C ++) [英] Drawing on 8bpp grayscale bitmap (unmanaged C++)
问题描述
我一直在尝试绘制一个8bpp的灰度位图没有成功。这里是我的一些尝试。也许有人可以指出我做错了什么。
I've been attempting to draw on an 8bpp grayscale bitmap without success. Here are some of my attempts. Maybe someone can point out what I'm doing wrong.
========================= ==========================
===================================================
尝试1:创建,选择,并绘制:
Attempt 1: Create, select, and draw:
在构造函数中:
CBitmap bm;
bm.CreateBitmap (200, 200, 1, 8, NULL);
在OnDraw中:
CDC *mdc=new CDC ();
HGDIOBJ tmp = mdc->SelectObject(bm);
结果:tmp为NULL,表示失败。
Result: tmp is NULL, indicating failure.
=========================================== ====
===================================================
尝试2:创建DIBSection
Attempt 2: CreateDIBSection
在构造函数中:
HBITMAP hbm;
BITMAPINFOHEADER bih;
BITMAPINFO bi;
HANDLE hb;
CDC* myDc = new CDC ();
HDC hdc = myDc->GetSafeHdc ();
void* bits;
RGBQUAD rq [256];
initBi ();
hbm = CreateDIBSection (hdc, &bi, DIB_RGB_COLORS, &bits, NULL, 0);
...
void CEightBitDrawingView::initBi()
{
bih.biSize = sizeof (BITMAPINFOHEADER);
bih.biWidth = 200;
bih.biHeight = -200;
bih.biPlanes = 1;
bih.biBitCount = 8;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 14173;
bih.biYPelsPerMeter = 14173;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
memset ((void *) rq, 0, 256 * sizeof (RGBQUAD));
bi.bmiHeader = bih;
bi.bmiColors = rq;
}
结果:这甚至不能编译,因为BITMAPINFO bmiColors成员被定义为:
RGBQUAD bmiColors [1];
Result: This doesn't even compile because the BITMAPINFO bmiColors member is defined as: RGBQUAD bmiColors[1];
因此不会接受多于一种RGB颜色。事实上,没有我分配给这个成员编译! (他们可能会使它更复杂吗?)
so won't accept more than one RGB color. In fact, nothing I assign to this member compiles! (Could they possibly make it any more complex!?)
任何建议将不胜感激!谢谢!
Any suggestions would be appreciated! Thanks!
================================= ==================
===================================================
推荐答案
这里。代码,演示如何在一个不受管理的世界中 - 在堆栈上分配动态大小的结构,填充并传递给CreateDIBSection。
Here. Code that demonstrates how to - in a not managed world - allocate a dynamically sized structure on the stack, fill it in and pass it to CreateDIBSection.
#include <malloc.h>
HBITMAP CreateGreyscaleBitmap(int cx, int cy)
{
BITMAPINFO* pbmi = (BITMAPINFO*)alloca( sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256);
pbmi->bmiHeader.biSize = sizeof (pbmi->bmiHeader);
pbmi->bmiHeader.biWidth = cx;
pbmi->bmiHeader.biHeight = cy;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 8;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = 0;
pbmi->bmiHeader.biXPelsPerMeter = 14173;
pbmi->bmiHeader.biYPelsPerMeter = 14173;
pbmi->bmiHeader.biClrUsed = 0;
pbmi->bmiHeader.biClrImportant = 0;
for(int i=0; i<256; i++)
{
pbmi->bmiColors[i].rgbRed = i;
pbmi->bmiColors[i].rgbGreen = i;
pbmi->bmiColors[i].rgbBlue = i;
pbmi->bmiColors[i].rgbReserved = 0;
}
PVOID pv;
return CreateDIBSection(NULL,pbmi,DIB_RGB_COLORS,&pv,NULL,0);
}
这篇关于绘制8bpp灰度位图(非托管C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!