CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2024
    Posts
    2

    Question Displaying .bmp images in a window

    Hello,

    My name is Mick and this is my first ever post on this forum. I have written several working C++ applications over the years using Borland and now MS Visual Studio, such as a Hex Editor, a Bitmap Creator, a coded Password Manager and a few others so, although I'm no expert, I'm not completely green.

    I have been having a problem understanding why I could not seem to display my own photos (saved in ?.bmp? format), into the ChildView window provided by MS Visual Studio.

    When displaying a photo as a named resource, I have no problem, the selected picture is displayed in the window, right way up.

    However, my aim here is to display random pictures selected using CFileDialog. I looked online about CBitmap::LoadImage() and found that, rather than using a fixed resource approach, which was limiting, I could replace the resID with a file path string to access the file directly, using the code below:


    void CChildView::OnPaint()
    {
    CPaintDC dc(this);

    CBitmap cbm;
    cbm.LoadBitmap(?<path>?);

    CDC memdc;
    memdc.CreateCompatibleDC(&dc);

    Bitmap* pOldBitmap = memdc.SelectObject(&cbm);

    dc.StretchBlt(0, 0, 800, 600, &memdc, 0, 0, m_pBMIH->biWidth, m_pBMIH->biHeight, SRCCOPY);

    memdc.SelectObject(pOldBitmap);
    }

    This produced nothing in the window. I then tried most of the other methods like cbm.CreateBitmap(), cbm.CreateBitmapIndirect(), cbm.CreateCompatibleBitmap(), cbm.GetBitmap() and cbm.GetBitmapBits(), all to no avail, just a blank window.

    I started fault testing to make sure correct values were present in the bitmaps used in these functions and found them to be OK. It was when I tested the memDC value that I found where the fault lay. The memDC was being set to 0.

    Looking in the Microsoft MFC help files, I found that if the memDC is not identical to the CPaint:c, it will be set to 0.

    Looking at my photo, which was originally a '.jpg', but converted in Paint to a '.bmp' for the purpose of application testing, I found this to be in 24 bit colour format. The value returned from dc:GetDeviceCaps(BITSPIXEL) was 32 bit format.

    I wrote a bit of code to convert my 24 bit photo into 32 bit format. When I executed my application again, lo and behold, there was my photo in the window, although upside down. I amended my code to display the lines top down and finally had my picture!

    However, I cannot believe this can be the right way to go, it seems so convoluted, not to mention the fact that it took around 1 full minute to do the conversion from 24 bit to 32 bit (4000x3000px), not user friendly at all. I could find no way to alter the Device Context (BITSPIXEL) to 24 bit to suit my photo.

    If a resource based photo can be displayed in the correct orientation instantly, why have I had to use all these 'get-arounds'? Can anybody tell me what I'm missing here? The resource compiler seems to have no problem with differing Bit Counts and non-identical DCs using the resource based method, why do I seem to have them in a non-resource method?

    Thanks for listening.

    Mick

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,407

    Re: Displaying .bmp images in a window

    The first thing you have to perform is tu replace the call of LoadBitmap with LoadImage
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2024
    Posts
    2

    Re: Displaying .bmp images in a window

    Thank you Victor, that worked a treat, never knew there was a ::LoadImage in Win32. Unfortunately, dc.StretchBlt( ) doesn't do a particularly good job reducing from 4000x3000 to 800x600, lots of colour artifacts. Much better when I use my Affinity 2 to convert the .jpg image size to 800x600 before saving it as a .bmp, then using dc.BitBlt( ). Well, now that's sorted I can move on to learning about CImage. Thanks again

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,407

    Re: Displaying .bmp images in a window

    You are welcome!
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2024
    Posts
    1

    Re: Displaying .bmp images in a window

    Instead of manually converting the bitmap, have you considered using the Windows Imaging Component (WIC) or GDI+? These APIs can handle various image formats and color depths more efficiently.

    For example, with GDI+, you could use the Gdiplus::Bitmap class to load your image directly:

    HTML Code:
    Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(L"path/to/your/image.bmp");
    Gdiplus::Graphics graphics(dc.GetSafeHdc());
    graphics.DrawImage(bitmap, 0, 0, 800, 600);
    delete bitmap;
    This approach should handle the color depth conversion and orientation automatically, and it's generally faster than manual conversion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured