Cannot display bitmap at higher resolution than CDC area
Hello dear gurus and expert coders.
I'm not going to start as a beginner and don't know much about image programming, but unfortunately these are facts :(
I am trying to display an image from a * ImageData bitmap which has a resolution of 1392x1032. I am trying to do this in a resolution area or 627x474.
However, retrying does not work. It works when I change the bitmap I created from * ImageData Image Width and Height to a resolution or size of about 627x474.
I really don't know how to solve this after trying all possible solutions from different forums and google.
pDC is CDC * and memDC is CDC, initialized in an earlier method Anything uninitialized here has been initialized in other ways.
Here is my code, dear humble gurus. Provide me with Yoda and Obi-Wan's instructions given to Luke Skywalker.
void DemoControl::ShowImage( void *ImageData )
{
int Width; //Width of Image From Camera
int Height; //Height of Image From Camera
int m_DisplayWidth = 627 ;//width of rectangle area to display
int m_DisplayHeight = 474;//height of rectangle area to display
GetImageSize( &Width, &Height ) ; //this will return Width = 1392, Height 1032
CBitmap bitmap;
bitmap.CreateBitmap(Width,Height,32,1,ImageData);
CBitmap* pOldBitmap = memDC.SelectObject((CBitmap*)&bitmap);
pDC->BitBlt(22, 24, 627, 474, &memDC, 0, 0, SRCCOPY);
memDC.SelectObject((CBitmap*)pOldBitmap);
ReleaseDC(pDC);
}
Unlock a few more parts
I think I should explain how the flow is going.
(a) A class (let's say the DemoTestingDlg class) will pass the CDC, as shown below, to another class (let's say the DemoControl class)
m_Demo = new DemoControl ;
m_Demo->Initialisation( this, this->GetDC() ) ;
(b) In class DemoControl
bool DemoControl :: Initialization (CDemoTestingDlg m_FormControl, CDC m_StaticDisplay) {
pDC = m_StaticDisplay ;
memDC.CreateCompatibleDC(pDC);
}
pDC and memDC as such in the header:
CDC* pDC ; CDC memDC;
(c) If the image can be said to be captured, the image pointer is passed to the DemoTestingDlg class, which subsequently calls the showImage method in the Demo Control class, which is the method I wrote in the question. Am I doing it right?
Note. It showed an image if lets say they are the same size (by which I mean CDC and bitmap), so I got the impression that my CDC pointer was passed correctly
a source to share
StretchBlt is your friend :)
Edit: OK, how do you get pDC? When is your function called? Form OnPaint or DrawItem?
This is the StretchBlt I make from the DrawItem call in the overriden CStatic.
HDC hBitmapDC = CreateCompatibleDC( pDrawItemStruct->hDC );
HBITMAP hBitmap = GetBitmap();
HGDIOBJ hOld = SelectObject( hBitmapDC, (HGDIOBJ)hBitmap );
StretchBlt( pDrawItemStruct->hDC, pDrawItemStruct->rcItem.left, pDrawItemStruct->rcItem.top, pDrawItemStruct->rcItem.right, pDrawItemStruct->rcItem.bottom,
hBitmapDC, 0, 0, 4, 4, SRCCOPY );
SelectObject( hBitmapDC, hOld );
DeleteObject( hBitmapDC );
Don't use MFC classes to stretch the 4x4 bitmap into a larger space, but works great. I assume you are not doing this in response to WM_PAINT / WM_DRAWITEM and / or using the wrong DC.
Change your edit: Are you calling DrawImage from a call to OnPaint or DrawItem?
I would think you better not cache this CDC and pass the CDC every time you want to draw it.
a source to share
"from a bitmap pointer * ImageData, which has a resolution of 1392x1032"
No, it's not 1392x1032. Resolution is the number of discrete visual units per distance.
Anyway, as mentioned above, you need to post more code. Show at least OnPaint (). Where do you create CPaintDC? Create a new project and put all your code there so that you have a minimal test suite that detects the problem. You seem to be roughly on the right track if you use StretchBlt () instead of the BitBlt () you are using now.
a source to share