Wie kann ich Gdiplus::Image in Zwischenablage kopieren?

Hallo zusammen!

Ich versuche ein Objekt vom Typ Gdiplus::Image in Zwischenablage kopieren, damit später in einer Anwendung (z.B. MS-Word, Paint, Photoshop etc.) mit Hilfe Ctrl-V einfügen möglich wäre.

Dafür benutze ich follgende Routine
1
2
3
4
5
6
7
8
9
10
11
12
13
if(!OpenClipboard(NULL))
        return 0;
    EmptyClipboard();
    HBITMAP hbitmap1;
    Bitmap* bitmapImg1 = new Bitmap(L"C:\\Users\\User1\\Desktop\\1.jpg");
    UINT w = bitmapImg1->GetWidth();
    UINT h = bitmapImg1->GetHeight();

    Color bgColor = Color::White;
    bitmapImg1->GetHBITMAP(bgColor, &hbitmap1);
    SetClipboardData(CF_BITMAP, hbitmap1);
   
    CloseClipboard(); 

Alles läuft ohne Fehlern.
Aber beim Enfügen-Aktion wird in andere Programm nichts eingefügt.

Kann mir jemand sagen, was in meinem Qulellkode stimmt nicht.
Danke im voraus
geokas
I suggest using english.

Look at this:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/816a35f6-9530-442b-9647-e856602cc0e2

you can't just set the handle to the bitmap. you need to copy the bits themself
Thanks for your answer

Look at this:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/816a35f6-9530-442b-9647-e856602cc0e2

you can't just set the handle to the bitmap. you need to copy the bits themself


I have tried to do so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
if(!OpenClipboard(NULL))
        return 0;
    EmptyClipboard();
    
    Bitmap* imgSrc = new Bitmap(L"C:\\Users\\user1\\Desktop\\1.jpg");
    UINT w = imgSrc->GetWidth();
    UINT h = imgSrc->GetHeight();

    Graphics* g = Graphics::FromImage(imgSrc);
    HDC hdcSrc = g->GetHDC();
    HBITMAP hbitmapSrc;
    Color bgColor = Color::White;
    imgSrc->GetHBITMAP(bgColor, &hbitmapSrc);
    SelectObject(hdcSrc, hbitmapSrc);

    BitBlt(hdcDst, 0, 0, w, h, hdcSrc, 0, 0, SRCCOPY);
    
    HBRUSH brush = CreateSolidBrush( RGB( 255, 0, 0 ));
    SelectObject( hdcDst, brush );
    Rectangle( hdcDst, 100, 100, 300, 200 );
    
    HANDLE resultH = SetClipboardData(CF_BITMAP, hbitmapDst);
    Bitmap* resultB = Bitmap::FromHBITMAP((HBITMAP)resultH, NULL);
    UINT wD = resultB->GetWidth();
    UINT hD = resultB->GetHeight();
    
    CloseClipboard();
    g->ReleaseHDC(hdcSrc);

    if(resultH == NULL)
        return 0;
    return 1;


Now is better, because now isn't nothing. Now is black rectangle, which has same size as my image.

I don't understand where is now my mistake?
Last edited on
Here's another example:

http://us.generation-nt.com/answer/gdi-plus-image-clipboard-help-27536922.html

Especially #4 looks promising

An important thing is that you detach the hbitmap from the object (like imgSrc), since it must not be destroyed.

(Maybe you should consider to use a higher gui lib like QT or wxWidgets)
Thank's for your help

I make with help of your link http://us.generation-nt.com/answer/gdi-plus-image-clipboard-help-27536922.html new example.
It works
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
if(!OpenClipboard(NULL))
        return 0;
    EmptyClipboard();
    
    Bitmap* imgSrc = new Bitmap(L"C:\\Users\\user1\\Desktop\\1.jpg");
    UINT w = imgSrc->GetWidth();
    UINT h = imgSrc->GetHeight();

    
    HBITMAP hbitmapSrc;
    Color bgColor = Color::White;
    imgSrc->GetHBITMAP(bgColor, &hbitmapSrc);
    

    
    DIBSECTION ds;
    GetObject( hbitmapSrc, sizeof(ds), &ds );
    ds.dsBmih.biCompression = BI_RGB; // change compression from BI_BITFIELDS to BI_RGB
    // Convert the DIB to a device dependent bitmap(i.e., DDB)
    HDC hDC = GetDC(NULL);
    HBITMAP hbitmapDst = CreateDIBitmap( hDC, &ds.dsBmih, CBM_INIT,
    ds.dsBm.bmBits, (BITMAPINFO*)&ds.dsBmih, DIB_RGB_COLORS );
    ReleaseDC(NULL, hDC);


    
    HANDLE resultH = SetClipboardData(CF_BITMAP, hbitmapDst);
    Bitmap* resultB = Bitmap::FromHBITMAP((HBITMAP)resultH, NULL);
    UINT wD = resultB->GetWidth();
    UINT hD = resultB->GetHeight();
    
    CloseClipboard();
    

    if(resultH == NULL)
        return 0;
    return 1;


Topic archived. No new replies allowed.