Discussion:
SelectObject() returns NULL
(too old to reply)
Peter
2005-08-17 16:58:04 UTC
Permalink
I use SelectObject() with parameter HBITMAP on memory device context.
When I pass HBITMAP of bitmap loaded from resource all is OK,
I see drawed icon on dialog surface.
When I use bitmap converted from mbm format, SElectObject() returns NULL
and nothing is drawed.
Does exist any way to acquire detailed error info - what is wrong with my
bitmap ?
Or maybe there is other error - how can I retrieve some error info ?

Peter
Kellie Fitton
2005-08-17 17:21:44 UTC
Permalink
Hi,

Well, before using the API SelectObject(), you should first call the
win32 API
GetObject(), this way, you ensure that the bitMap was loaded into the
memory,
and also you can use the API's GetLastError(), FormatMessage(), and
findOut
why the API GetObject() had failed, that should give you some clues.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_912s.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp

Hope these information helps,

Kellie.
Peter
2005-08-17 20:03:03 UTC
Permalink
Thanks,

I red GetObject() doc, in my case I can use only GetBitmapBits().
It returns 4032 bytes of bitmap bits. and GetLastError() is 0.

How can I retrieve info what is wrong ?

my bitmap is created with CreateBitmap(w, h, 1, 24, img);

Peter
Post by Kellie Fitton
Hi,
Well, before using the API SelectObject(), you should first call the
win32 API
GetObject(), this way, you ensure that the bitMap was loaded into the
memory,
and also you can use the API's GetLastError(), FormatMessage(), and
findOut
why the API GetObject() had failed, that should give you some clues.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_912s.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp
Hope these information helps,
Kellie.
Vipin
2005-08-17 20:13:37 UTC
Permalink
Your problem is this:-

You are trying to create a DDB and select into a HDC which is not compatible
to it. What is your DC's bitspixel( you can use GetDeviceCaps(hdc ,
BITSPIXEL). I am sure they are not the same, hence the failure.


To select a bitmap with arbitrary pixel depth, you got to use DIB. You need
to use CreateDIBSection(...) to create the DIB.

let me know if you need more help. But I have to sleep now. I will check
again in another 10 hours.

Thanks
Vipin
Post by Peter
Thanks,
I red GetObject() doc, in my case I can use only GetBitmapBits().
It returns 4032 bytes of bitmap bits. and GetLastError() is 0.
How can I retrieve info what is wrong ?
my bitmap is created with CreateBitmap(w, h, 1, 24, img);
Peter
Post by Kellie Fitton
Hi,
Well, before using the API SelectObject(), you should first call the
win32 API
GetObject(), this way, you ensure that the bitMap was loaded into the
memory,
and also you can use the API's GetLastError(), FormatMessage(), and
findOut
why the API GetObject() had failed, that should give you some clues.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_912s.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp
Post by Peter
Post by Kellie Fitton
Hope these information helps,
Kellie.
Peter
2005-08-18 08:18:03 UTC
Permalink
Yes it's true,
HDC has 23 pixel depth, but my created bitmap is 24 pixel depth.

This bitmap is created by code that extracts it from mbm format.

What functions can to help convert this 24bits/pixel to 32bits/pixel bitmap ?
I see into CreateDIBSection() doc - but I think that in my case
it's not usable - or am I mistaken ?

