Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 Discussions

Attempted us of CreateDIBSection not working

michael_green
Beginner
1,252 Views
Hi All,

I am making my first attempt to use CreateDIBSection, more or less basing it on Charles Petzold's example in Programming Windows. Here are the relevant sections of my code:

type (t_bitmapinfoheader) bmih
type (t_bitmapinfo) m_bmi
byte mgb
pointer (pmgb,mgb)
.
.
call ZeroMemory(loc(m_bmi),sizeof(bmih))

m_bmi%bmiHeader%biSize = sizeof(bmih)
m_bmi%bmiHeader%biWidth = 200
m_bmi%bmiHeader%biHeight = 200
m_bmi%bmiHeader%biPlanes = 1
m_bmi%bmiHeader%biBitCount = 24
m_bmi%bmiHeader%biCompression = BI_RGB
m_bmi%bmiHeader%biSizeImage = 0
m_bmi%bmiHeader%biXPelsPerMeter = 0
m_bmi%bmiHeader%biYPelsPerMeter = 0
m_bmi%bmiHeader%biClrUsed = 0
m_bmi%bmiHeader%biClrImportant = 0

hWndTiffBM = CreateDIBSection(hdc,m_bmi,0,loc(pmgb),NULL,0)
write(errcode,'(I6)')GetLastError()

The errcode always returns 8, which converts to "Not enough storage is available to process this command."

Please can someone tell me what's going on here? I find it very hard to generalise from the examples I can find, almost all of which are in C - whatI long for is an example in Fortran. Any help greatly appreciated.

Many thanks in advance,
Mike
0 Kudos
7 Replies
anthonyrichards
New Contributor III
1,252 Views
Quoting - michaelgreen
Hi All,

I am making my first attempt to use CreateDIBSection, more or less basing it on Charles Petzold's example in Programming Windows. Here are the relevant sections of my code:

type (t_bitmapinfoheader) bmih
type (t_bitmapinfo) m_bmi
byte mgb
pointer (pmgb,mgb)
.
.
call ZeroMemory(loc(m_bmi),sizeof(bmih))

m_bmi%bmiHeader%biSize = sizeof(bmih)
m_bmi%bmiHeader%biWidth = 200
m_bmi%bmiHeader%biHeight = 200
m_bmi%bmiHeader%biPlanes = 1
m_bmi%bmiHeader%biBitCount = 24
m_bmi%bmiHeader%biCompression = BI_RGB
m_bmi%bmiHeader%biSizeImage = 0
m_bmi%bmiHeader%biXPelsPerMeter = 0
m_bmi%bmiHeader%biYPelsPerMeter = 0
m_bmi%bmiHeader%biClrUsed = 0
m_bmi%bmiHeader%biClrImportant = 0

hWndTiffBM = CreateDIBSection(hdc,m_bmi,0,loc(pmgb),NULL,0)
write(errcode,'(I6)')GetLastError()

The errcode always returns 8, which converts to "Not enough storage is available to process this command."

Please can someone tell me what's going on here? I find it very hard to generalise from the examples I can find, almost all of which are in C - whatI long for is an example in Fortran. Any help greatly appreciated.

Many thanks in advance,
Mike

Make sure you use hdc=CreateCompatibleDC(0) to get a valid device context handle.
Then try the CreateDIBsection call with zeros as the last four arguments.
0 Kudos
Paul_Curtis
Valued Contributor I
1,252 Views

1. declare mgb as INTEGER(1), and pmgb as INTEGER(INT_PTR_KIND()) or similar
2. I don't think you want LOC(pmgb) which is too many levels of indirection, just LOC(mgb).
0 Kudos
michael_green
Beginner
1,252 Views
Quoting - Paul Curtis

1. declare mgb as INTEGER(1), and pmgb as INTEGER(INT_PTR_KIND()) or similar
2. I don't think you want LOC(pmgb) which is too many levels of indirection, just LOC(mgb).

Hi Guys, thanks for replies.

