Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3046

Displaying .bmp images in a window

$
0
0
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::dc, 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

Viewing all articles
Browse latest Browse all 3046

Trending Articles