CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2014
    Posts
    4

    paste a SetClipboardData(CF_DIB) image to powerpoint?

    Hi
    I have a VisualC++ program to display image data to the screen. The data is a 24-bit, RGB bitmap that is properly shown using BitBlt().

    I have a CopyWindow() function that puts the BITMAPINFO structure and RGB image data on the clipboard as a DIB with the SetClipboardData(CF_DIB,...) call.

    My problem happens when I try to paste the image into MS Powerpoint (Office 2010). I've tried Paste and all the options in Paste Special but the image will not appear.

    If I open Paint and do a Paste, the image appears properly in that application. Then I can do a Copy from there and am able to Paste that into Powerpoint.

    How is Paint able to accept the DIB?
    And what is different about how Paste puts that bitmap onto the Clipboard so that Powerpoint can see it?

    Thanks.

    --Ed

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: paste a SetClipboardData(CF_DIB) image to powerpoint?

    Could you show a piece of code?
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2014
    Posts
    4

    Re: paste a SetClipboardData(CF_DIB) image to powerpoint?

    Here is the code.
    (Note that error checking is removed for brevity but I checked the return codes and they all succeed.)

    // ---------------------------------------------
    RGBTRIPLE *m_pUTDispData;
    BITMAPINFO m_bmi;
    CDC *pDC = this->GetDC();
    void *m_pDIBData;

    memset(&m_bmi, 0, sizeof(BITMAPINFO));
    m_bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
    m_bmi.bmiHeader.biWidth = DATA_WIDTH; // width of data buffer in pixels
    m_bmi.bmiHeader.biHeight = -DATA_LENGTH; // length of data buffer in pixels
    m_bmi.bmiHeader.biPlanes = 1;
    m_bmi.bmiHeader.biBitCount = 24;
    m_bmi.bmiHeader.biCompression = BI_RGB;
    m_bmi.bmiHeader.biSizeImage = 0; // can be 0 for BI_RGB bitmaps
    m_bmi.bmiHeader.biXPelsPerMeter = 0;
    m_bmi.bmiHeader.biYPelsPerMeter = 0;
    m_bmi.bmiHeader.biClrImportant = 0; // all colors are required for display
    m_bmi.bmiHeader.biClrUsed = 0;


    m_hDisplayBitmap=CreateDIBSection(pDC->m_hDC, &m_bmi, DIB_RGB_COLORS, &m_pDIBData, NULL, 0);
    m_pUTDispData = (RGBTRIPLE *)m_pDIBData;

    // walk through m_pUTDispData DATA_WIDTH x DATA_LENGTH and fill it with RGB triplets.
    // can be any color - a rainbow, doesn't matter - it successfully BitBlt()s to the screen


    // ---------------------------------------------

    // Later in the CopyWindow() routine... (note that m_pUTDispData and m_bmi are the
    // same variables from above


    HANDLE hData = GlobalAlloc(GHND | GMEM_SHARE,
    sizeof(BITMAPINFO)+m_bmi.bmiHeader.biWidth*m_bmi.bmiHeader.biHeight*3);
    LPVOID pData = (LPVOID)GlobalLock(hData);
    memcpy(pData,&m_bmi,sizeof(BITMAPINFO));
    memcpy((UCHAR *)pData+sizeof(BITMAPINFO), (LPVOID)m_pUTDispData,
    m_bmi.bmiHeader.biWidth*m_bmi.bmiHeader.biHeight*3);
    GlobalUnlock(hData);
    OpenClipboard();
    EmptyClipboard();
    SetClipboardData(CF_DIB, hData);
    CloseClipboard();

    // ---------------------------------------------

  4. #4
    Join Date
    Jan 2014
    Posts
    4

    Re: paste a SetClipboardData(CF_DIB) image to powerpoint?

    ach... too much copy and paste. the "m_bmi.bmiHeader.biHeight = -DATA_LENGTH" line should NOT have the negative sign. my misbehaving code has that number as positive.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: paste a SetClipboardData(CF_DIB) image to powerpoint?

    Quote Originally Posted by ejbaker View Post
    ach... too much copy and paste. the "m_bmi.bmiHeader.biHeight = -DATA_LENGTH" line should NOT have the negative sign. my misbehaving code has that number as positive.
    When posting code, format it first, click 'Go Adanced', paste the code and then select it and click '#' to have it displayed properly.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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