Peter
Post by Vipin
Your problem is this:-
You are trying to create a DDB and select into a HDC which is not compatible
to it. What is your DC's bitspixel( you can use GetDeviceCaps(hdc ,
BITSPIXEL). I am sure they are not the same, hence the failure.
To select a bitmap with arbitrary pixel depth, you got to use DIB. You need
to use CreateDIBSection(...) to create the DIB.
let me know if you need more help. But I have to sleep now. I will check
again in another 10 hours.
Thanks
Vipin
Post by Peter
Thanks,
I red GetObject() doc, in my case I can use only GetBitmapBits().
It returns 4032 bytes of bitmap bits. and GetLastError() is 0.
How can I retrieve info what is wrong ?
my bitmap is created with CreateBitmap(w, h, 1, 24, img);
Peter
Post by Kellie Fitton
Hi,
Well, before using the API SelectObject(), you should first call the
win32 API
GetObject(), this way, you ensure that the bitMap was loaded into the
memory,
and also you can use the API's GetLastError(), FormatMessage(), and
findOut
why the API GetObject() had failed, that should give you some clues.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_912s.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp
Post by Peter
Post by Kellie Fitton
Hope these information helps,
Kellie.
Peter
2005-08-18 10:25:09 UTC
Permalink
sorry - error in first sentence: HDC has 32 pixed depth, not 23 :-)
Peter
Post by Peter
Yes it's true,
HDC has 23 pixel depth, but my created bitmap is 24 pixel depth.
This bitmap is created by code that extracts it from mbm format.
What functions can to help convert this 24bits/pixel to 32bits/pixel bitmap ?
I see into CreateDIBSection() doc - but I think that in my case
it's not usable - or am I mistaken ?
Peter
Post by Vipin
Your problem is this:-
You are trying to create a DDB and select into a HDC which is not compatible
to it. What is your DC's bitspixel( you can use GetDeviceCaps(hdc ,
BITSPIXEL). I am sure they are not the same, hence the failure.
To select a bitmap with arbitrary pixel depth, you got to use DIB. You need
to use CreateDIBSection(...) to create the DIB.
let me know if you need more help. But I have to sleep now. I will check
again in another 10 hours.
Thanks
Vipin
Post by Peter
Thanks,
I red GetObject() doc, in my case I can use only GetBitmapBits().
It returns 4032 bytes of bitmap bits. and GetLastError() is 0.
How can I retrieve info what is wrong ?
my bitmap is created with CreateBitmap(w, h, 1, 24, img);
Peter
Post by Kellie Fitton
Hi,
Well, before using the API SelectObject(), you should first call the
win32 API
GetObject(), this way, you ensure that the bitMap was loaded into the
memory,
and also you can use the API's GetLastError(), FormatMessage(), and
findOut
why the API GetObject() had failed, that should give you some clues.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_912s.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp
Post by Peter
Post by Kellie Fitton
Hope these information helps,
Kellie.
Peter
2005-08-18 08:27:01 UTC
Permalink
Maybe other solution in my case is to create dialog that has HDC compatible
with
this 24bits/pixel bitmap (normally dialog has 32bits/pixel)
This is possible solution, because it's originaly aplication for smartphone
and PC version does not need better color resolution that has smartphone
application.

Is possible to create dialog with other than default HDC ?

Peter
Post by Vipin
Your problem is this:-
You are trying to create a DDB and select into a HDC which is not compatible
to it. What is your DC's bitspixel( you can use GetDeviceCaps(hdc ,
BITSPIXEL). I am sure they are not the same, hence the failure.
To select a bitmap with arbitrary pixel depth, you got to use DIB. You need
to use CreateDIBSection(...) to create the DIB.
let me know if you need more help. But I have to sleep now. I will check
again in another 10 hours.
Thanks
Vipin
Post by Peter
Thanks,
I red GetObject() doc, in my case I can use only GetBitmapBits().
It returns 4032 bytes of bitmap bits. and GetLastError() is 0.
How can I retrieve info what is wrong ?
my bitmap is created with CreateBitmap(w, h, 1, 24, img);
Peter
Post by Kellie Fitton
Hi,
Well, before using the API SelectObject(), you should first call the
win32 API
GetObject(), this way, you ensure that the bitMap was loaded into the
memory,
and also you can use the API's GetLastError(), FormatMessage(), and
findOut
why the API GetObject() had failed, that should give you some clues.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_912s.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp
Post by Peter
Post by Kellie Fitton
Hope these information helps,
Kellie.
Kellie Fitton
2005-08-17 20:28:26 UTC
Permalink
Hi,

Try to use the API CreateCompatibleBitmap(), someThing like this:

GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()

Also, instead of using GetBitmapBits(), try to replace it with the API:

GetDIBits()

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp


Kellie.
Vipin
2005-08-18 03:59:29 UTC
Permalink
The reason which I have stated is the cause of the problem.
Post by Kellie Fitton
Hi,
GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()
GetDIBits()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp
Post by Kellie Fitton
Kellie.
Peter
2005-08-18 08:19:05 UTC
Permalink
I am reading CreateCompatibleBitmap() doc.
No problem to call this function, but I have available only bitmap data that
has 24 bits/pixel and not 32 bits/pixel like my HDC compatible bitmap.

How to convert these 24bits/pixel data to my compatible bitmap ?

Peter
Post by Kellie Fitton
Hi,
GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()
GetDIBits()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp
Kellie.
DriverStarter
2005-08-18 12:45:49 UTC
Permalink
Can you use GetDiBits()/SetDiBits() ?
Post by Peter
I am reading CreateCompatibleBitmap() doc.
No problem to call this function, but I have available only bitmap data that
has 24 bits/pixel and not 32 bits/pixel like my HDC compatible bitmap.
How to convert these 24bits/pixel data to my compatible bitmap ?
Peter
Post by Kellie Fitton
Hi,
GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()
GetDIBits()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp
Kellie.
Peter
2005-08-18 13:45:03 UTC
Permalink
Goal !

