Copy bitmap to clipboard

Source: Internet
Author: User

 

 

Copying a bitmap to the clipboard is quite simple. Remember that if the bitmap needs a color palette, you should also copy the palette.

 

Function 1: copy the device-related bitmap to the clipboard.

 

The CopyBitmapToClipboard () function copies a DDB bitmap to the clipboard. If a color palette is provided, it copies the color palette at the same time.

Note thatDetach (). This is important, because at this time the GDI object owner has been transferred to the clipboard.

 

// CopyBitmapToClipboard-Copies a device-dependent bitmap to clipboard

// PWnd-Pointer to window that opens the clipboard

// Bitmap-The device-dependent bitmap

// PPal-Pointer to logical palette-Can be NULL

// NOTE-GDI objects are detached from bitmap & pPal

//As the clipboard owns them after the copy

Void CopyBitmapToClipboard (const CWnd * pWnd, CBitmap & bitmap, CPalette * pPal)

{

: OpenClipboard (pWnd-> GetSafeHwnd ());

: EmptyClipboard ();

If (pPal)

: SetClipboardData (CF_PALETTE, pPal-> GetSafeHandle ());

: SetClipboardData (CF_BITMAP, bitmap. GetSafeHandle ());

: CloseClipboard ();

Bitmap. Detach ();

If (pPal)

PPal-> Detach ();

}

Function 2: copy the device-independent bitmap to the clipboard.

 

The CopyDIBToClipboard () function is very similar to the CopyBitmapToClipboard () function. The memory handle contains BITMAPINFO and bitmap bit information, which is allocated through GlobalAlloc.

 

// CopyDIBToClipboard-Copies a device-dependent bitmap to clipboard

// PWnd-Pointer to window that opens the clipboard

// HDIB-Memory handle that contains BITMAPINFO & bitmap bits

// PPal-Pointer to logical palette-Can be NULL

// NOTE-GDI objects are detached from bitmap & pPal

//As the clipboard owns them after the copy

Void CopyDIBToClipboard (const CWnd * pWnd, HGLOBAL hDIB, CPalette * pPal)

{

: OpenClipboard (pWnd-> GetSafeHwnd ());

: EmptyClipboard ();

If (pPal)

: SetClipboardData (CF_PALETTE, pPal-> GetSafeHandle ());

: SetClipboardData (CF_DIB, hDIB );

: CloseClipboard ();

Bitmap. Detach ();

If (pPal)

PPal-> Detach ();

}

 

Function 3: copy one imageWindows Image to clipboard

 

Void CopyWndToClipboard (CWnd * pWnd)

{

CBitmapBitmap;

CClientDCDc (pWnd );

CDC&

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.