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. Help with OnPaint

Help with OnPaint

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelptutorialquestion
5 Posts 5 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.
  • S Offline
    S Offline
    S van Leent
    wrote on last edited by
    #1

    Currently, I have this code in my on paint event in a view. CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the following CPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...) However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)");

    L P J J 4 Replies Last reply
    0
    • S S van Leent

      Currently, I have this code in my on paint event in a view. CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the following CPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...) However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)");

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to allocate a bitmap (ideally a DIBSection) of the appropriate size and select it into the device context. The default bitmap in a freshly created DC is 1x1. If you use a DIBSection, you can get direct access to the bitmap's pixel data. This can dramatically speed up pixel manipulation.

      1 Reply Last reply
      0
      • S S van Leent

        Currently, I have this code in my on paint event in a view. CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the following CPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...) However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)");

        P Offline
        P Offline
        Prakash Nadar
        wrote on last edited by
        #3

        The effective way of createing a memory dc.

        HDC memDC = CreateCompatibleDC ( hDC );
        HBITMAP memBM = CreateCompatibleBitmap ( hDC );
        SelectObject ( memDC, memBM );
        

        Interviewer (me) : "Did you install Linux all my yourself?" Candidate : "Yes i installed Linux all my yourself."

        1 Reply Last reply
        0
        • S S van Leent

          Currently, I have this code in my on paint event in a view. CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the following CPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...) However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)");

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          Go look in the "Bitmaps & Pallettes" section of codeproject; both CDibSectionLite and CDibData provide ways of accessing pixels directly without having to select them into a DC first. Note: Accessing pixel data through any DC is very slow. INTP

          1 Reply Last reply
          0
          • S S van Leent

            Currently, I have this code in my on paint event in a view. CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the following CPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...) However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)");

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            As was suggested before you need to create a compatible bitmap. You also need to pass the dc from the paint DC to the CreateCompatibleDC function, not the other way around. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            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