Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

screenshot

rageshctechrageshctech Member Posts: 20
i have took a screenshot by using the code:
hdc=GetDC(hwnd);
GetClientRect(hwnd,&rt);
mdc = CreateDC("Display",NULL,NULL,NULL);
in to the mdc the HDC variable. I want to convert it in to HBITMAP structure and print it in an image box. Can any one help me?

Comments

  • anthrax11anthrax11 Member Posts: 511
    : i have took a screenshot by using the code:
    : hdc=GetDC(hwnd);
    : GetClientRect(hwnd,&rt);
    : mdc = CreateDC("Display",NULL,NULL,NULL);
    : in to the mdc the HDC variable. I want to convert it in to HBITMAP structure and print it in an image box. Can any one help me?
    :

    Try this:
    [code]
    HDC WinDC;
    HDC CopyDC;
    hBitmap HBITMAP;
    RECT rt;

    [green]//Get the size of the window[/green]
    GetWindowRect (hwnd, &rt);


    [green]//Get the DC of the window[/green]
    WinDC = GetDC (hwnd);

    [green]//To get a copy of the window image, you need to
    //make another DC compatible with the original[/green]
    CopyDC = CreateCompatibleDC (WinDC);

    [green]//Create a bitmap compatible with the DC[/green]
    hBitmap = CreateCompatibleBitmap (WinDC,
    rt.right - rt.left, [green]//width[/green]
    rt.bottom - rt.top);[green]//height[/green]

    [green]//Associate the bitmap with the DC[/green]
    SelectObject (CopyDC, hBitmap);

    [green]//Copy the window DC to the compatible DC[/green]
    BitBlt (CopyDC, [green]//destination[/green]
    0,0,
    rt.right - rt.left, [green]//width[/green]
    rt.bottom - rt.top, [green]//height[/green]
    WinDC, [green]//source[/green]
    0, 0,
    SRCCOPY);

    [green]//We don`t need the DCs anymore[/green]
    ReleaseDC(hwnd, WinDC);
    ReleaseDC(hwnd, CopyDC);

    [green]//Now hBitmap is the handle of the bitmap that contains the screenshot
    //I`m not sure what you want to do with it now, but to display
    //the screenshot you could copy the compatible DC back to the screen DC
    //using BitBlt[/green]
    [/code]
    My C is still a bit weak, but I hope I got it right;)
    Feel free to ask more questions if something didn`t quite work out..
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : : i have took a screenshot by using the code:
    : : hdc=GetDC(hwnd);
    : : GetClientRect(hwnd,&rt);
    : : mdc = CreateDC("Display",NULL,NULL,NULL);
    : : in to the mdc the HDC variable. I want to convert it in to HBITMAP structure and print it in an image box. Can any one help me?
    : :
    :
    : Try this:
    : [code]
    : HDC WinDC;
    : HDC CopyDC;
    : hBitmap HBITMAP;
    : RECT rt;
    :
    : [green]//Get the size of the window[/green]
    : GetWindowRect (hwnd, &rt);
    :
    :
    : [green]//Get the DC of the window[/green]
    : WinDC = GetDC (hwnd);
    :
    : [green]//To get a copy of the window image, you need to
    : //make another DC compatible with the original[/green]
    : CopyDC = CreateCompatibleDC (WinDC);
    :
    : [green]//Create a bitmap compatible with the DC[/green]
    : hBitmap = CreateCompatibleBitmap (WinDC,
    : rt.right - rt.left, [green]//width[/green]
    : rt.bottom - rt.top);[green]//height[/green]
    :
    : [green]//Associate the bitmap with the DC[/green]
    : SelectObject (CopyDC, hBitmap);
    :
    : [green]//Copy the window DC to the compatible DC[/green]
    : BitBlt (CopyDC, [green]//destination[/green]
    : 0,0,
    : rt.right - rt.left, [green]//width[/green]
    : rt.bottom - rt.top, [green]//height[/green]
    : WinDC, [green]//source[/green]
    : 0, 0,
    : SRCCOPY);
    :
    : [green]//We don`t need the DCs anymore[/green]
    : ReleaseDC(hwnd, WinDC);
    : ReleaseDC(hwnd, CopyDC);
    :
    : [green]//Now hBitmap is the handle of the bitmap that contains the screenshot
    : //I`m not sure what you want to do with it now, but to display
    : //the screenshot you could copy the compatible DC back to the screen DC
    : //using BitBlt[/green]
    : [/code]
    : My C is still a bit weak, but I hope I got it right;)
    : Feel free to ask more questions if something didn`t quite work out..
    :
    [blue]hBitmap is useless, until you de-select it back from memory DC. Also, CreateCompatibleDC() must be closed with DeleteDC() - not ReleaseDC()[/blue]
  • anthrax11anthrax11 Member Posts: 511
    : : : i have took a screenshot by using the code:
    : : : hdc=GetDC(hwnd);
    : : : GetClientRect(hwnd,&rt);
    : : : mdc = CreateDC("Display",NULL,NULL,NULL);
    : : : in to the mdc the HDC variable. I want to convert it in to HBITMAP structure and print it in an image box. Can any one help me?
    : : :
    : :
    : : Try this:
    : : [code]
    : : HDC WinDC;
    : : HDC CopyDC;
    : : hBitmap HBITMAP;
    : : RECT rt;
    : :
    : : [green]//Get the size of the window[/green]
    : : GetWindowRect (hwnd, &rt);
    : :
    : :
    : : [green]//Get the DC of the window[/green]
    : : WinDC = GetDC (hwnd);
    : :
    : : [green]//To get a copy of the window image, you need to
    : : //make another DC compatible with the original[/green]
    : : CopyDC = CreateCompatibleDC (WinDC);
    : :
    : : [green]//Create a bitmap compatible with the DC[/green]
    : : hBitmap = CreateCompatibleBitmap (WinDC,
    : : rt.right - rt.left, [green]//width[/green]
    : : rt.bottom - rt.top);[green]//height[/green]
    : :
    : : [green]//Associate the bitmap with the DC[/green]
    : : SelectObject (CopyDC, hBitmap);
    : :
    : : [green]//Copy the window DC to the compatible DC[/green]
    : : BitBlt (CopyDC, [green]//destination[/green]
    : : 0,0,
    : : rt.right - rt.left, [green]//width[/green]
    : : rt.bottom - rt.top, [green]//height[/green]
    : : WinDC, [green]//source[/green]
    : : 0, 0,
    : : SRCCOPY);
    : :
    : : [green]//We don`t need the DCs anymore[/green]
    : : ReleaseDC(hwnd, WinDC);
    : : ReleaseDC(hwnd, CopyDC);
    : :
    : : [green]//Now hBitmap is the handle of the bitmap that contains the screenshot
    : : //I`m not sure what you want to do with it now, but to display
    : : //the screenshot you could copy the compatible DC back to the screen DC
    : : //using BitBlt[/green]
    : : [/code]
    : : My C is still a bit weak, but I hope I got it right;)
    : : Feel free to ask more questions if something didn`t quite work out..
    : :
    : [blue]hBitmap is useless, until you de-select it back from memory DC. Also, CreateCompatibleDC() must be closed with DeleteDC() - not ReleaseDC()[/blue]
    :
    [green]I see, thanks for pointing that out.[green]
  • rageshctechrageshctech Member Posts: 20
    : : : : i have took a screenshot by using the code:
    : : : : hdc=GetDC(hwnd);
    : : : : GetClientRect(hwnd,&rt);
    : : : : mdc = CreateDC("Display",NULL,NULL,NULL);
    : : : : in to the mdc the HDC variable. I want to convert it in to HBITMAP structure and print it in an image box. Can any one help me?
    : : : :
    : : :
    : : : Try this:
    : : : [code]
    : : : HDC WinDC;
    : : : HDC CopyDC;
    : : : hBitmap HBITMAP;
    : : : RECT rt;
    : : :
    : : : [green]//Get the size of the window[/green]
    : : : GetWindowRect (hwnd, &rt);
    : : :
    : : :
    : : : [green]//Get the DC of the window[/green]
    : : : WinDC = GetDC (hwnd);
    : : :
    : : : [green]//To get a copy of the window image, you need to
    : : : //make another DC compatible with the original[/green]
    : : : CopyDC = CreateCompatibleDC (WinDC);
    : : :
    : : : [green]//Create a bitmap compatible with the DC[/green]
    : : : hBitmap = CreateCompatibleBitmap (WinDC,
    : : : rt.right - rt.left, [green]//width[/green]
    : : : rt.bottom - rt.top);[green]//height[/green]
    : : :
    : : : [green]//Associate the bitmap with the DC[/green]
    : : : SelectObject (CopyDC, hBitmap);
    : : :
    : : : [green]//Copy the window DC to the compatible DC[/green]
    : : : BitBlt (CopyDC, [green]//destination[/green]
    : : : 0,0,
    : : : rt.right - rt.left, [green]//width[/green]
    : : : rt.bottom - rt.top, [green]//height[/green]
    : : : WinDC, [green]//source[/green]
    : : : 0, 0,
    : : : SRCCOPY);
    : : :
    : : : [green]//We don`t need the DCs anymore[/green]
    : : : ReleaseDC(hwnd, WinDC);
    : : : ReleaseDC(hwnd, CopyDC);
    : : :
    : : : [green]//Now hBitmap is the handle of the bitmap that contains the screenshot
    : : : //I`m not sure what you want to do with it now, but to display
    : : : //the screenshot you could copy the compatible DC back to the screen DC
    : : : //using BitBlt[/green]
    : : : [/code]
    : : : My C is still a bit weak, but I hope I got it right;)
    : : : Feel free to ask more questions if something didn`t quite work out..
    : : :
    : : [blue]hBitmap is useless, until you de-select it back from memory DC. Also, CreateCompatibleDC() must be closed with DeleteDC() - not ReleaseDC()[/blue]
    : :
    : [green]I see, thanks for pointing that out.[green]
    :
    Then how can I de-select it back. Al so I want the screenshot of the whole screen and not just the window. (HBITMAP from my code above will be more useful for me and will be thannk ful. ) Thanks.


  • anthrax11anthrax11 Member Posts: 511
    : : : [blue]hBitmap is useless, until you de-select it back from memory DC. Also, CreateCompatibleDC() must be closed with DeleteDC() - not ReleaseDC()[/blue]
    : : :
    : : [green]I see, thanks for pointing that out.[/green]
    : :
    : Then how can I de-select it back. Al so I want the screenshot of the whole screen and not just the window. (HBITMAP from my code above will be more useful for me and will be thannk ful. ) Thanks.
    :
    :
    [green]To be honest, I don`t know what AsmGuru62 means by "de-selecting the bitmap back". I`ve tested the code and it worked fine, hBitmap was totally usable. He was definately right about DeleteDC, though.

    Anyway, to get a screenshot of the whole screen, replace the parameter of GetDC with NULL. Also, you`ll need another way of getting the width and height of the DC, [blue]GetDeviceCaps[/blue] can do that for you. You can use this method for window DCs as well, instead of GetWindowRect, that is.[/green]
    [code]
    HDC WinDC;
    HDC CopyDC;
    HBITMAP hBitmap;
    HWND Button;
    ULONG bWidth, bHeight;

    [green]// Use either NULL for the screen or hwnd for a window[/green]
    WinDC = GetDC (NULL)
    CopyDC = CreateCompatibleDC (WinDC);
    [blue]
    bWidth = GetDeviceCaps(WinDC, HORZRES);
    bHeight = GetDeviceCaps(WinDC, VERTRES);
    [/blue]
    hBitmap = CreateCompatibleBitmap (WinDC, bWidth, bHeight);

    SelectObject (CopyDC, hBitmap);

    BitBlt (CopyDC,
    0,0,
    bWidth, bHeight,
    WinDC,
    0, 0,
    SRCCOPY);

    ReleaseDC(NULL, WinDC);
    DeleteDC(CopyDC);

    [green]//This is what I used to test the bitmap[/green]
    Button=CreateWindowEx(0, "BUTTON", NULL,
    WS_CHILD | WS_VISIBLE | BS_BITMAP,
    20,20,500,500,
    hwnd,0,0,0);
    SendMessage(Button, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
    [/code]
  • rageshctechrageshctech Member Posts: 20
    : : : : [blue]hBitmap is useless, until you de-select it back from memory DC. Also, CreateCompatibleDC() must be closed with DeleteDC() - not ReleaseDC()[/blue]
    : : : :
    : : : [green]I see, thanks for pointing that out.[/green]
    : : :
    : : Then how can I de-select it back. Al so I want the screenshot of the whole screen and not just the window. (HBITMAP from my code above will be more useful for me and will be thannk ful. ) Thanks.
    : :
    : :
    : [green]To be honest, I don`t know what AsmGuru62 means by "de-selecting the bitmap back". I`ve tested the code and it worked fine, hBitmap was totally usable. He was definately right about DeleteDC, though.
    :
    : Anyway, to get a screenshot of the whole screen, replace the parameter of GetDC with NULL. Also, you`ll need another way of getting the width and height of the DC, [blue]GetDeviceCaps[/blue] can do that for you. You can use this method for window DCs as well, instead of GetWindowRect, that is.[/green]
    : [code]
    : HDC WinDC;
    : HDC CopyDC;
    : HBITMAP hBitmap;
    : HWND Button;
    : ULONG bWidth, bHeight;
    :
    : [green]// Use either NULL for the screen or hwnd for a window[/green]
    : WinDC = GetDC (NULL)
    : CopyDC = CreateCompatibleDC (WinDC);
    : [blue]
    : bWidth = GetDeviceCaps(WinDC, HORZRES);
    : bHeight = GetDeviceCaps(WinDC, VERTRES);
    : [/blue]
    : hBitmap = CreateCompatibleBitmap (WinDC, bWidth, bHeight);
    :
    : SelectObject (CopyDC, hBitmap);
    :
    : BitBlt (CopyDC,
    : 0,0,
    : bWidth, bHeight,
    : WinDC,
    : 0, 0,
    : SRCCOPY);
    :
    : ReleaseDC(NULL, WinDC);
    : DeleteDC(CopyDC);
    :
    : [green]//This is what I used to test the bitmap[/green]
    : Button=CreateWindowEx(0, "BUTTON", NULL,
    : WS_CHILD | WS_VISIBLE | BS_BITMAP,
    : 20,20,500,500,
    : hwnd,0,0,0);
    : SendMessage(Button, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
    : [/code]
    :
    Thank you. If you dont mind consider my question above.
  • anthrax11anthrax11 Member Posts: 511
    : : : : : [blue]hBitmap is useless, until you de-select it back from memory DC. Also, CreateCompatibleDC() must be closed with DeleteDC() - not ReleaseDC()[/blue]
    : : : : :
    : : : : [green]I see, thanks for pointing that out.[/green]
    : : : :
    : : : Then how can I de-select it back. Al so I want the screenshot of the whole screen and not just the window. (HBITMAP from my code above will be more useful for me and will be thannk ful. ) Thanks.
    : : :
    : : :
    : : [green]To be honest, I don`t know what AsmGuru62 means by "de-selecting the bitmap back". I`ve tested the code and it worked fine, hBitmap was totally usable. He was definately right about DeleteDC, though.
    : :
    : : Anyway, to get a screenshot of the whole screen, replace the parameter of GetDC with NULL. Also, you`ll need another way of getting the width and height of the DC, [blue]GetDeviceCaps[/blue] can do that for you. You can use this method for window DCs as well, instead of GetWindowRect, that is.[/green]
    : : [code]
    : : HDC WinDC;
    : : HDC CopyDC;
    : : HBITMAP hBitmap;
    : : HWND Button;
    : : ULONG bWidth, bHeight;
    : :
    : : [green]// Use either NULL for the screen or hwnd for a window[/green]
    : : WinDC = GetDC (NULL)
    : : CopyDC = CreateCompatibleDC (WinDC);
    : : [blue]
    : : bWidth = GetDeviceCaps(WinDC, HORZRES);
    : : bHeight = GetDeviceCaps(WinDC, VERTRES);
    : : [/blue]
    : : hBitmap = CreateCompatibleBitmap (WinDC, bWidth, bHeight);
    : :
    : : SelectObject (CopyDC, hBitmap);
    : :
    : : BitBlt (CopyDC,
    : : 0,0,
    : : bWidth, bHeight,
    : : WinDC,
    : : 0, 0,
    : : SRCCOPY);
    : :
    : : ReleaseDC(NULL, WinDC);
    : : DeleteDC(CopyDC);
    : :
    : : [green]//This is what I used to test the bitmap[/green]
    : : Button=CreateWindowEx(0, "BUTTON", NULL,
    : : WS_CHILD | WS_VISIBLE | BS_BITMAP,
    : : 20,20,500,500,
    : : hwnd,0,0,0);
    : : SendMessage(Button, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
    : : [/code]
    : :
    : Thank you. If you dont mind consider my question above.
    :
    [green]
    Hmm.. I believe I already did answer both the questions or am I missing something? Or do you mean I should answer your other post about saving the bitmap to a file?[/green]
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [b][red]This message was edited by AsmGuru62 at 2007-1-22 22:42:8[/red][/b][hr]
    [blue]This is one good source:
    http://msdn2.microsoft.com/en-us/library/ms533272.aspx

    ... and some code (watch how object de-selected back):
    http://msdn2.microsoft.com/en-us/library/ms533262.aspx

    Also, I think I have some code on the matter on my site:
    http://www.codexxi.com/MyBlocks.html#atPrntScrn

    it is not MFC - just Win32.
    [/blue]


  • rageshctechrageshctech Member Posts: 20
    : : : : : : [blue]hBitmap is useless, until you de-select it back from memory DC. Also, CreateCompatibleDC() must be closed with DeleteDC() - not ReleaseDC()[/blue]
    : : : : : :
    : : : : : [green]I see, thanks for pointing that out.[/green]
    : : : : :
    : : : : Then how can I de-select it back. Al so I want the screenshot of the whole screen and not just the window. (HBITMAP from my code above will be more useful for me and will be thannk ful. ) Thanks.
    : : : :
    : : : :
    : : : [green]To be honest, I don`t know what AsmGuru62 means by "de-selecting the bitmap back". I`ve tested the code and it worked fine, hBitmap was totally usable. He was definately right about DeleteDC, though.
    : : :
    : : : Anyway, to get a screenshot of the whole screen, replace the parameter of GetDC with NULL. Also, you`ll need another way of getting the width and height of the DC, [blue]GetDeviceCaps[/blue] can do that for you. You can use this method for window DCs as well, instead of GetWindowRect, that is.[/green]
    : : : [code]
    : : : HDC WinDC;
    : : : HDC CopyDC;
    : : : HBITMAP hBitmap;
    : : : HWND Button;
    : : : ULONG bWidth, bHeight;
    : : :
    : : : [green]// Use either NULL for the screen or hwnd for a window[/green]
    : : : WinDC = GetDC (NULL)
    : : : CopyDC = CreateCompatibleDC (WinDC);
    : : : [blue]
    : : : bWidth = GetDeviceCaps(WinDC, HORZRES);
    : : : bHeight = GetDeviceCaps(WinDC, VERTRES);
    : : : [/blue]
    : : : hBitmap = CreateCompatibleBitmap (WinDC, bWidth, bHeight);
    : : :
    : : : SelectObject (CopyDC, hBitmap);
    : : :
    : : : BitBlt (CopyDC,
    : : : 0,0,
    : : : bWidth, bHeight,
    : : : WinDC,
    : : : 0, 0,
    : : : SRCCOPY);
    : : :
    : : : ReleaseDC(NULL, WinDC);
    : : : DeleteDC(CopyDC);
    : : :
    : : : [green]//This is what I used to test the bitmap[/green]
    : : : Button=CreateWindowEx(0, "BUTTON", NULL,
    : : : WS_CHILD | WS_VISIBLE | BS_BITMAP,
    : : : 20,20,500,500,
    : : : hwnd,0,0,0);
    : : : SendMessage(Button, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
    : : : [/code]
    : : :
    : : Thank you. If you dont mind consider my question above.
    : :
    : [green]
    : Hmm.. I believe I already did answer both the questions or am I missing something? Or do you mean I should answer your other post about saving the bitmap to a file?[/green]
    [red]ya, From the hbitmap. and if you know also tell me how to retrieve it in to HBITMAP from the file[/red]

Sign In or Register to comment.