As explained in the MSDN article
Using the Clipboard, you have to copy the data to allocated global memory and call
SetClipboardData
passing the clipboard format and the global memory handle.
Because there is no standard clipboard format for PNG images, you have to register a clipboard format using the
RegisterClipboardFormat function[
^]. Commonly used are "PNG" and "image/png" (the MIME type).
When having the image as file, load it into the allocated memory (untested example code without error checks):
struct _stat st;
stat(fileName, &st);
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, st.st_size);
LPVOID pGlobal = GlobalLock(hGlobal);
FILE *f = fopen(fileName, "rb");
fread(pGlobal, 1, st.st_size, f);
fclose(f);
GlobalUnlock(hGlobal);
UINT cf = RegisterClipboardFormat("PNG");
OpenClipboard(hWnd);
EmptyClipboard();
SetClipboardData(cf, hGlobal);
CloseClipboard();
When using MFC, you can also use the
CImage
class to create the PNG image as
IStream
and put that on the clipboard using the
COleDataSource
class.