I need to draw text to byte array, to convert it later to DirectX11 texture.
I was trying many thinks, for example like this:
HDC hdc= GetDC( g_hWnd );
int w= 600;
int h= 450;
unsigned* buf= new unsigned [w*h];
for( int a=0;a<w*h;a++)buf[a]= 0x0;
HBITMAP hbmp= CreateBitmap( w, h, 1, 4*8, buf );
if(!hbmp)throw "error bmp";
HDC vhdc= CreateCompatibleDC( hdc );
if(!vhdc)throw "error vhdc";
SelectObject( vhdc, hbmp );
TextOut( vhdc, 0, 0, L"TEST", 4 );
But after that buf is still empty.
I need it for intro 64KB, so I can not use big libraries.
Here is another not working code that I tried:
unsigned* buf= new unsigned [w*h];
for( int a=0;a<w*h;a++)buf[a]= 0x0;
HDC vhdc= CreateCompatibleDC( hdc ); if(!vhdc)throw "vhdc is hard";
HBITMAP hbmp= CreateCompatibleBitmap( hdc, w, h );
BITMAPINFO bmi = {{sizeof(BITMAPINFOHEADER),w,-h,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0}};
SelectObject( vhdc, hbmp );
TextOut( vhdc, 0, 0, L"TEST", 4 );
BITMAPINFO bmpi;
ZeroMemory( &bmpi, sizeof(bmpi) );
//GetDIBits(vhdc, hbmp, 0, h, buf, &bmpi, NULL);
GetDIBits(vhdc, hbmp, 0, h, buf, &bmpi, BI_RGB);
I was using vhdc and hdc as GetDIBits argument, non of it work.
jlahd already have answered the question.
But I will post working code in case anybody need it.
HDC hdc= GetDC( g_hWnd ); /// g_hWnd is my windows handle type HWND
int w= 1024;
int h= 768;
unsigned* buf= new unsigned [w*h];
HDC vhdc= CreateCompatibleDC( hdc ); if(!vhdc)throw "error with vhdc";
HBITMAP hbmp= CreateCompatibleBitmap( hdc, w, h );
BITMAPINFO bmpi = {{sizeof(BITMAPINFOHEADER),w,-h,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0}};
SelectObject( vhdc, hbmp );
TextOut( vhdc, 10, 10, L"HELLO WORLD", 11 );
GetDIBits(vhdc, hbmp, 0, h, buf, &bmpi, BI_RGB);
After that code buf store data with image with "HELLO WORLD" drawed on it.
CreateBitmap only uses the given data as input. The buffer is not updated when you draw on it. You should use CreateDIBSection instead.
Related
how to flip a HBITMAP horizontally?As an option, I thought to get an array of colors from BITMAP and write them to another BITMAP, but somehow it's too busy. Are there built-in functions or other options to do this?
You can use StretchBlt with a negative dimension as below:
HBITMAP FlipBitmapHorizontally(HBITMAP hbm) {
BITMAP bm;
GetObject(hbm, sizeof(BITMAP), &bm);
int wd = bm.bmWidth;
int hgt = bm.bmHeight;
HDC hdcScr = GetDC(NULL);
HDC hdcFlipped = CreateCompatibleDC(hdcScr);
HBITMAP hbmFlipped = CreateCompatibleBitmap(hdcScr, wd, hgt);
HGDIOBJ oldFlipped = SelectObject(hdcFlipped, hbmFlipped);
HDC hdcSrc = CreateCompatibleDC(hdcScr);
HGDIOBJ oldSrc = SelectObject(hdcSrc, hbm);
StretchBlt(hdcFlipped, wd, 0, -wd, hgt, hdcSrc, 0, 0, wd, hgt, SRCCOPY);
SelectObject(hdcSrc, oldSrc);
DeleteDC(hdcSrc);
SelectObject(hdcFlipped, oldFlipped);
DeleteDC(hdcFlipped);
ReleaseDC(NULL, hdcScr);
return hbmFlipped;
}
I'm creating a program using the Win32 API, and I need to create a new bitmap filled with one color and with given dimensions.
Here is my code:
m_hBitmap =( HBITMAP ) CreateCompatibleBitmap(hDC, iWidth, iHeight);
HDC hDCn = CreateCompatibleDC( hDC );
SelectObject( hDCn, m_hBitmap );
ExtFloodFill(hDCn, 0, 0, crColor, FLOODFILLSURFACE);
DeleteDC( hDCn );
The bitmap dimensions match, but the bitmap is always black regardless of the crColor parameter.
Use FillRect() instead of ExtFloodFill().
Also, you need to de-select the bitmap before you delete the DC, otherwise the original bitmap created and selected into the DC by CreateCompatibleDC() will be leaked.
Try this:
m_hBitmap = CreateCompatibleBitmap(hDC, iWidth, iHeight);
HDC hDCn = CreateCompatibleDC(hDC);
HBITMAP hOld = (HBITMAP) SelectObject(hDCn, m_hBitmap); // <-- SAVE OLD BITMAP!
//ExtFloodFill(hDCn, 0, 0, crColor, FLOODFILLSURFACE);
RECT r;
r.left = r.top = 0;
r.right = iWidth;
r.bottom = iHeight;
HBRUSH hBrush = CreateSolidBrush(crColor);
FillRect(hDCn, &r, hBrush);
DeleteObject(hBrush);
SelectObject(hDCn, hOld); // <-- RESTORE OLD BITMAP
DeleteDC(hDCn);
So i've been trying to rescale a bitmap without it printing the original and reprinting the rescaled image. I'm trying to use StretchBlt(), based on the MSDN Microsoft rescaling images function:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd162950(v=vs.85).aspx
but that requires a secondary hdc tied to the Source, which stretching can't be done without printing the HBITMAP first. Is there a way to convert HBITMAP's into HDC's? I have been able to get HANDLE's out of HBITMAP's, which might provide a more direct route. The other thing i could do is create a resized bitmap in allocated memory (not saved) out of the standard bitmap and print that.
The standard way i print bitmaps is:
HBITMAP hBitmap;
static HANDLE hDIB = NULL;
CHAR szFileName[MAX_PATH] = "fileName.bmp";
hDIB = OpenDIB((LPSTR)szFileName);
hBitmap = BitmapFromDIB(hDIB, NULL);
DrawBitmap(hdc, x, y, hBitmap, SRCCOPY);
Another option i could try is to look into another means of displaying the bmp. I'm pretty new to win32, so I don't know any other means of accomplishing this task. Any insight into how i can rescale the BITMAP without printing it in the first place.
The link you posted (Scaling an Image) already contains code, that renders a bitmap. All you need to do is replace the call to BitBlt with StretchBlt:
BOOL DrawBitmap (HDC hDC, INT x, INT y, INT width, INT height, HBITMAP hBitmap, DWORD dwROP)
{
HDC hDCBits;
BITMAP Bitmap;
BOOL bResult;
if (!hDC || !hBitmap)
return FALSE;
hDCBits = CreateCompatibleDC(hDC);
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
SelectObject(hDCBits, hBitmap);
// Replace with StretchBlt call
//bResult = BitBlt(hDC, x, y, Bitmap.bmWidth, Bitmap.bmHeight, hDCBits, 0, 0, dwROP);
bResult = StretchBlt(hDC, x, y, width, height,
hDCBits, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, dwROP);
DeleteDC(hDCBits);
return bResult;
}
You can call this from your WM_PAINT message handler, for example:
case WM_PAINT:
{
PAINTSTRUCT ps = { 0 };
HDC hDC = ::BeginPaint( hWnd, &ps );
RECT rc = { 0 };
::GetClientRect( hWnd, &rc );
DrawBitmap( hDC, 0, 0, rc.right, rc.bottom, hBitmap, SRCCOPY );
::EndPaint( hWnd, &ps );
}
break;
I am trying to draw plot using matrix, where each element is DWORD value, that represent pixel ARGB value. Idea behind the application is that one separate thread do the calculation (mat. formula), whose result will be the matrix with ARGB values. When WM_PAINT is raised I will use function to draw, using the filled matrix:
void DrawImage( HDC hDC, WORD wWidth, WORD wHeight )
{
HBITMAP hBitmap;
HDC hMemDC;
BITMAPINFO bi;
int iSize = sizeof( BITMAPINFO );
memcpy( &bi, dwBytes + 3, iSize);
hBitmap = CreateCompatibleBitmap(hDC, wWidth, wHeight);
hMemDC = CreateCompatibleDC( hDC );
if ( 0 == SetDIBits( hDC, hBitmap, 0,
wHeight, dwBytes, &bi, DIB_RGB_COLORS ) )
{
// error MSDN http://msdn.microsoft.com/en-us/library/
//windows/desktop/dd162973%28v=vs.85%29.aspx
}
hBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
BitBlt(hDC, 0, 0, wWidth, wHeight, hMemDC, 0, 0, SRCCOPY);
DeleteObject(SelectObject(hMemDC, hBitmap));
DeleteDC(hMemDC);
}
so, when WM_PAINT is raised:
case WM_PAINT:
{
PAINTSTRUCT ps = { 0 };
HDC hDC = BeginPaint( hWnd, &ps );
DrawImage( hDC, iWidth, iHeight );
EndPaint( hWnd, &ps );
return 0L;
}
but nothing happens. The area is black. When SetDIBits returns 0 it means that "One or more of the input parameters is invalid" according to MSDN. I am out of ideas..
sizeof( BITMAPINFO ) includes only 1 pixel. BITMAPINFO is a variable-length structure so you need to compute its size dynamically and allocate sufficient memory dynamically.
You pointed me to investigate handling BITMAPINFO structure, and there it was. After I have changed one plane, 32 bits per pixel and width 800 and height 600 - it worked!
void DrawImage( HDC hDC, WORD wWidth, WORD wHeight )
{
HDC hMemDC;
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biHeight = 600;
bmi.bmiHeader.biWidth = 800;
bmi.bmiHeader.biPlanes = 1;
hMemDC = CreateCompatibleDC( hDC );
HBITMAP hBitmap = CreateDIBSection( hMemDC, &bmi, DIB_RGB_COLORS,
(void**) &dwBytes, NULL, 0);
SetDIBits( hDC, hBitmap, 0, wHeight, dwBytes, &bmi, DIB_RGB_COLORS );
HBITMAP hOldBitmap = (HBITMAP) SelectObject( hMemDC, hBitmap );
BitBlt(hDC, 0, 0, wWidth, wHeight, hMemDC, 0, 0, SRCCOPY);
SelectObject( hMemDC, hBitmap );
DeleteObject( hBitmap );
DeleteDC(hMemDC);
}
I got proper result:
I have a question similar to this, How to display text in system tray icon with win32 API?
I tried his solution but it's not working for me. I get a small 4x16 white image as the system icon instead of text and I can't understand why.
I'm not using MFC/.NET just win32 api.
void UpdateIcon(HWND hWnd){
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 100;
nid.hIcon = CreateSmallIcon(hWnd);
nid.uFlags = NIF_ICON;
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
HICON CreateSmallIcon( HWND hWnd )
{
static TCHAR *szText = TEXT ( "100" );
HDC hdc, hdcMem;
HBITMAP hBitmap = NULL;
HBITMAP hOldBitMap = NULL;
HBITMAP hBitmapMask = NULL;
ICONINFO iconInfo;
HFONT hFont;
HICON hIcon;
hdc = GetDC ( hWnd );
hdcMem = CreateCompatibleDC ( hdc );
hBitmap = CreateCompatibleBitmap ( hdc, 16, 16 );
hBitmapMask = CreateCompatibleBitmap ( hdc, 16, 16 );
ReleaseDC ( hWnd, hdc );
hOldBitMap = (HBITMAP) SelectObject ( hdcMem, hBitmap );
PatBlt ( hdcMem, 0, 0, 16, 16, WHITENESS );
// Draw percentage
hFont = CreateFont (12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
TEXT ("Arial"));
hFont = (HFONT) SelectObject ( hdcMem, hFont );
TextOut ( hdcMem, 0, 0, szText, lstrlen (szText) );
SelectObject ( hdc, hOldBitMap );
hOldBitMap = NULL;
iconInfo.fIcon = TRUE;
iconInfo.xHotspot = 0;
iconInfo.yHotspot = 0;
iconInfo.hbmMask = hBitmapMask;
iconInfo.hbmColor = hBitmap;
hIcon = CreateIconIndirect ( &iconInfo );
DeleteObject ( SelectObject ( hdcMem, hFont ) );
DeleteDC ( hdcMem );
DeleteDC ( hdc );
DeleteObject ( hBitmap );
DeleteObject ( hBitmapMask );
return hIcon;
}
I don't have windows installed currently so i cannot check if this will work better, but i found potential problem - from MSDN documentation of CreateIconIndirect function:
The application must continue to manage the original bitmaps and delete them when they are no longer necessary.
Seems like you are deleting bitmaps too soon.
You need to set background and possibly foreground colors:
SetTextColor( hdcMem, 0x00FF0000 ); // 0x00bbggrr, not rrggbb !!
SetBkMode( hdcMem, TRANSPARENT ); // VERY IMPORTANT
I think DeleteDC ( hdc ); is not needed here as you used GetDC().