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. Image flickers when Invalidate() is called.

Image flickers when Invalidate() is called.

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresbusinesstutorialquestion
7 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.
  • J Offline
    J Offline
    jazzkiller
    wrote on last edited by
    #1

    Hi all, I was able to display an raw image (8bit per pixel) using StretchDIBits method. However, my OnPaint() is called alot so the image is flickering like crazy. How can I get rid of this flickering business. I've read tutorials on double buffering, however, I still don't understand how to apply it to this case where the image is from an array of bytes. Thanks,

    U C 2 Replies Last reply
    0
    • J jazzkiller

      Hi all, I was able to display an raw image (8bit per pixel) using StretchDIBits method. However, my OnPaint() is called alot so the image is flickering like crazy. How can I get rid of this flickering business. I've read tutorials on double buffering, however, I still don't understand how to apply it to this case where the image is from an array of bytes. Thanks,

      U Offline
      U Offline
      User 900278
      wrote on last edited by
      #2

      First store your Image in MemoryDC. then call Invalidaterect(); You will get code in KRUGLANSKI.

      1 Reply Last reply
      0
      • J jazzkiller

        Hi all, I was able to display an raw image (8bit per pixel) using StretchDIBits method. However, my OnPaint() is called alot so the image is flickering like crazy. How can I get rid of this flickering business. I've read tutorials on double buffering, however, I still don't understand how to apply it to this case where the image is from an array of bytes. Thanks,

        C Offline
        C Offline
        Christopher Lloyd
        wrote on last edited by
        #3

        If the window flickers it must mean that something else is being drawn to it between each of your StretchDIBits calls (otherwise you'd just be blitting the same image over the top of itself and you'd see no flicker). Probably what's causing the flicker is that your window's background is being erased as part of the repaint. However, if your image fills the window this erasing is entirely unnecessary and you should prevent it. An easy way to do this is to trap the WM_ERASEBKGND message and return zero -you should then find that that flickering goes away.

        J 1 Reply Last reply
        0
        • C Christopher Lloyd

          If the window flickers it must mean that something else is being drawn to it between each of your StretchDIBits calls (otherwise you'd just be blitting the same image over the top of itself and you'd see no flicker). Probably what's causing the flicker is that your window's background is being erased as part of the repaint. However, if your image fills the window this erasing is entirely unnecessary and you should prevent it. An easy way to do this is to trap the WM_ERASEBKGND message and return zero -you should then find that that flickering goes away.

          J Offline
          J Offline
          jazzkiller
          wrote on last edited by
          #4

          Hi, I have tried to override OnEraseBkgnd, the problem still occurs. I am writing and ViewLCD application for a camera (just like a digital camera with an LCD at the back to view the scene that the camera is pointing to). Thanks,

          C 1 Reply Last reply
          0
          • J jazzkiller

            Hi, I have tried to override OnEraseBkgnd, the problem still occurs. I am writing and ViewLCD application for a camera (just like a digital camera with an LCD at the back to view the scene that the camera is pointing to). Thanks,

            C Offline
            C Offline
            Christopher Lloyd
            wrote on last edited by
            #5

            Well, the flicker will only appear if something other than your bitmap is being drawn between your updates - the obvious candidate is erasing the backrgound but is there anything else in your WM_PAINT processing that might be causing this?

            J 1 Reply Last reply
            0
            • C Christopher Lloyd

              Well, the flicker will only appear if something other than your bitmap is being drawn between your updates - the obvious candidate is erasing the backrgound but is there anything else in your WM_PAINT processing that might be causing this?

              J Offline
              J Offline
              jazzkiller
              wrote on last edited by
              #6

              This is all in my OnPaint() of a CChildView: CPaintDC dc(this); // device context for painting CDC* pDC = &dc; // Convert to context device pointer HDC hdc = pDC->GetSafeHdc(); // Prepare Header for image BITMAPINFO *m_pBitmapInfo; m_pBitmapInfo = (BITMAPINFO*)new char[sizeof(BITMAPINFO) + sizeof(RGBQUAD)*256]; // Load header m_pBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); m_pBitmapInfo->bmiHeader.biWidth = ImX; m_pBitmapInfo->bmiHeader.biHeight = -ImY; m_pBitmapInfo->bmiHeader.biPlanes = 1; m_pBitmapInfo->bmiHeader.biBitCount = 8; m_pBitmapInfo->bmiHeader.biCompression = BI_RGB; m_pBitmapInfo->bmiHeader.biSizeImage = 0; m_pBitmapInfo->bmiHeader.biXPelsPerMeter = 0; m_pBitmapInfo->bmiHeader.biYPelsPerMeter = 0; m_pBitmapInfo->bmiHeader.biClrUsed = 256; m_pBitmapInfo->bmiHeader.biClrImportant = 0; int i; for ( i = 0; i < 256; i++ ) { m_pBitmapInfo->bmiColors[i].rgbBlue = i; m_pBitmapInfo->bmiColors[i].rgbGreen = i; m_pBitmapInfo->bmiColors[i].rgbRed = i; m_pBitmapInfo->bmiColors[i].rgbReserved = 0; } // Stretch pixels out to screen StretchDIBits(hdc, xCord,yCord,ImX,ImY, 0,0,ImX,ImY, img, //------->>> Pointer to array of bits, a CChildView variable member. m_pBitmapInfo, DIB_RGB_COLORS, SRCCOPY);

              C 1 Reply Last reply
              0
              • J jazzkiller

                This is all in my OnPaint() of a CChildView: CPaintDC dc(this); // device context for painting CDC* pDC = &dc; // Convert to context device pointer HDC hdc = pDC->GetSafeHdc(); // Prepare Header for image BITMAPINFO *m_pBitmapInfo; m_pBitmapInfo = (BITMAPINFO*)new char[sizeof(BITMAPINFO) + sizeof(RGBQUAD)*256]; // Load header m_pBitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); m_pBitmapInfo->bmiHeader.biWidth = ImX; m_pBitmapInfo->bmiHeader.biHeight = -ImY; m_pBitmapInfo->bmiHeader.biPlanes = 1; m_pBitmapInfo->bmiHeader.biBitCount = 8; m_pBitmapInfo->bmiHeader.biCompression = BI_RGB; m_pBitmapInfo->bmiHeader.biSizeImage = 0; m_pBitmapInfo->bmiHeader.biXPelsPerMeter = 0; m_pBitmapInfo->bmiHeader.biYPelsPerMeter = 0; m_pBitmapInfo->bmiHeader.biClrUsed = 256; m_pBitmapInfo->bmiHeader.biClrImportant = 0; int i; for ( i = 0; i < 256; i++ ) { m_pBitmapInfo->bmiColors[i].rgbBlue = i; m_pBitmapInfo->bmiColors[i].rgbGreen = i; m_pBitmapInfo->bmiColors[i].rgbRed = i; m_pBitmapInfo->bmiColors[i].rgbReserved = 0; } // Stretch pixels out to screen StretchDIBits(hdc, xCord,yCord,ImX,ImY, 0,0,ImX,ImY, img, //------->>> Pointer to array of bits, a CChildView variable member. m_pBitmapInfo, DIB_RGB_COLORS, SRCCOPY);

                C Offline
                C Offline
                Christopher Lloyd
                wrote on last edited by
                #7

                Looks OK to me, although I'd set up the BitmapInfo record outside the paint processing. If ImX, ImY change between calls you can set just them. This would get that nasty for loop out of there! Anyhow, back to the flicker - the other possibility is that your WindowClass (that's class as in WNDCLASSEX) has a background brush defined. In this case I seem to recall that BeginPaint will erase the background without raising a WM_ERASEBKGND message.

                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