平滑缩放位图,保存位图到bmp文件

   1: //Scale the SrcBitmap, with destWidth*destHeight
   2: void ScaleBitmap(CBitmap *pSrcBitmap, CBitmap *pDestBitmap, int destWidth, int destHeight)
   3: {
   4:     BITMAP bitmapInfo;
   5:     pSrcBitmap->GetBitmap(&bitmapInfo);
   6:     int srcWidth = bitmapInfo.bmWidth;
   7:     int srcHeight = bitmapInfo.bmHeight;
   8:  
   9:     HDC hScreenDC = ::GetDC(NULL);
  10:     CDC* pScreenDC = CDC::FromHandle(hScreenDC);
  11:     CDC* pSrcDC = new CDC();
  12:     pSrcDC->CreateCompatibleDC(pScreenDC);
  13:     CBitmap* pSrcOldBitmap = pSrcDC->SelectObject(pSrcBitmap);
  14:  
  15:     pDestBitmap->CreateCompatibleBitmap(pSrcDC,destWidth,destHeight);
  16:  
  17:     CDC* pDestDC = new CDC();
  18:     pDestDC->CreateCompatibleDC(pSrcDC);
  19:     CBitmap* pDestOldBitmap = pDestDC->SelectObject(pDestBitmap);
  20:  
  21:     pDestDC->SetStretchBltMode(HALFTONE);
  22:     pDestDC->StretchBlt(0,0,destWidth,destHeight,pSrcDC,0,0,srcWidth,srcHeight,SRCCOPY);
  23:     pDestDC->SelectObject(pDestOldBitmap);
  24:     pSrcDC->SelectObject(pSrcOldBitmap);
  25:  
  26:     delete pDestDC;
  27:     delete pSrcDC;
  28:  
  29:     ::ReleaseDC(NULL,hScreenDC);
  30: }
  31:  
  32: //Save a bitmap as a bitmap file
  33: //#include <atlimage.h>
  34: CBitmap* pSrcBitmap = new CBitmap();
  35: pSrcBitmap->Attach(hBitmap);
  36:  
  37: CBitmap* pDestBitmap = new CBitmap();
  38: ScaleBitmap(pSrcBitmap,pDestBitmap,destWidth,destHeight);
  39:  
  40: CImage image;
  41: image.Attach((HBITMAP)*pDestBitmap);
  42: image.Save(fileName, Gdiplus::ImageFormatBMP);
  43: image.Detach();
  44:  
  45: pSrcBitmap->Detach();
  46: delete pSrcBitmap;
  47: delete pDestBitmap;

posted on 2009-07-08 00:09  wudong  阅读(1054)  评论(0编辑  收藏  举报

导航