MemDC.h

  1. #ifndef _MEMDC_H_
  2. #define _MEMDC_H_
  3. //
  4. // CMemDC - memory DC
  5. //
  6. // Author: Keith Rule
  7. // Email:  keithr@europa.com
  8. // Copyright 1996-1999, Keith Rule
  9. //
  10. // You may freely use or modify this code provided this
  11. // Copyright is included in all derived versions.
  12. //
  13. // History - 10/3/97 Fixed scrolling bug.
  14. //                   Added print support. - KR
  15. //
  16. //           11/3/99 Fixed most common complaint. Added
  17. //                   background color fill. - KR
  18. //
  19. //           11/3/99 Added support for mapping modes other than
  20. //                   MM_TEXT as suggested by Lee Sang Hun. - KR
  21. //
  22. // This class implements a memory Device Context which allows
  23. // flicker free drawing.
  24. class CMemDC : public CDC {
  25. protected:
  26.    CBitmap  m_bitmap;       // Offscreen bitmap
  27.    CBitmap* m_oldBitmap;    // bitmap originally found in CMemDC
  28.    CDC*     m_pDC;          // Saves CDC passed in constructor
  29.    CRect    m_rect;         // Rectangle of drawing area.
  30.    BOOL     m_bMemDC;       // TRUE if CDC really is a Memory DC.
  31.     
  32.    void Construct(CDC* pDC)
  33.    {
  34.         ASSERT(pDC != NULL); 
  35.         // Some initialization
  36.         m_pDC = pDC;
  37.         m_oldBitmap = NULL;
  38.         m_bMemDC = !pDC->IsPrinting();
  39.         if (m_bMemDC) {
  40.             // Create a Memory DC
  41.             CreateCompatibleDC(pDC);
  42.             pDC->LPtoDP(&m_rect);
  43.             m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  44.             m_oldBitmap = SelectObject(&m_bitmap);
  45.             
  46.             SetMapMode(pDC->GetMapMode());
  47.             pDC->DPtoLP(&m_rect);
  48.             SetWindowOrg(m_rect.left, m_rect.top);
  49.         } else {
  50.             // Make a copy of the relevent parts of the current DC for printing
  51.             m_bPrinting = pDC->m_bPrinting;
  52.             m_hDC       = pDC->m_hDC;
  53.             m_hAttribDC = pDC->m_hAttribDC;
  54.         }
  55.         // Fill background 
  56.         FillSolidRect(m_rect, pDC->GetBkColor());
  57.     }
  58. // TRK begin
  59. public:
  60.    CMemDC(CDC* pDC                  ) : CDC() { pDC->GetClipBox(&m_rect); Construct(pDC); }
  61.    CMemDC(CDC* pDC, const RECT& rect) : CDC() { m_rect = rect           ; Construct(pDC); }
  62. // TRK end
  63.     
  64.    virtual ~CMemDC()
  65.    {        
  66.         if (m_bMemDC) {
  67.             // Copy the offscreen bitmap onto the screen.
  68.             m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  69.                 this, m_rect.left, m_rect.top, SRCCOPY);            
  70.             
  71.             //Swap back the original bitmap.
  72.             SelectObject(m_oldBitmap);        
  73.         } else {
  74.             // All we need to do is replace the DC with an illegal value,
  75.             // this keeps us from accidently deleting the handles associated with
  76.             // the CDC that was passed to the constructor.            
  77.             m_hDC = m_hAttribDC = NULL;
  78.         }    
  79.     }
  80.     
  81.     // Allow usage as a pointer    
  82.     CMemDC* operator->() 
  83.     {
  84.         return this;
  85.     }    
  86.     // Allow usage as a pointer    
  87.     operator CMemDC*() 
  88.     {
  89.         return this;
  90.     }
  91. };
  92. #endif

 

先用普通DC构造一个memDC,然后把绘图操作作用在memDC上,当memDC析构时,自动把结果写到普通DC上。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用MFC将JPG图片转换为单色位图的代码: 1. 首先,需要包含以下头文件: ```c++ #include <afxwin.h> #include <afxext.h> #include <afxcmn.h> #include <afxdlgs.h> #include <atlimage.h> ``` 2. 然后,创建一个CImage对象并加载JPG文件: ```c++ CImage image; image.Load(_T("test.jpg")); ``` 3. 接下来,将图像转换为灰度图像: ```c++ CImage grayImage; grayImage.Create(image.GetWidth(), image.GetHeight(), 8); CDC* pDC = CDC::FromHandle(grayImage.GetDC()); CDC memDC; memDC.CreateCompatibleDC(pDC); CBitmap bmp; bmp.CreateCompatibleBitmap(pDC, image.GetWidth(), image.GetHeight()); CBitmap* pOldBmp = memDC.SelectObject(&bmp); memDC.BitBlt(0, 0, image.GetWidth(), image.GetHeight(), pDC, 0, 0, SRCCOPY); memDC.SelectObject(pOldBmp); grayImage.ReleaseDC(); ``` 4. 然后,将灰度图像转换为单色位图: ```c++ CImage monoImage; monoImage.Create(grayImage.GetWidth(), grayImage.GetHeight(), 1); pDC = CDC::FromHandle(monoImage.GetDC()); pDC->SetStretchBltMode(HALFTONE); pDC->SetBrushOrg(0, 0); pDC->BitBlt(0, 0, grayImage.GetWidth(), grayImage.GetHeight(), NULL, 0, 0, WHITENESS); pDC->SetTextColor(RGB(0, 0, 0)); pDC->SetBkColor(RGB(255, 255, 255)); pDC->BitBlt(0, 0, grayImage.GetWidth(), grayImage.GetHeight(), CDC::FromHandle(grayImage.GetDC()), 0, 0, SRCAND); monoImage.ReleaseDC(); ``` 5. 最后,保存单色位图: ```c++ monoImage.Save(_T("test_mono.bmp"), Gdiplus::ImageFormatBMP); ``` 完整代码如下: ```c++ #include <afxwin.h> #include <afxext.h> #include <afxcmn.h> #include <afxdlgs.h> #include <atlimage.h> void ConvertToMono(LPCTSTR lpszFileName) { CImage image; image.Load(lpszFileName); CImage grayImage; grayImage.Create(image.GetWidth(), image.GetHeight(), 8); CDC* pDC = CDC::FromHandle(grayImage.GetDC()); CDC memDC; memDC.CreateCompatibleDC(pDC); CBitmap bmp; bmp.CreateCompatibleBitmap(pDC, image.GetWidth(), image.GetHeight()); CBitmap* pOldBmp = memDC.SelectObject(&bmp); memDC.BitBlt(0, 0, image.GetWidth(), image.GetHeight(), pDC, 0, 0, SRCCOPY); memDC.SelectObject(pOldBmp); grayImage.ReleaseDC(); CImage monoImage; monoImage.Create(grayImage.GetWidth(), grayImage.GetHeight(), 1); pDC = CDC::FromHandle(monoImage.GetDC()); pDC->SetStretchBltMode(HALFTONE); pDC->SetBrushOrg(0, 0); pDC->BitBlt(0, 0, grayImage.GetWidth(), grayImage.GetHeight(), NULL, 0, 0, WHITENESS); pDC->SetTextColor(RGB(0, 0, 0)); pDC->SetBkColor(RGB(255, 255, 255)); pDC->BitBlt(0, 0, grayImage.GetWidth(), grayImage.GetHeight(), CDC::FromHandle(grayImage.GetDC()), 0, 0, SRCAND); monoImage.ReleaseDC(); monoImage.Save(_T("test_mono.bmp"), Gdiplus::ImageFormatBMP); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值