At last something is visible, but picture is corrupted,
Like every next line begins one pixel to the right,
so it looks oblique, and all picture is reversed,
my code looks like this:

hBitmap24 = ::CreateBitmap(w, h, 1, 24, img);// img is 24bits/pixel image data
bmInfo = bmLib.CreateBitmapInfoStruct(this->m_hWnd, hBitmap24);
hBitmap32 = CreateCompatibleBitmap(hdc, w, h);//hdc retrieved from current
dialog
iRes = SetDIBits(hdc, hBitmap32, 1, h, img, bmInfo, DIB_RGB_COLORS);
oldObj = SelectObject(memCompDC, hBitmap32);

How to solve problem with reversed and oblique picture ?

Peter
Post by DriverStarter
Can you use GetDiBits()/SetDiBits() ?
Post by Peter
I am reading CreateCompatibleBitmap() doc.
No problem to call this function, but I have available only bitmap data that
has 24 bits/pixel and not 32 bits/pixel like my HDC compatible bitmap.
How to convert these 24bits/pixel data to my compatible bitmap ?
Peter
Post by Kellie Fitton
Hi,
GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()
GetDIBits()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp
Kellie.
DriverStarter
2005-08-18 13:52:52 UTC
Permalink
It seems you need a little bit research on BITMAPINFO!
Post by Peter
Goal !
At last something is visible, but picture is corrupted,
Like every next line begins one pixel to the right,
so it looks oblique, and all picture is reversed,
hBitmap24 = ::CreateBitmap(w, h, 1, 24, img);// img is 24bits/pixel image data
bmInfo = bmLib.CreateBitmapInfoStruct(this->m_hWnd, hBitmap24);
hBitmap32 = CreateCompatibleBitmap(hdc, w, h);//hdc retrieved from current
dialog
iRes = SetDIBits(hdc, hBitmap32, 1, h, img, bmInfo, DIB_RGB_COLORS);
oldObj = SelectObject(memCompDC, hBitmap32);
How to solve problem with reversed and oblique picture ?
Peter
Post by DriverStarter
Can you use GetDiBits()/SetDiBits() ?
Post by Peter
I am reading CreateCompatibleBitmap() doc.
No problem to call this function, but I have available only bitmap data that
has 24 bits/pixel and not 32 bits/pixel like my HDC compatible bitmap.
How to convert these 24bits/pixel data to my compatible bitmap ?
Peter
Post by Kellie Fitton
Hi,
GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()
GetDIBits()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp
Kellie.
DriverStarter
2005-08-18 14:02:37 UTC
Permalink
how about this:


SelectObject(hdc24, hbmp24);
SelectObject(hdc32, hbmp32);
BitBlt(hdc24, hdc32);
Post by Peter
Goal !
At last something is visible, but picture is corrupted,
Like every next line begins one pixel to the right,
so it looks oblique, and all picture is reversed,
hBitmap24 = ::CreateBitmap(w, h, 1, 24, img);// img is 24bits/pixel image data
bmInfo = bmLib.CreateBitmapInfoStruct(this->m_hWnd, hBitmap24);
hBitmap32 = CreateCompatibleBitmap(hdc, w, h);//hdc retrieved from current
dialog
iRes = SetDIBits(hdc, hBitmap32, 1, h, img, bmInfo, DIB_RGB_COLORS);
oldObj = SelectObject(memCompDC, hBitmap32);
How to solve problem with reversed and oblique picture ?
Peter
Post by DriverStarter
Can you use GetDiBits()/SetDiBits() ?
Post by Peter
I am reading CreateCompatibleBitmap() doc.
No problem to call this function, but I have available only bitmap data that
has 24 bits/pixel and not 32 bits/pixel like my HDC compatible bitmap.
How to convert these 24bits/pixel data to my compatible bitmap ?
Peter
Post by Kellie Fitton
Hi,
GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()
GetDIBits()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp
Kellie.
Peter
2005-08-18 14:13:07 UTC
Permalink
It's not possible.
On my PC does not exist hdc24,
On PC I have only bitmap data that are 24bits/pixel and hdc32.

