Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Drawing speed

Drawing speed

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsperformancequestionhtmlcom
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    Hi everybody. I trying to draw an overlay over a CDib bitmap:

    ///*//// Draw the bitmap ////////////////////////////////////////////////////////////////
    CSize sizeDoc = pDoc->GetDib()->GetDimensions();
    int cx = (int)((float)sizeDoc.cx * m_fZoomFactor);
    int cy = (int)((float)sizeDoc.cy * m_fZoomFactor);
    CSize sizeLog(cx, cy);
    CBitmap bmp;
    bmp.CreateCompatibleBitmap(pDC, sizeLog.cx, sizeLog.cy); // create background bitmap
    CDC MemDC;
    MemDC.CreateCompatibleDC(pDC); // create memory device context
    CBitmap* pOldBitmap = MemDC.SelectObject(&bmp); // select background into memory device context
    pDoc->GetDib()->Draw(&MemDC, CPoint(0,0), sizeLog); // draw content of dib into memory device context
    DrawOverlay(&MemDC, pDC); // draw overlay (inline method)
    pDC->BitBlt(0, 0, sizeLog.cx, sizeLog.cy, &MemDC, 0, 0, SRCCOPY); // switch memory device context to real device context
    pDC->SelectObject(pOldBitmap); // select old bitmap
    ///*/////////////////////////////////////////////////////////////////////////////////////

    and everything goes well, except when user try to drag the view, while view goes slowly ... why ? (That behaviour is more visible when the picture is a little bit zoomed)How can I speed up the code from above ? Or, what I'm doing wrong ? I have here[^] a simple sample project .... Thank you.

    C S 2 Replies Last reply
    0
    • _ _Flaviu

      Hi everybody. I trying to draw an overlay over a CDib bitmap:

      ///*//// Draw the bitmap ////////////////////////////////////////////////////////////////
      CSize sizeDoc = pDoc->GetDib()->GetDimensions();
      int cx = (int)((float)sizeDoc.cx * m_fZoomFactor);
      int cy = (int)((float)sizeDoc.cy * m_fZoomFactor);
      CSize sizeLog(cx, cy);
      CBitmap bmp;
      bmp.CreateCompatibleBitmap(pDC, sizeLog.cx, sizeLog.cy); // create background bitmap
      CDC MemDC;
      MemDC.CreateCompatibleDC(pDC); // create memory device context
      CBitmap* pOldBitmap = MemDC.SelectObject(&bmp); // select background into memory device context
      pDoc->GetDib()->Draw(&MemDC, CPoint(0,0), sizeLog); // draw content of dib into memory device context
      DrawOverlay(&MemDC, pDC); // draw overlay (inline method)
      pDC->BitBlt(0, 0, sizeLog.cx, sizeLog.cy, &MemDC, 0, 0, SRCCOPY); // switch memory device context to real device context
      pDC->SelectObject(pOldBitmap); // select old bitmap
      ///*/////////////////////////////////////////////////////////////////////////////////////

      and everything goes well, except when user try to drag the view, while view goes slowly ... why ? (That behaviour is more visible when the picture is a little bit zoomed)How can I speed up the code from above ? Or, what I'm doing wrong ? I have here[^] a simple sample project .... Thank you.

      C Offline
      C Offline
      chaau
      wrote on last edited by
      #2

      The problem is with this line:

      pDoc->GetDib()->Draw(&MemDC, CPoint(0,0), sizeLog);

      You need to replace it with another BitBlt that will copy stuff to MemDC

      _ 1 Reply Last reply
      0
      • C chaau

        The problem is with this line:

        pDoc->GetDib()->Draw(&MemDC, CPoint(0,0), sizeLog);

        You need to replace it with another BitBlt that will copy stuff to MemDC

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #3

        Thank you for your interest. But I don't know how can I put content of CDib (CMyDoc::m_dib) into MemDC with CDC::BitBlt(...). Can you show me a little sample here ? Will be very helpfull to me ... You can test the behaviuour from above post link.

        C 1 Reply Last reply
        0
        • _ _Flaviu

          Hi everybody. I trying to draw an overlay over a CDib bitmap:

          ///*//// Draw the bitmap ////////////////////////////////////////////////////////////////
          CSize sizeDoc = pDoc->GetDib()->GetDimensions();
          int cx = (int)((float)sizeDoc.cx * m_fZoomFactor);
          int cy = (int)((float)sizeDoc.cy * m_fZoomFactor);
          CSize sizeLog(cx, cy);
          CBitmap bmp;
          bmp.CreateCompatibleBitmap(pDC, sizeLog.cx, sizeLog.cy); // create background bitmap
          CDC MemDC;
          MemDC.CreateCompatibleDC(pDC); // create memory device context
          CBitmap* pOldBitmap = MemDC.SelectObject(&bmp); // select background into memory device context
          pDoc->GetDib()->Draw(&MemDC, CPoint(0,0), sizeLog); // draw content of dib into memory device context
          DrawOverlay(&MemDC, pDC); // draw overlay (inline method)
          pDC->BitBlt(0, 0, sizeLog.cx, sizeLog.cy, &MemDC, 0, 0, SRCCOPY); // switch memory device context to real device context
          pDC->SelectObject(pOldBitmap); // select old bitmap
          ///*/////////////////////////////////////////////////////////////////////////////////////

          and everything goes well, except when user try to drag the view, while view goes slowly ... why ? (That behaviour is more visible when the picture is a little bit zoomed)How can I speed up the code from above ? Or, what I'm doing wrong ? I have here[^] a simple sample project .... Thank you.

          S Offline
          S Offline
          Sivaraman Dhamodharan
          wrote on last edited by
          #4

          Google it for "Double Buffering"

          Programming Article

          _ 2 Replies Last reply
          0
          • S Sivaraman Dhamodharan

            Google it for "Double Buffering"

            Programming Article

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #5

            I think that is what I'm doing above: draw everything into a memory DC, and when everything is done, show it in real DC ... I'm missing something ?

            1 Reply Last reply
            0
            • _ _Flaviu

              Thank you for your interest. But I don't know how can I put content of CDib (CMyDoc::m_dib) into MemDC with CDC::BitBlt(...). Can you show me a little sample here ? Will be very helpfull to me ... You can test the behaviuour from above post link.

              C Offline
              C Offline
              chaau
              wrote on last edited by
              #6

              You need to create DIB (I think you have already already created it based on the code), then you need to copy the bitmap bits to the DIB's buffer using this code:

              HBITMAP	hOldBitmap = NULL;
              
              if (m\_hBitmap == NULL)
              {
              	m\_hBitmap = CreateDIBSection(hDC, (BITMAPINFO\*)m\_hDib, DIB\_RGB\_COLORS, &m\_lpBits, NULL, 0);
              	if (m\_hBitmap == NULL)	return;
              	if (m\_lpBits == NULL)
              	{
              		::DeleteObject(m\_hBitmap);
              		m\_hBitmap = NULL;
              		return;
              	} // if
              } // if
              
              memcpy(m\_lpBits, ((LPBYTE)m\_hDib + \*(LPDWORD)m\_hDib + GetPaletteSize()), m\_bi.biSizeImage);
              
              if (m\_hMemDC == NULL)
              {
              	m\_hMemDC = CreateCompatibleDC(hDC);
              	if (m\_hMemDC == NULL)	return;
              } // if
              
              hOldBitmap = (HBITMAP)SelectObject(m\_hMemDC, m\_hBitmap);
              
              BitBlt(hDC, dwX, dwY, m\_bi.biWidth, m\_bi.biHeight, m\_hMemDC, 0, 0, SRCCOPY);
              SelectObject(m\_hMemDC, hOldBitmap);
              

              The main part here is the memcpy. I have copied it from my program. If you can't understand what the "m_" variables are, let me know and I will explain

              _ 1 Reply Last reply
              0
              • C chaau

                You need to create DIB (I think you have already already created it based on the code), then you need to copy the bitmap bits to the DIB's buffer using this code:

                HBITMAP	hOldBitmap = NULL;
                
                if (m\_hBitmap == NULL)
                {
                	m\_hBitmap = CreateDIBSection(hDC, (BITMAPINFO\*)m\_hDib, DIB\_RGB\_COLORS, &m\_lpBits, NULL, 0);
                	if (m\_hBitmap == NULL)	return;
                	if (m\_lpBits == NULL)
                	{
                		::DeleteObject(m\_hBitmap);
                		m\_hBitmap = NULL;
                		return;
                	} // if
                } // if
                
                memcpy(m\_lpBits, ((LPBYTE)m\_hDib + \*(LPDWORD)m\_hDib + GetPaletteSize()), m\_bi.biSizeImage);
                
                if (m\_hMemDC == NULL)
                {
                	m\_hMemDC = CreateCompatibleDC(hDC);
                	if (m\_hMemDC == NULL)	return;
                } // if
                
                hOldBitmap = (HBITMAP)SelectObject(m\_hMemDC, m\_hBitmap);
                
                BitBlt(hDC, dwX, dwY, m\_bi.biWidth, m\_bi.biHeight, m\_hMemDC, 0, 0, SRCCOPY);
                SelectObject(m\_hMemDC, hOldBitmap);
                

                The main part here is the memcpy. I have copied it from my program. If you can't understand what the "m_" variables are, let me know and I will explain

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #7

                Yes, I know what 'm_' is, (member variable), but I also saw that you have m_hMemDC, so you apply the double-buffering tehnique ? Like in this[^] application ? Anyway, I kindly thank you, I'll apply your ideea ...

                1 Reply Last reply
                0
                • S Sivaraman Dhamodharan

                  Google it for "Double Buffering"

                  Programming Article

                  _ Offline
                  _ Offline
                  _Flaviu
                  wrote on last edited by
                  #8

                  I make a little confusion (or big one): double bouffering use a memory device context as member variable, single buffering use a local memory device context ... thank you sirama2004. Best regards !

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups