Click here to Skip to main content
15,918,706 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How can i save the content of the image present in the Picture control, i tried the following code but not able to save correct image.i am getting only Black image.

<br />
<br />
CWnd* pWnd = GetDlgItem(IDC_PICTURE);<br />
<br />
HWND hWnd = pWnd->GetSafeHwnd();<br />
<br />
HDC hdc= ::GetDC(hWnd);<br />
<br />
cdc.Attach(hdc);<br />
<br />
MemDC.CreateCompatibleDC(&cdc);<br />
<br />
CBitmap Bmp;<br />
<br />
Bmp.CreateCompatibleBitmap(&cdc,w,b);<br />
<br />
CBitmap *pOldBitmap = MemDC.SelectObject(&Bmp);<br />
<br />
cdc.BitBlt(0,0,w,b,&MemDC,0,0,SRCCOPY);<br />
<br />
MemDC.SelectObject(pOldBitmap);<br />
<br />
CImage Image;<br />
<br />
Image.Attach((HBITMAP)Bmp.Detach());<br />
<br />
Image.Save(L"C:\\image.bmp");<br />
<br />


Thanks
Karthik
Posted

Looks like you are blitting the wrong way.

MemDC.BitBlt(0,0,w,b,&cdc,0,0,SRCCOPY);


Also you need some cleaning up, but maybe you already do that.
 
Share this answer
 
Hi Niklas,

I changed the blitting but no use, Yes the cleanup has been taken care, but not in the sample code.
 
Share this answer
 
Here is an alternate solution for you:
C++
CStatic *pStatic = static_cast<CStatic*>(GetDlgItem(IDC_PICTURE));
CImage image;
image.Attach(pStatic->GetBitmap());
image.Save("C:\\image.bmp");
image.Detach();

assuming you have a bitmap in your control. You might need to verify the GetBitmap() call.
 
Share this answer
 
I tested the code
This will work if the BMP is preloaded.
But in my case it is loaded in runtime so GetBitmap is failing.
 
Share this answer
 
Comments
Niklas L 30-Jun-10 9:07am    
If it's loaded with CStatic::SetBitmap(), it should still work according to the docs.

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