Click here to Skip to main content
15,887,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to resize a bit picture saved in a file with the following code

C++
CImage img;
CImage sm_img;

img.Load(FileName);

sm_img.Create(80,60,img.GetBPP(),
HDC sm_hdc = sm_img.GetDC();

img.StretchBlt(sm_hdc,0,0,80,60,SRCCOPY);
sm_img.Save(sjpgFileName);

img.Detach();


The result is bad as colors are lost. I guest we need to set som attributes for the variable sm_img but don't know how. Can you help me, please !
Posted

See, for instance, here: "Scaling CImage"[^].
 
Share this answer
 
Comments
Lam Giang 5-Jun-12 8:46am    
In the first version, I got a bad image, but in the version using your solution
I've got a black one. Still do not understand
CPallini 5-Jun-12 8:55am    
Did you pass the screen DC to CreateCompatibleDC, as suggested?
Lam Giang 5-Jun-12 9:44am    
This is the code snippet I tested
<pre lang="c++">

CImage img;
CImage sm_img;
img.Load(jpgFileName);

CDC *screenDC = GetDC();
CDC *pMDC = new CDC;
pMDC->CreateCompatibleDC(screenDC);

CBitmap *pb = new CBitmap;

int iNewWidth = 80;
int iNewHeight = 60;

pb->CreateCompatibleBitmap(pMDC, iNewWidth, iNewHeight);
//SetStretchBltMode(pMDC->m_hDC,COLORONCOLOR);
img.StretchBlt(pMDC->m_hDC,0, 0, iNewWidth, iNewHeight, SRCCOPY);


// The next two lines test the image on a picture control.
// The output has wrong color palette, like my initial version

HDC hdcpic = ::GetDC(m_ClipPreview.m_hWnd);
img.StretchBlt( hdcpic,0, 0, iNewWidth, iNewHeight, SRCCOPY);

CBitmap *pob = pMDC->SelectObject(pb);

// The next lines create a black image

sm_img.Attach((HBITMAP)(*pb));
sm_img.Save(sjpgFileName);
img.Detach();
ReleaseDC(screenDC);

</pre>

I am running VS2005 on Windows 7, screen resolution 1280x800, 32 color bit
You don't need to create a DC (pMDC). Just use the DC from sm_img (sm_img.GetDC()):
    CImage img;
CImage sm_img;
HRESULT res = img.Load(jpgFileName);

CDC *screenDC = GetDC();

int iNewWidth = 80;
int iNewHeight = 60;

sm_img.Create(iNewWidth, iNewHeight,32);

SetStretchBltMode(sm_img.GetDC(),COLORONCOLOR);
img.StretchBlt(sm_img.GetDC(),0, 0, iNewWidth, iNewHeight, SRCCOPY);
// The next two lines test the image on a picture control.
// The output has wrong color palette, like my initial version
HDC hdcpic = ::GetDC(m_ClipPreview.m_hWnd);

img.StretchBlt( hdcpic,0, 0, iNewWidth, iNewHeight, SRCCOPY);

sm_img.Save(jpgFileName);
ReleaseDC(screenDC);


Hope it helps

I added one line
C++
sm_img.ReleaseDC();

in the end to avoid error when exiting the function
 
Share this answer
 
v2
Comments
Lam Giang 5-Jun-12 23:52pm    
Yes, it really helps.
ErnestoNet 6-Jun-12 9:49am    
sm_img.ReleaseDC();
You're right, I forgot to release the DC.
With the line you added it should work.
Member 15786969 23-Mar-23 2:08am    
CImage img;
CImage sm_img;
img.Load(L"C://Users//mi//Downloads//drishg.bmp");

CDC* screenDC = GetDC();

int iNewWidth = 384;
int iNewHeight = 384;

sm_img.Create(iNewWidth, iNewHeight, 24);

SetStretchBltMode(sm_img.GetDC(), COLORONCOLOR);
img.StretchBlt(sm_img.GetDC(), 0, 0, iNewWidth, iNewHeight, SRCCOPY);

// The next two lines test the image on a picture control.
//HDC hdcpic = sm_img.GetDC();

//img.StretchBlt(hdcpic, 0, 0, iNewWidth, iNewHeight, SRCCOPY);

sm_img.Save(L"C://Users//mi//Downloads//Resizedrish.bmp");

sm_img.Destroy();
ReleaseDC(screenDC);
sm_img.ReleaseDC();

I'm using this but still giving me asserrtion

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