Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following a snippet found on the forum, I have managed to convert a bitmap image to a PNG file. However, I need to copy this image to the clipboard.

See snippet below:

C++
INT convertPNG(HBITMAP img)
{
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	{ // Diese Klammer ist erforderlich weil der Destruktor von bmp vor GdiplusShutdown
	  // aufgerufen werden muss
		Bitmap *image = new Bitmap(img, NULL);

		// Save the image.
		CLSID pngClsid;
		GetEncoderClsid(L"image/png", &pngClsid);
		image->Save(L"mypngImg.png", &pngClsid, NULL);
	}

	GdiplusShutdown(gdiplusToken);

	return 0;
}


What I have tried:

I really do not know where to begin, but I suppose that I can do something with the image pointer without writing the file to the disk.

I can open the clipboard but that is the extent of my knowledge at this point.
Posted
Updated 26-Sep-17 1:53am
v2
Comments
Simon Ferry 26-Sep-17 8:55am    
wow, interesting result

I would start with the documentation: Clipboard at MSDN[^]. You probably have to use a private clipboard format, for your data.
 
Share this answer
 
I have looked at the clipboard documentation which shows an example of pasting text to the clipboard here: Using the Clipboard (Windows)[^]

That is all good but how do I accomplish the same with the image as described above?
 
Share this answer
 
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):
C++
// Get the file size
struct _stat st;
stat(fileName, &st);
// Allocate buffer and read data
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);
// Register clipboard format
UINT cf = RegisterClipboardFormat("PNG");
// Put it on the clipboard.
// The global memory is owned by the clipboard and will be freed when
//  new data is put on the clipboard.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900