Hdc24 is on smartphone - it works fine with 24bits/pixel bitmaps on
smartphone.
Peter
Post by DriverStarter
SelectObject(hdc24, hbmp24);
SelectObject(hdc32, hbmp32);
BitBlt(hdc24, hdc32);
Post by Peter
Goal !
At last something is visible, but picture is corrupted,
Like every next line begins one pixel to the right,
so it looks oblique, and all picture is reversed,
hBitmap24 = ::CreateBitmap(w, h, 1, 24, img);// img is 24bits/pixel image data
bmInfo = bmLib.CreateBitmapInfoStruct(this->m_hWnd, hBitmap24);
hBitmap32 = CreateCompatibleBitmap(hdc, w, h);//hdc retrieved from current
dialog
iRes = SetDIBits(hdc, hBitmap32, 1, h, img, bmInfo, DIB_RGB_COLORS);
oldObj = SelectObject(memCompDC, hBitmap32);
How to solve problem with reversed and oblique picture ?
Peter
Post by DriverStarter
Can you use GetDiBits()/SetDiBits() ?
Post by Peter
I am reading CreateCompatibleBitmap() doc.
No problem to call this function, but I have available only bitmap data that
has 24 bits/pixel and not 32 bits/pixel like my HDC compatible bitmap.
How to convert these 24bits/pixel data to my compatible bitmap ?
Peter
Post by Kellie Fitton
Hi,
GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()
GetDIBits()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp
Kellie.
DriverStarter
2005-08-18 14:38:45 UTC
Permalink
hdc24 = CreatecompatibleDc(hdc32);
hbmp24 = CreateDibSection(24);
SeelctObject(hdc24, hbmp24);

just for ID purpose, actually hdc24 is a compatible 32bit dc.
Post by Peter
It's not possible.
On my PC does not exist hdc24,
On PC I have only bitmap data that are 24bits/pixel and hdc32.
Hdc24 is on smartphone - it works fine with 24bits/pixel bitmaps on
smartphone.
Peter
Post by DriverStarter
SelectObject(hdc24, hbmp24);
SelectObject(hdc32, hbmp32);
BitBlt(hdc24, hdc32);
Post by Peter
Goal !
At last something is visible, but picture is corrupted,
Like every next line begins one pixel to the right,
so it looks oblique, and all picture is reversed,
hBitmap24 = ::CreateBitmap(w, h, 1, 24, img);// img is 24bits/pixel
image
data
bmInfo = bmLib.CreateBitmapInfoStruct(this->m_hWnd, hBitmap24);
hBitmap32 = CreateCompatibleBitmap(hdc, w, h);//hdc retrieved from current
dialog
iRes = SetDIBits(hdc, hBitmap32, 1, h, img, bmInfo, DIB_RGB_COLORS);
oldObj = SelectObject(memCompDC, hBitmap32);
How to solve problem with reversed and oblique picture ?
Peter
Post by DriverStarter
Can you use GetDiBits()/SetDiBits() ?
Post by Peter
I am reading CreateCompatibleBitmap() doc.
No problem to call this function, but I have available only bitmap
data
that
has 24 bits/pixel and not 32 bits/pixel like my HDC compatible bitmap.
How to convert these 24bits/pixel data to my compatible bitmap ?
Peter
Post by Kellie Fitton
Hi,
GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()
GetDIBits()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp
Kellie.
DriverStarter
2005-08-18 14:41:04 UTC
Permalink
it works on my 32-bit screen DC;
Post by Peter
It's not possible.
On my PC does not exist hdc24,
On PC I have only bitmap data that are 24bits/pixel and hdc32.
Hdc24 is on smartphone - it works fine with 24bits/pixel bitmaps on
smartphone.
Peter
Post by DriverStarter
SelectObject(hdc24, hbmp24);
SelectObject(hdc32, hbmp32);
BitBlt(hdc24, hdc32);
Post by Peter
Goal !
At last something is visible, but picture is corrupted,
Like every next line begins one pixel to the right,
so it looks oblique, and all picture is reversed,
hBitmap24 = ::CreateBitmap(w, h, 1, 24, img);// img is 24bits/pixel
image
data
bmInfo = bmLib.CreateBitmapInfoStruct(this->m_hWnd, hBitmap24);
hBitmap32 = CreateCompatibleBitmap(hdc, w, h);//hdc retrieved from current
dialog
iRes = SetDIBits(hdc, hBitmap32, 1, h, img, bmInfo, DIB_RGB_COLORS);
oldObj = SelectObject(memCompDC, hBitmap32);
How to solve problem with reversed and oblique picture ?
Peter
Post by DriverStarter
Can you use GetDiBits()/SetDiBits() ?
Post by Peter
I am reading CreateCompatibleBitmap() doc.
No problem to call this function, but I have available only bitmap
data
that
has 24 bits/pixel and not 32 bits/pixel like my HDC compatible bitmap.
How to convert these 24bits/pixel data to my compatible bitmap ?
Peter
Post by Kellie Fitton
Hi,
GetDC()
CreateCompatibleDC()
GetDeviceCaps(HORZRES)
GetDeviceCaps(VERTRES)
CreateCompatibleBitmap()
GetObject()
SelectObject()
GetDIBits()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp
Kellie.
Loading...