CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2007
    Posts
    47

    writre bitmap file

    I have captured the screen and copy the image to clipboard.
    Now, i want to write the image from clipboard to bitmap file.
    but i don't know how to write a bit map file, any suggestion for me,.please????

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: writre bitmap file

    Open Paint, paste the clipboard buffer and then save it in bmp, jpg or (don't remember what other formats paint can create)

  3. #3
    Join Date
    May 2007
    Posts
    437

    Re: writre bitmap file

    using vc++

    then go through this link

    http://msdn2.microsoft.com/en-us/lib...ax(VS.80).aspx

    read it , may be it will help you

    else best way S_M_A already told you

  4. #4
    Join Date
    May 2005
    Posts
    4,954

    Re: writre bitmap file

    Quote Originally Posted by iwtbapd
    I have captured the screen and copy the image to clipboard.
    Now, i want to write the image from clipboard to bitmap file.
    but i don't know how to write a bit map file, any suggestion for me,.please????
    Look at this thread for sample.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  5. #5
    Join Date
    May 2007
    Posts
    47

    Re: writre bitmap file

    Quote Originally Posted by S_M_A
    Open Paint, paste the clipboard buffer and then save it in bmp, jpg or (don't remember what other formats paint can create)
    Oh it's so easy . But i don't want to do it,i want to create a bitmap file,write the image to the file

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: writre bitmap file

    I'm sorry iwtbapd, I realized that later on but then the post was already there. Of course I could have deleted it but then it would have been loose references.

    I've been thinking about it though. I wonder if Paint actually can be used as a "save-to-file server"? Would be an easy way not to have to bother about different file formats, just open Paint, send a WM_PASTE and then make Paint save to file.

    Hope the answers from golanshahar & ashukasama helped you.

  7. #7
    Join Date
    May 2007
    Posts
    47

    Re: writre bitmap file

    oh,never mind.thank for ur helping !!

  8. #8
    Join Date
    May 2007
    Posts
    4

    Re: writre bitmap file

    I doubt if Paint could be used that way SMA. But after reading this thread, I've determined that it's incredibly easy to write your own "save-to-file server", the key being that almost all (if not all) applications cut and/or copy pictures to the clipboard as bitmaps! So the save-to-file server doesn't have to know about any other formats. Here is what is needed (well, and the Writebmp func).

    Code:
    OpenClipboard(hwnd);
    HBITMAP hbmp = GetClipboardData(CF_BITMAP);
    CloseClipboard();

  9. #9
    Join Date
    May 2007
    Posts
    47

    Re: writre bitmap file

    Quote Originally Posted by tiger12506
    I doubt if Paint could be used that way SMA. But after reading this thread, I've determined that it's incredibly easy to write your own "save-to-file server", the key being that almost all (if not all) applications cut and/or copy pictures to the clipboard as bitmaps! So the save-to-file server doesn't have to know about any other formats. Here is what is needed (well, and the Writebmp func).
    oh, my problem is unable to write bitmap file. When you had a HBITMAP,how could you write this bitmap to file.And i can't u <fstream.h> for using ofstream.
    Can u explain more clearly about your solution

  10. #10
    Join Date
    May 2005
    Posts
    4,954

    Re: writre bitmap file

    Quote Originally Posted by iwtbapd
    oh, my problem is unable to write bitmap file. When you had a HBITMAP,how could you write this bitmap to file.And i can't u <fstream.h> for using ofstream.
    Can u explain more clearly about your solution
    You can use ::GetBitmapBits() to get the raw bits from the image, and ::GetObject() to get the width/height/bpp and you have all the info in order to write it to file.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  11. #11
    Join Date
    Jul 2009
    Posts
    2

    Re: writre bitmap file

    First of all hello people in this great forum. Good post this one.

    I tried using "golanshahar" and "dave2k" tips but I got a nasty incompatibility with bmps.

    In (1) and (2) I capture and save a desktop photo. The photo seems ok, 32 bits depth, good width, high and image, opens ok in windows, paint, photoshop.. BUT when I try to load again using LoadImage function, I got a NULL HANDLE returned with 0 error, when loadImage works okey for any other bmp not generated this way.

    Very confused.. any help appreciated..


    // (1) MAKE DESKTOP PHOTO

    RECT rc;
    HWND hWnd = ::GetDesktopWindow();
    ::GetWindowRect(hWnd, &rc);

    int w = rc.right-rc.left;
    int h = rc.bottom-rc.top;

    HDC hDC = ::GetDC(0);
    HDC memDC = ::CreateCompatibleDC(hDC);
    HBITMAP memBM = ::CreateCompatibleBitmap(hDC, w, h);
    ::SelectObject(memDC, memBM );
    ::BitBlt(memDC, 0, 0, w, h , hDC, rc.left, rc.top , SRCCOPY );

    int size = 4 * w * h;

    BYTE *lpBits = new BYTE[size];

    ::GetBitmapBits((HBITMAP)memBM,size,(int*)lpBits);

    // here you have the data both in memBM and also in rawBits

    ::ReleaseDC( 0, hDC );

    // (2) WRITE TO PHYSICAL FILE BMP

    WriteBmp("photo.bmp", w, h, 32,(int*)lpBits);

    delete [] lpBits;

    // (3) TRY TO LOAD THE PHYSICAL FILE BMP GENERATED
    HBITMAP hMyBmp = (HBITMAP)LoadImage( NULL,
    _T("photo.bmp"),
    IMAGE_BITMAP, 0, 0,
    LR_LOADFROMFILE);

    if ( hMyBmp == NULL )
    {
    //GetLastError();

    CWnd *Display;
    Display = GetDlgItem(IDC_EDIT1);

    CString strError;
    strError.Format(_T("ERROR: %i, NULL HANDLE RETURNED"), GetLastError() );
    LPTSTR lpszError;
    lpszError = strError.GetBuffer(strError.GetLength());
    Display->SetWindowTextW(lpszError);
    }

    Also tried another "writebmp" alike function found in this page http://sarathc.wordpress.com/2007/03...itmap-to-file/ in case there was a problem in writebmp but still the same problem

  12. #12
    Join Date
    May 2005
    Posts
    4,954

    Re: writre bitmap file

    Quote Originally Posted by cobayashi View Post
    First of all hello people in this great forum. Good post this one.

    I tried using "golanshahar" and "dave2k" tips but I got a nasty incompatibility with bmps.

    In (1) and (2) I capture and save a desktop photo. The photo seems ok, 32 bits depth, good width, high and image, opens ok in windows, paint, photoshop.. BUT when I try to load again using LoadImage function, I got a NULL HANDLE returned with 0 error, when loadImage works okey for any other bmp not generated this way.

    Very confused.. any help appreciated..


    // (1) MAKE DESKTOP PHOTO

    RECT rc;
    HWND hWnd = ::GetDesktopWindow();
    ::GetWindowRect(hWnd, &rc);

    int w = rc.right-rc.left;
    int h = rc.bottom-rc.top;

    HDC hDC = ::GetDC(0);
    HDC memDC = ::CreateCompatibleDC(hDC);
    HBITMAP memBM = ::CreateCompatibleBitmap(hDC, w, h);
    ::SelectObject(memDC, memBM );
    ::BitBlt(memDC, 0, 0, w, h , hDC, rc.left, rc.top , SRCCOPY );

    int size = 4 * w * h;

    BYTE *lpBits = new BYTE[size];

    ::GetBitmapBits((HBITMAP)memBM,size,(int*)lpBits);

    // here you have the data both in memBM and also in rawBits

    ::ReleaseDC( 0, hDC );

    // (2) WRITE TO PHYSICAL FILE BMP

    WriteBmp("photo.bmp", w, h, 32,(int*)lpBits);

    delete [] lpBits;

    // (3) TRY TO LOAD THE PHYSICAL FILE BMP GENERATED
    HBITMAP hMyBmp = (HBITMAP)LoadImage( NULL,
    _T("photo.bmp"),
    IMAGE_BITMAP, 0, 0,
    LR_LOADFROMFILE);

    if ( hMyBmp == NULL )
    {
    //GetLastError();

    CWnd *Display;
    Display = GetDlgItem(IDC_EDIT1);

    CString strError;
    strError.Format(_T("ERROR: &#37;i, NULL HANDLE RETURNED"), GetLastError() );
    LPTSTR lpszError;
    lpszError = strError.GetBuffer(strError.GetLength());
    Display->SetWindowTextW(lpszError);
    }

    Also tried another "writebmp" alike function found in this page http://sarathc.wordpress.com/2007/03...itmap-to-file/ in case there was a problem in writebmp but still the same problem
    Did you try?
    Code:
      HBITMAP hMyBmp = (HBITMAP)LoadImage(  NULL, 
                _T("photo.bmp"),
                IMAGE_BITMAP, 0, 0,
                LR_LOADFROMFILE|LR_CREATEDIBSECTION);
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  13. #13
    Join Date
    Jul 2009
    Posts
    2

    Re: writre bitmap file

    Quote Originally Posted by golanshahar View Post
    Did you try?
    Code:
      HBITMAP hMyBmp = (HBITMAP)LoadImage(  NULL, 
                _T("photo.bmp"),
                IMAGE_BITMAP, 0, 0,
                LR_LOADFROMFILE|LR_CREATEDIBSECTION);
    Cheers
    Thank you Golanshahar

    Doesn't work either. I think there is a some small bug or lack of info written in WriteBmp. Bmps generated are good enough to to be seen in apps, including the windows viewer.. but not good enough to be loaded with LoadImage as "Windows NT-based systems, performs stricter validation of LoadImage()" :

    http://support.microsoft.com/kb/329092/en-us/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured