Display bitmap problem
In my main dialog I have two buttons:
These two buttons load a bitmap from a file to a picture box ShowBmp
I have two test bitmap files 01.bmp & 02.bmp for testing.
I can view both bitmap files fine with windows.
For some reason within my program one shows fine and the other does not
show at all.
The OnPaint function has been removed from the main dialog.
I already checked that bitmap selection does work within OnPaint.
Any ideas?
void CMyDemoDlg::OnView1() // button 1
{
m_ShowBmp.SetBitmap(1);
m_ShowBmp.Invalidate();
}
void CMyDemoDlg::OnView2() // button 2
{
m_ShowBmp.SetBitmap(2);
m_ShowBmp.Invalidate();
}
=======================================================
// ShowBmp.cpp : implementation file
#include "stdafx.h"
#include "ShowBmp.h"
#include "resource.h"
// CShowBmp
IMPLEMENT_DYNAMIC(CShowBmp, CStatic)
CShowBmp::CShowBmp()
: m_IDBitmap(0)
{
}
CShowBmp::~CShowBmp()
{
}
BEGIN_MESSAGE_MAP(CShowBmp, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CShowBmp::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect cr;
GetClientRect(&cr);
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
int SavedDC1 = MemDC.SaveDC();
CBitmap MemBitmap;
MemBitmap.CreateCompatibleBitmap(&dc,cr.Width(),cr.Height());
MemDC.SelectObject(&MemBitmap);
//start with a grey background
MemDC.FillSolidRect(&cr,RGB(220,220,220));
if (m_IDBitmap == 1) //loads bitmap if set
{
// load the .bmp from a file
CString szFilename("C:\\TestFolder\\01.bmp");
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap Bitmap;
Bitmap.Attach(hBmp);
BITMAP BmpInfo;
Bitmap.GetBitmap(&BmpInfo);
CDC MemDC2;
MemDC2.CreateCompatibleDC(&dc);
int SavedDC2 = MemDC2.SaveDC();
MemDC2.SelectObject(&Bitmap); //copy bitmap to memory dc.
MemDC.StretchBlt(0,0,cr.Width(),cr.Height(),&MemDC2,0,0,BmpInfo.bmWidth,BmpInfo.bmHeight,SRCCOPY);
MemDC2.RestoreDC(SavedDC2);
}
if (m_IDBitmap == 2) //loads bitmap if set
{
CString szFilename("C:\\TestFolder\\02.bmp"); //
loads the .bmp from a file
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap Bitmap;
Bitmap.Attach(hBmp);
BITMAP BmpInfo;
Bitmap.GetBitmap(&BmpInfo);
CDC MemDC2;
MemDC2.CreateCompatibleDC(&dc);
int SavedDC2 = MemDC2.SaveDC();
MemDC2.SelectObject(&Bitmap); //copy bitmap to memory dc.
MemDC.StretchBlt(0,0,cr.Width(),cr.Height(),&MemDC2,0,0,BmpInfo.bmWidth,BmpInfo.bmHeight,SRCCOPY);
MemDC2.RestoreDC(SavedDC2);
}
dc.BitBlt(0,0,cr.Width(),cr.Height(),&MemDC,0,0,SRCCOPY); //dump
everything from memory dc to dialog
MemDC.RestoreDC(SavedDC1);
}