Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm having issue with a piece of code meant to scale an image.I found a peice of code to do this here Scaling CImage[^]. My problem is that the line CDC *screenDC = GetDC(); gives the error "a value of type "HDC" cannot be used to initialize an entity of type "CDC *". I'm not really sure how to fix it as my all accounts it should be working correctly and most of the other solutions offer this as an example.

Here is the code
C++
int iNewWidth = 2048;
	int iNewHeight = 2048;

	CImage image;
	image.Load(_T("C:\\Image.jpg"));
	
	CDC *screenDC = GetDC(); //Section that gives error
	CDC *pMDC = new CDC;
	pMDC->CreateCompatibleDC(screenDC);

	CBitmap *pb = new CBitmap;
	pb->CreateCompatibleBitmap(screenDC, iNewWidth, iNewHeight);

	CBitmap *pob = pMDC->SelectObject(pb);
	image.StretchBlt(pMDC->m_hDC,0, 0, iNewWidth, iNewHeight, 0, 0, image.GetWidth(), image.GetHeight(), SRCCOPY);
	pMDC->SelectObject(pob);
	
	CImage new_image;
	new_image.Attach((HBITMAP)(*pb));
	new_image.Save(_T("c:\\NewImage.jpg"));
	new_image.Detach();
	ReleaseDC(screenDC);


Thanks

What I have tried:

Removing this line causes the image to be black after it has been scaled.
Posted
Updated 13-Jul-16 21:39pm
Comments
barneyman 13-Jul-16 19:08pm    
are you in a CWnd derived class when you call this?

Sounds like you're getting ::GetDC, which returns an HDC
Member 12633893 13-Jul-16 19:16pm    
It is in a void function that is called from the main() of the application if that is what you mean?
Mohibur Rashid 13-Jul-16 21:55pm    
Your code is mix of plain win32 functions and MFC. In MFC you do not interact with main function. CWnd::GetDC() return CDC* on the other hand ::GetDC return HDC.
CDC *screenDC = GetDC();
should be
HDC screenDC = GetDC();
and also replace all CDC* with HDC

1 solution

I guess that you arent in a CWnd derived class, where it is a member function.

For that reason is a function FromHandle() available.

Read some more about graphics and sample code to fill you knowledge gaps. ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900