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. hdc to Bitmap

hdc to Bitmap

Scheduled Pinned Locked Moved C / C++ / MFC
comgraphicsquestion
8 Posts 2 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.
  • G Offline
    G Offline
    GauranG Shah
    wrote on last edited by
    #1

    Hello friends, I require to show the dc into the Picture Control as a bitmap. For that I am using the code like below, but only shows blank bitmap in the Picture control.

    HDC DeskDC = GetDC(NULL);
    HDC hdcStatic = GetDC(hWndStatic);
    HBITMAP hb = CreateCompatibleBitmap(hdcStatic,200,200);
    SelectObject(hdcStatic,hb);
    SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
    BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
    SendMessage(hWndStatic, STM_SETIMAGE , IMAGE_BITMAP,(LPARAM)hb);

    Please tell me if you know what is going wrong.

    [ Screen Capture ][ Tool Tip ]

    M 1 Reply Last reply
    0
    • G GauranG Shah

      Hello friends, I require to show the dc into the Picture Control as a bitmap. For that I am using the code like below, but only shows blank bitmap in the Picture control.

      HDC DeskDC = GetDC(NULL);
      HDC hdcStatic = GetDC(hWndStatic);
      HBITMAP hb = CreateCompatibleBitmap(hdcStatic,200,200);
      SelectObject(hdcStatic,hb);
      SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
      BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
      SendMessage(hWndStatic, STM_SETIMAGE , IMAGE_BITMAP,(LPARAM)hb);

      Please tell me if you know what is going wrong.

      [ Screen Capture ][ Tool Tip ]

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      What are you trying to do??? You want the bitmap to be a copy of a a 200x200 rect of the screen? That's fine. You should select the hb bitmap back out of the memory DC before you hand it off to the static (picture) control.HGDIOBJ hOldBM = SelectObject(hdcStatic,hb);
      SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
      BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
      SelectObject(hdcStatic, hOldBM);

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      G 2 Replies Last reply
      0
      • M Mark Salsbery

        What are you trying to do??? You want the bitmap to be a copy of a a 200x200 rect of the screen? That's fine. You should select the hb bitmap back out of the memory DC before you hand it off to the static (picture) control.HGDIOBJ hOldBM = SelectObject(hdcStatic,hb);
        SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
        BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
        SelectObject(hdcStatic, hOldBM);

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        G Offline
        G Offline
        GauranG Shah
        wrote on last edited by
        #3

        Thnk you vary much for you help. But still I am not able to perform the thing. I am doing something like this as you have told me to do. But getting NULL in hOLdBM. and So getting BLANK Image (black Image)

        HDC DeskDC = GetDC(NULL);
        HDC hdcStatic = GetDC(hWndStatic);
        HBITMAP hb = CreateCompatibleBitmap(hdcStatic,200,200);
        //Getting NULL(0x000000) in hOldBM. When I check The error Code it Show 0x00000005. Means "ACCESS DENIED"
        HGDIOBJ hOldBM = SelectObject(hdcStatic,hb);
        SelectObject(hdcStatic, hOldBM);

        SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
        BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
        SelectObject(hdcStatic, hOldBM);

        //Gettting noting in the Picture Control
        SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hOldBM);

        //Blank Image (Black Colored)
        SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hb);

        -- modified at 2:00 Friday 12th October, 2007

        [ Screen Capture ][ Tool Tip ]

        M 1 Reply Last reply
        0
        • G GauranG Shah

          Thnk you vary much for you help. But still I am not able to perform the thing. I am doing something like this as you have told me to do. But getting NULL in hOLdBM. and So getting BLANK Image (black Image)

          HDC DeskDC = GetDC(NULL);
          HDC hdcStatic = GetDC(hWndStatic);
          HBITMAP hb = CreateCompatibleBitmap(hdcStatic,200,200);
          //Getting NULL(0x000000) in hOldBM. When I check The error Code it Show 0x00000005. Means "ACCESS DENIED"
          HGDIOBJ hOldBM = SelectObject(hdcStatic,hb);
          SelectObject(hdcStatic, hOldBM);

          SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
          BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
          SelectObject(hdcStatic, hOldBM);

          //Gettting noting in the Picture Control
          SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hOldBM);

          //Blank Image (Black Colored)
          SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hb);

          -- modified at 2:00 Friday 12th October, 2007

          [ Screen Capture ][ Tool Tip ]

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Try this

          HDC DeskDC = GetDC(NULL);
          HDC hdcMem = CreateCompatibleDC(0);
          HBITMAP hb = CreateCompatibleBitmap(DeskDC,200,200); //<-- Fixed

          HGDIOBJ hOldBM = SelectObject(hdcMem,hb);

          BitBlt(hdcMem,0,0,200,200,DeskDC,0,0,SRCCOPY);

          SelectObject(hdcMem, hOldBM);

          SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;

          SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hb);

          *edit* Fixed my bug

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          G 1 Reply Last reply
          0
          • M Mark Salsbery

            Try this

            HDC DeskDC = GetDC(NULL);
            HDC hdcMem = CreateCompatibleDC(0);
            HBITMAP hb = CreateCompatibleBitmap(DeskDC,200,200); //<-- Fixed

            HGDIOBJ hOldBM = SelectObject(hdcMem,hb);

            BitBlt(hdcMem,0,0,200,200,DeskDC,0,0,SRCCOPY);

            SelectObject(hdcMem, hOldBM);

            SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;

            SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hb);

            *edit* Fixed my bug

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            G Offline
            G Offline
            GauranG Shah
            wrote on last edited by
            #5

            Hi Thnx a lot. It works. But it displays only monochrome bitmap. I.e. the image I m gettting in static control is Blak and white image.

            [ Screen Capture ][ Tool Tip ]

            M 1 Reply Last reply
            0
            • M Mark Salsbery

              What are you trying to do??? You want the bitmap to be a copy of a a 200x200 rect of the screen? That's fine. You should select the hb bitmap back out of the memory DC before you hand it off to the static (picture) control.HGDIOBJ hOldBM = SelectObject(hdcStatic,hb);
              SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
              BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
              SelectObject(hdcStatic, hOldBM);

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              G Offline
              G Offline
              GauranG Shah
              wrote on last edited by
              #6

              Thnx a lot for you help I just made the little change and its working..................:) HDC DeskDC = GetDC(NULL); HDC hdcMem = CreateCompatibleDC(DeskDC); HBITMAP hb = CreateCompatibleBitmap(DeskDC,200,200); HGDIOBJ hOldBM = SelectObject(hdcMem,hb); BitBlt(hdcMem,0,0,200,200,DeskDC,0,0,SRCCOPY); SelectObject(hdcMem, hOldBM); SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ; SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hb);

              [ Screen Capture ][ Tool Tip ]

              M 1 Reply Last reply
              0
              • G GauranG Shah

                Hi Thnx a lot. It works. But it displays only monochrome bitmap. I.e. the image I m gettting in static control is Blak and white image.

                [ Screen Capture ][ Tool Tip ]

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                I screwed up - see my modified post.  Sorry about that :)

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                • G GauranG Shah

                  Thnx a lot for you help I just made the little change and its working..................:) HDC DeskDC = GetDC(NULL); HDC hdcMem = CreateCompatibleDC(DeskDC); HBITMAP hb = CreateCompatibleBitmap(DeskDC,200,200); HGDIOBJ hOldBM = SelectObject(hdcMem,hb); BitBlt(hdcMem,0,0,200,200,DeskDC,0,0,SRCCOPY); SelectObject(hdcMem, hOldBM); SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ; SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hb);

                  [ Screen Capture ][ Tool Tip ]

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  You found it....good :) Cheers!

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  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