Drawing on 8bpp grayscale bitmap (unmanaged)
-
Hi all, 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. =================================================== Attempt 1: Create, select, and draw: In constructor:
CBitmap bm; bm.CreateBitmap (200, 200, 1, 8, NULL);
In OnDraw:
CDC \*mdc=new CDC (); HGDIOBJ tmp = mdc->SelectObject(bm);
Result: tmp is NULL, indicating failure. =================================================== Attempt 2: CreateDIBSection In constructor:
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;
}
Result: This doesn't even compile because the BITMAPINFO bmiColors member is defined as:
RGBQUAD bmiColors\[1\];
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! ===================================================
-
Hi all, 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. =================================================== Attempt 1: Create, select, and draw: In constructor:
CBitmap bm; bm.CreateBitmap (200, 200, 1, 8, NULL);
In OnDraw:
CDC \*mdc=new CDC (); HGDIOBJ tmp = mdc->SelectObject(bm);
Result: tmp is NULL, indicating failure. =================================================== Attempt 2: CreateDIBSection In constructor:
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;
}
Result: This doesn't even compile because the BITMAPINFO bmiColors member is defined as:
RGBQUAD bmiColors\[1\];
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! ===================================================
Alan Balkany wrote:
CDC *mdc=new CDC (); HGDIOBJ tmp = mdc->SelectObject(bm);
- do you create the DC anywhere or you just instantiate it? You need to create the DC, a CDC instance alone isn't enough. Where does
bm
come from? Acording to what you saidbm
would be a local variable in the constructor, so how would it get to OnDraw? About the RGBQUAD thing:Alan Balkany wrote:
RGBQUAD bmiColors[1];
this means that an array of RGBQUADs containing a variable number of colors will follow directly the BITMAPINFO structure. I think the
biClrUsed
member would indicate this in the struct (see the documentation for the structure). You have to allocate enough memory to contain the bitmap info struct AND the palette after it, you can set the members in the palette -if you like- thorough thebmiColors
array. Something like:BITMAPINFO *bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 255);
...fill the bmiHeader ...
bmi->bmiCOlors[0] = firstColor;
bmi->bmiCOlors[1] = secondColor;
...> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
-
Alan Balkany wrote:
CDC *mdc=new CDC (); HGDIOBJ tmp = mdc->SelectObject(bm);
- do you create the DC anywhere or you just instantiate it? You need to create the DC, a CDC instance alone isn't enough. Where does
bm
come from? Acording to what you saidbm
would be a local variable in the constructor, so how would it get to OnDraw? About the RGBQUAD thing:Alan Balkany wrote:
RGBQUAD bmiColors[1];
this means that an array of RGBQUADs containing a variable number of colors will follow directly the BITMAPINFO structure. I think the
biClrUsed
member would indicate this in the struct (see the documentation for the structure). You have to allocate enough memory to contain the bitmap info struct AND the palette after it, you can set the members in the palette -if you like- thorough thebmiColors
array. Something like:BITMAPINFO *bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 255);
...fill the bmiHeader ...
bmi->bmiCOlors[0] = firstColor;
bmi->bmiCOlors[1] = secondColor;
...> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
Code-o-mat, thanks for clearing up the mystery about bmiColors! I just created a new DC because (it appears that) existing DCs will default to the color depth of my display. If this isn't correct, what's the right way to obtain a DC to draw on an 8bpp bitmap? (The bitmap is actually declared in the class, and was just included in the constructor's code to show the type.)
-
Code-o-mat, thanks for clearing up the mystery about bmiColors! I just created a new DC because (it appears that) existing DCs will default to the color depth of my display. If this isn't correct, what's the right way to obtain a DC to draw on an 8bpp bitmap? (The bitmap is actually declared in the class, and was just included in the constructor's code to show the type.)
I needed to do this some time ago and as far as i can remember i went the DIB section way myself, creating a palette that went from 0,0,0 to 255,255,255. If you create a screen-compatible DC and then select your bitmap into it i think you should be able to draw on it.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
-
I needed to do this some time ago and as far as i can remember i went the DIB section way myself, creating a palette that went from 0,0,0 to 255,255,255. If you create a screen-compatible DC and then select your bitmap into it i think you should be able to draw on it.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
Thanks for the help, Code-o-mat! I can now generate bitmaps, but they're coming out incorrectly. Short version: I created an 8bpp bitmap, then I BitBlt it to the screen and save it into a file. The copy on the screen has color, which suggests it's not just 8bpp. The version saved to the file is 8bpp, but it's totally black, where it should have at least *some* of the drawing. Question: How can I draw on an 8bpp grayscale bitmap and save it to a file? Can anyone see what I'm doing wrong? ======================================================== The Details Declarations:
CBitmap bm;
HBITMAP hbm;
BITMAPINFO* bi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256);
BITMAPINFOHEADER bih;Constructor:
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;bi->bmiHeader = bih; for (int i = 0; i < 256; i++) { bi->bmiColors \[i\].rgbRed = i; bi->bmiColors \[i\].rgbGreen = i; bi->bmiColors \[i\].rgbBlue = i; }
}
OnDraw:
void CEightBitDrawingView::OnDraw(CDC* pDC/*pDC*/)
{CClientDC dc(this); CDC \*mdc = GetDC (); HGDIOBJ tmp = mdc->SelectObject(hbm); mdc->FloodFill (0, 0, RGB (255, 255, 255)); // Draw box: CBrush\* br = new CBrush (RGB (255, 0, 255)); CRect rect (30, 60, 130, 160); mdc->FillRect (&rect, br); mdc->TextOutW (50, 50, L"Testing..."); CImage image; //image.Attach ((HBITMAP)bm.m\_hObject); image.Attach (hbm); HRESULT hr = image.Save(\_T("C:\\\\test.tif"), Gdiplus::ImageFormatTIFF); GetClientRect(&rect); dc.BitBlt(0,0,rect.right,rect.bottom, mdc, 0,0,SRCCOPY); mdc->SelectObject (tmp); delete br;
}
-
Thanks for the help, Code-o-mat! I can now generate bitmaps, but they're coming out incorrectly. Short version: I created an 8bpp bitmap, then I BitBlt it to the screen and save it into a file. The copy on the screen has color, which suggests it's not just 8bpp. The version saved to the file is 8bpp, but it's totally black, where it should have at least *some* of the drawing. Question: How can I draw on an 8bpp grayscale bitmap and save it to a file? Can anyone see what I'm doing wrong? ======================================================== The Details Declarations:
CBitmap bm;
HBITMAP hbm;
BITMAPINFO* bi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256);
BITMAPINFOHEADER bih;Constructor:
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;bi->bmiHeader = bih; for (int i = 0; i < 256; i++) { bi->bmiColors \[i\].rgbRed = i; bi->bmiColors \[i\].rgbGreen = i; bi->bmiColors \[i\].rgbBlue = i; }
}
OnDraw:
void CEightBitDrawingView::OnDraw(CDC* pDC/*pDC*/)
{CClientDC dc(this); CDC \*mdc = GetDC (); HGDIOBJ tmp = mdc->SelectObject(hbm); mdc->FloodFill (0, 0, RGB (255, 255, 255)); // Draw box: CBrush\* br = new CBrush (RGB (255, 0, 255)); CRect rect (30, 60, 130, 160); mdc->FillRect (&rect, br); mdc->TextOutW (50, 50, L"Testing..."); CImage image; //image.Attach ((HBITMAP)bm.m\_hObject); image.Attach (hbm); HRESULT hr = image.Save(\_T("C:\\\\test.tif"), Gdiplus::ImageFormatTIFF); GetClientRect(&rect); dc.BitBlt(0,0,rect.right,rect.bottom, mdc, 0,0,SRCCOPY); mdc->SelectObject (tmp); delete br;
}
Am not sure where you are going wrong, i did a quick proggie to check and it produced a correct bitmap:
BITMAPINFO *bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
BITMAPINFOHEADER &bih(bmi->bmiHeader);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;for (int I = 0; I < 255; I++)
{
bmi->bmiColors[I].rgbBlue = bmi->bmiColors[I].rgbGreen = bmi->bmiColors[I].rgbRed = (BYTE)I;
bmi->bmiColors[I].rgbReserved = 0;
}void *Pixels;
HBITMAP hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &Pixels, NULL, 0);//fill the bitmap so it wil display a gradient to check if the palette is correctly used...
BYTE *Pix = (BYTE *)Pixels;
for (int Y = 0; Y < abs(bih.biHeight); Y++)
for (int X = 0; X < bih.biWidth; X++, Pix++)
*Pix = (BYTE)Y;free(bmi);
CImage Image;
Image.Attach(hbmp);
Image.Save(_T("r:\\test.bmp"), Gdiplus::ImageFormatBMP);DeleteObject(hbmp);
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
-
Thanks for the help, Code-o-mat! I can now generate bitmaps, but they're coming out incorrectly. Short version: I created an 8bpp bitmap, then I BitBlt it to the screen and save it into a file. The copy on the screen has color, which suggests it's not just 8bpp. The version saved to the file is 8bpp, but it's totally black, where it should have at least *some* of the drawing. Question: How can I draw on an 8bpp grayscale bitmap and save it to a file? Can anyone see what I'm doing wrong? ======================================================== The Details Declarations:
CBitmap bm;
HBITMAP hbm;
BITMAPINFO* bi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256);
BITMAPINFOHEADER bih;Constructor:
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;bi->bmiHeader = bih; for (int i = 0; i < 256; i++) { bi->bmiColors \[i\].rgbRed = i; bi->bmiColors \[i\].rgbGreen = i; bi->bmiColors \[i\].rgbBlue = i; }
}
OnDraw:
void CEightBitDrawingView::OnDraw(CDC* pDC/*pDC*/)
{CClientDC dc(this); CDC \*mdc = GetDC (); HGDIOBJ tmp = mdc->SelectObject(hbm); mdc->FloodFill (0, 0, RGB (255, 255, 255)); // Draw box: CBrush\* br = new CBrush (RGB (255, 0, 255)); CRect rect (30, 60, 130, 160); mdc->FillRect (&rect, br); mdc->TextOutW (50, 50, L"Testing..."); CImage image; //image.Attach ((HBITMAP)bm.m\_hObject); image.Attach (hbm); HRESULT hr = image.Save(\_T("C:\\\\test.tif"), Gdiplus::ImageFormatTIFF); GetClientRect(&rect); dc.BitBlt(0,0,rect.right,rect.bottom, mdc, 0,0,SRCCOPY); mdc->SelectObject (tmp); delete br;
}
I also added this:
HDC dc = ::GetDC(NULL);
HDC memdc = CreateCompatibleDC(dc);
::ReleaseDC(NULL, dc);HGDIOBJ Ofont = ::SelectObject(memdc, GetStockObject(DEFAULT_GUI_FONT));
HGDIOBJ Obmp = ::SelectObject(memdc, hbmp);::SetTextColor(memdc, RGB(128, 128, 128));
::SetBkColor(memdc, RGB(64, 64, 64));
::TextOutA(memdc, 2, 2, "Hello world", 11);::SelectObject(memdc, Ofont);
::SelectObject(memdc, Obmp);
DeleteDC(dc);between
free(bmi);
andCImage Image;
, this works fine too, i get the text in grays.> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
-
I also added this:
HDC dc = ::GetDC(NULL);
HDC memdc = CreateCompatibleDC(dc);
::ReleaseDC(NULL, dc);HGDIOBJ Ofont = ::SelectObject(memdc, GetStockObject(DEFAULT_GUI_FONT));
HGDIOBJ Obmp = ::SelectObject(memdc, hbmp);::SetTextColor(memdc, RGB(128, 128, 128));
::SetBkColor(memdc, RGB(64, 64, 64));
::TextOutA(memdc, 2, 2, "Hello world", 11);::SelectObject(memdc, Ofont);
::SelectObject(memdc, Obmp);
DeleteDC(dc);between
free(bmi);
andCImage Image;
, this works fine too, i get the text in grays.> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
Thanks again Code-o-mat! This gives me a lot to experiment with!
-
Thanks again Code-o-mat! This gives me a lot to experiment with!
No probs, i hope it helps, good luck.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
-
I also added this:
HDC dc = ::GetDC(NULL);
HDC memdc = CreateCompatibleDC(dc);
::ReleaseDC(NULL, dc);HGDIOBJ Ofont = ::SelectObject(memdc, GetStockObject(DEFAULT_GUI_FONT));
HGDIOBJ Obmp = ::SelectObject(memdc, hbmp);::SetTextColor(memdc, RGB(128, 128, 128));
::SetBkColor(memdc, RGB(64, 64, 64));
::TextOutA(memdc, 2, 2, "Hello world", 11);::SelectObject(memdc, Ofont);
::SelectObject(memdc, Obmp);
DeleteDC(dc);between
free(bmi);
andCImage Image;
, this works fine too, i get the text in grays.> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
It worked!! Thanks so much! A big 5 points for you! (I'd give you more if I could.)
-
It worked!! Thanks so much! A big 5 points for you! (I'd give you more if I could.)
Great :thumbsup:, glad i could help, thanks for the points. :)
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <