CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Saving view

  1. #1
    Join Date
    Jan 2009
    Posts
    399

    Saving view

    I have an application that compound more CBitmaps into single one, and all of them I spread it into CScrollView. How can I save entire view (helped by CImage), but not just visible part in window client ? I saw on internet a lot of examples of how to save CDC, but just visible part ... in my case this is not suitable ... could you help me ?

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Saving view

    You save that absolutely same way: create memory dc, create compatible bitmap, draw there as you wish, detach bitmap from dc, save bitmap to file.
    Best regards,
    Igor

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Saving view

    Once you have the (composed) bitmap, there's no sweat to attach it to a CImage object, then save it in a file.

    Example
    Code:
    HRESULT CFoo::SaveCBitmapToFile(CBitmap* pBitmap, LPCTSTR szFilePath)
    {
        ASSERT_VALID(pBitmap);
    
        CImage image;
        image.Attach((HBITMAP)pBitmap->GetSafeHandle());
        HRESULT hr = image.Save(szFilePath);
        image.Detach(); // Do not forget this!
        
        return hr;
    }
    // like a walk in the park.

    See
    Last edited by ovidiucucu; October 3rd, 2015 at 06:06 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Saving view

    [ additional note ]

    CImage is a wonderful ATL/MFC class which can help you to save lines, time and improve the code readability and maintainability, by making not obviously necessary to write yourself code for creating memory DCs, compatible bitmaps, bit-blitting, and so on, and so on.

    Example
    Code:
        // ...
        CImage imageToSave;
        imageToSave.Create(nDestWidth, nDestHeight, nBPP);
        HDC hDestDC = imageToSave.GetDC();
    
        // fill the destination background
        CDC::FromHandle(hDestDC)->FillSolidRect(0, 0, nDestWidth, nDestHeight, crBack);
    
        // draw a bitmap into the destination at coordinates x, y
        CImage image1;
        image1.Attach((HBITMAP)pBitmap1->GetSafeHandle());
        image1.Draw(hDestDC, x, y);
    
        // draw anything else your muscles want
        // ...
    
        // save the destination image to a file
        HRESULT hr = imageToSave.Save(pszFilePath);
    
    
        image1.Detach();         // Do not forget Detach if a bitmap was previously attached!
        imageToSave.ReleaseDC(); // Do not forget ReleaseDC if GetDC was previously called!
        // ...
    See also
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Jan 2009
    Posts
    399

    Re: Saving view

    Sorry for later response ... I have wrote the same code, except one line:
    Code:
    Image.Attach((HBITMAP)pBitmap->m_hObject);
    instead of
    Code:
    image.Attach((HBITMAP)pBitmap->GetSafeHandle());
    I noticed that with my code, I couldn't save extra large images, with yours, I do. Thank you.
    Last edited by mesajflaviu; October 16th, 2015 at 03:25 AM.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Saving view

    Quote Originally Posted by mesajflaviu View Post
    Sorry for later response ... I have wrote the same code, except one line:
    Code:
    Image.Attach((HBITMAP)pBitmap->m_hObject);
    instead of
    Code:
    image.Attach((HBITMAP)pBitmap->GetSafeHandle());
    Generally, if using pointers, calling CGdiObject::GetSafeHandle is safer than directly accessing the m_hObject member.
    In our case, if pBitmap is NULL, pBitmap->GetSafeHandle() will return NULL while pBitmap->m_hObject will lead in an access violation and the application will crash.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

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