Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2006

Tip: Looking for answers? Try searching our database.

DrawDibDraw displays nothing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ulillillia - 17 Dec 2006 22:24 GMT
My code compiles without any errors and the only warnings are irrelevant to
the function used to load and display the image with.  The file loads
properly, but DrawDibDraw isn't displaying anything at all.  What's wrong
with it?  The BMP image itself should be very easily detected - it's black
with colored stars behind very colorful 3D text that has realistic lighting.  
Why doesn't it display the image, not a single pixel of it?  The
"window_size_base" values are the interior (white) space of the window, which
is 640 pixels wide and 480 high.  The image itself is 512x384, both multiples
of 4 and thus no extra zeros are needed (spacers).  There should be 64 pixels
on the left and right edges of white space and 48 from the top and bottom.  
This is the area where I've started the drawing part:

[code]
FILE *file_handle; // handle for reading/writing files
char bmp_image[589824]; // data for the bitmap image // 589878 bytes is the
file size
char error_msg[256]; // string for indicating errors

HDRAWDIB img_handle; // pointer for a DrawDib handle
HWND hwnd; // window handle
HDC HDC_handle; // pointer for HDC handle
WNDCLASSEX windowClass; // for a window class
MSG msg; // a message for windows

LPBITMAPINFOHEADER bmp_head_pointer;
LPVOID bmp_image_pointer;
BITMAPFILEHEADER bmp_filehead;
BITMAPINFOHEADER bmp_head;

void draw_test_image()
{
    img_handle = DrawDibOpen();
   
    file_handle = fopen("C:\\My Documents\\My programs\\ulillilliacity.bmp",
"rb"); // read the source BMP file to display, binary mode
    fread(&bmp_filehead.bfType, 2, 1, file_handle); // bytes 00 and 01
    fread(&bmp_filehead.bfSize, 4, 1, file_handle); // 02 through 05
    fread(&bmp_filehead.bfReserved1, 2, 1, file_handle); // 06 and 07
    fread(&bmp_filehead.bfReserved2, 2, 1, file_handle); // 08 and 09
    fread(&bmp_filehead.bfOffBits, 4, 1, file_handle); // 0A through 0D
   
    // fill the bitmap info header struct reading from the file
    fread(&bmp_head.biSize, 4, 1, file_handle); // bytes 0E through 11
    fread(&bmp_head.biWidth, 4, 1, file_handle); // 12 through 15
    fread(&bmp_head.biHeight, 4, 1, file_handle); // 16 through 19
    fread(&bmp_head.biPlanes, 2, 1, file_handle); // 1A and 1B
    fread(&bmp_head.biBitCount, 2, 1, file_handle); // 1C and 1D
    fread(&bmp_head.biCompression, 4, 1, file_handle); // 1E through 21
    fread(&bmp_head.biSizeImage, 4, 1, file_handle); // 22 through 25
    fread(&bmp_head.biXPelsPerMeter, 4, 1, file_handle); // 26 through 29
    fread(&bmp_head.biYPelsPerMeter, 4, 1, file_handle); // 2A through 2D
    fread(&bmp_head.biClrUsed, 4, 1, file_handle); // 2E through 31
    fread(&bmp_head.biClrImportant, 4, 1, file_handle); // 32 through 35
    fread(&bmp_image, 1, bmp_head.biSizeImage, file_handle); // read the
remainder of the bits, the image data part // bytes 36 and onward
    fclose(file_handle);
   
    DrawDibDraw(img_handle, HDC_handle,
window_size_base.x/2-bmp_head.biWidth/2,
window_size_base.y/2-bmp_head.biHeight/2, bmp_head.biWidth,
bmp_head.biHeight, bmp_head_pointer, bmp_image_pointer, 0, 0,
bmp_head.biWidth, bmp_head.biHeight, NULL); // center the full image in the
window
    DrawDibClose(img_handle); // free the resources since only one "render" is
made
    ReleaseDC(hwnd, HDC_handle); // free the DC handle
}
[/code]

Signature

Some programming:
void bluewater() {while(game_runs == 1)
{if fox.position == in_blue_water) {fox.health -= 1;}
else {fox.wants_to_get_wet = true;} wait_frames(1);}}

a_dreamer - 18 Dec 2006 14:50 GMT
hi ulillillia

i' think your bitmap is 24bpp dept
i had the  same problem ,but only when i try to draw its as an unscaled
image (zoom = 1 )
check this case ( try to draw it as a scaled image ) and reply me
good luck.

> My code compiles without any errors and the only warnings are irrelevant to
> the function used to load and display the image with.  The file loads
[quoted text clipped - 70 lines]
> {if fox.position == in_blue_water) {fox.health -= 1;}
> else {fox.wants_to_get_wet = true;} wait_frames(1);}}
Tamas Demjen - 18 Dec 2006 17:37 GMT
>     fread(&bmp_image, 1, bmp_head.biSizeImage, file_handle); // read the
> remainder of the bits, the image data part // bytes 36 and onward

Well, biSizeImage is usually 0 for uncompressed bitmaps. You're supposed
to calculate the size on your own using the following formula:

const int pixels_size = (bmp_head.biWidth * bmp_head.biBitCount + 31) /
32 * 4;

Also note that if the bmp_head.biBitCount <= 8, you also have a palette
before the pixel data.

if(bmp_head.biBitCount <= 8)
{
   const int pal_colors = (bih->biClrUsed == 0 ?
      (1 << (bih->biBitCount)) : bih->biClrUsed);
   // TODO read pal_colors * sizeof(RGBQUAD) bytes
}

You have to do this after reading bmp_head and before reading the
image's pixels.

I think you should read bmp_filehead and bmp_head in 1 piece, instead of
breaking them up into bytes, like this:

fread(&bmp_filehead, sizeof(BITMAPFILEHEADER), 1, file_handle);
fread(&bmp_head, sizeof(BITMAPINFOHEADER), 1, file_handle);

Your code is way overcomplicated and I have no time to verifying that
you got the offsets and bits correctly.

Tom
Tamas Demjen - 18 Dec 2006 17:51 GMT
> const int pixels_size = (bmp_head.biWidth * bmp_head.biBitCount + 31) /
> 32 * 4;

Oops, this is the size of each line. Multiply this by bmp_head.biHeight
to get the uncompressed image size (without the palette and headers,
just the pixels). That's how much memory you need to allocate.

Tom
Tamas Demjen - 18 Dec 2006 17:47 GMT
> char bmp_image[589824]; // data for the bitmap image // 589878 bytes is the

This is not good either. Although the documentation doesn't mention this
anywhere, your bitmap pixel data must begin at a DWORD (or WORD?)
boundary, otherwise DrawDibDraw won't display your image. I'm speaking
from experience -- it simply won't display anything unless the alignment
is right. When I'm using Borland compilers, even malloc won't always
work -- sometimes my image displays, and sometimes it doesn't, depending
on how malloc aligns the memory, which is totally unpredictable. I was
only getting good, reliable results when I was using VirtualAlloc:

unsigned char* bmp_image = static_cast<unsigned char*>(VirtualAlloc(0,
bmp_head.biSizeImage, MEM_COMMIT, PAGE_READWRITE)); // allocate
[...] // use
VirtualFree(bmp_image, 0, MEM_RELEASE); // free

In Visual C++ malloc should be OK (not sure), but I don't think you can
use stack variables.

Also note that I woulnd't use DrawDibDraw to resize images. Its quality
is extremely bad, as it's not doing interpolation. DrawDibDraw is only
really fast if you don't resize just display 1:1.

Tom

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.