Click here to Skip to main content
11,721,289 members (86,745 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC image resize
I want to resize a bit picture saved in a file with the following code

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 4-Jun-12 23:58pm
Lam Giang1.4K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

See, for instance, here: "Scaling CImage"[^].
  Permalink  
Comments
Lam Giang at 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 at 5-Jun-12 8:55am
   
Did you pass the screen DC to CreateCompatibleDC, as suggested?
Lam Giang at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
sm_img.ReleaseDC();
in the end to avoid error when exiting the function
  Permalink  
v2
Comments
Lam Giang at 5-Jun-12 23:52pm
   
Yes, it really helps.
ErnestoNet at 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.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 515
1 OriginalGriff 279
2 Mika Wendelius 170
3 F-ES Sitecore 150
4 cyycoish 70
0 Sergey Alexandrovich Kryukov 2,209
1 OriginalGriff 969
2 F-ES Sitecore 660
3 Maciej Los 609
4 Richard MacCutchan 560


Advertise | Privacy | Mobile
Web04 | 2.8.150901.1 | Last Updated 6 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100