I tried every combination of suggestions and none worked. But what I did find was a problem with the statement:

m_bmi%bmiHeader%biBitCount = 24

If I make the value 0, GetLastError returns a 0 errcode, but the handle returned by CreateDIBSection is null. All other values for the BitCount generate an 8 for the errcode.

According to the documentation, the 4th argument should be a pointer to a pointer, which is why I used LOC(pmgb). I tried LOC(mgb) but that made no difference.

I have been using CreateCompatibleBitmap successfully for some time to display TIFF images. The trouble with this is my users want to display bigger images and/or multiple images and CreateCompatibleBitmap soon uses up all available resources. From what I have read CreateDIBSection may be the way to go, but I can't even get past first base with it - even Petzold says it's a weird function. Any further suggestions greatly appreciated.

Many thanks
Mike
0 Kudos
GVautier
New Contributor III
1,252 Views
Hello

I think you made a mistake about ppvbits argument of CreateDibSection. This argument is "a pointer to a variable that receives a pointer to the location of the DIB bit values".

See http://msdn.microsoft.com/en-us/library/dd183494%28VS.85%29.aspx

So I will write :

type (t_bitmapinfo) m_bmi
integer ptr_mgb
.
.
call ZeroMemory(loc(m_bmi%bmiHeader),sizeof(m_bmi%bmiHeader))

m_bmi%bmiHeader%biSize = sizeof(m_bmi%bmiHeader)
m_bmi%bmiHeader%biWidth = 200
m_bmi%bmiHeader%biHeight = 200
m_bmi%bmiHeader%biPlanes = 1
m_bmi%bmiHeader%biBitCount = 24
m_bmi%bmiHeader%biCompression = BI_RGB

hWndTiffBM = CreateDIBSection(hdc,m_bmi,0,ptr_mgb,NULL,0)

After the return of he function, ptr_mgb will contain the value of the pointer to the location of the DIB bit values.
0 Kudos
michael_green
Beginner
1,252 Views
Quoting - gvautier
Hello

I think you made a mistake about ppvbits argument of CreateDibSection. This argument is "a pointer to a variable that receives a pointer to the location of the DIB bit values".

See http://msdn.microsoft.com/en-us/library/dd183494%28VS.85%29.aspx

So I will write :

type (t_bitmapinfo) m_bmi
integer ptr_mgb
.
.
call ZeroMemory(loc(m_bmi%bmiHeader),sizeof(m_bmi%bmiHeader))

m_bmi%bmiHeader%biSize = sizeof(bmih)
m_bmi%bmiHeader%biWidth = 200
m_bmi%bmiHeader%biHeight = 200
m_bmi%bmiHeader%biPlanes = 1
m_bmi%bmiHeader%biBitCount = 24
m_bmi%bmiHeader%biCompression = BI_RGB

hWndTiffBM = CreateDIBSection(hdc,m_bmi,0,ptr_mgb,NULL,0)

After the return of he function, ptr_mgb will contain the value of the pointer to the location of the DIB bit values.

Thanks very much for the suggestion. What you say makes sense. I tried it, it compiles OK, but the error code is still 8. Any more ideas anyone?

Many thanks
Mike
0 Kudos
anthonyrichards
New Contributor III
1,252 Views
Quoting - michaelgreen

Thanks very much for the suggestion. What you say makes sense. I tried it, it compiles OK, but the error code is still 8. Any more ideas anyone?

Many thanks
Mike

Your original code produces a handle value that makes sense (it is not FALSE, NULL, 0xFFFFFFFF, or 1), and the pointer value appears OK as well. Maybe it will work anyway!
0 Kudos
GVautier
New Contributor III
1,252 Views
Quoting - anthonyrichards

Your original code produces a handle value that makes sense (it is not FALSE, NULL, 0xFFFFFFFF, or 1), and the pointer value appears OK as well. Maybe it will work anyway!

Effectively, the GetLastError() return value must be considered only if the called function fails and set an error return value in that case a NUL value.
0 Kudos
Reply