Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to copy an image file into clipboard C/C++?

Posted on 2016-07-19
1
Medium Priority
?
354 Views
Last Modified: 2016-07-21
I need a C++/C code example that demonstrates how to copy a 24-bit bmp picture file on the computer/disk into the clipboard.
I am working with a Visual C++ Win32 console application on Windows 7.
OpenClipboard();
EmptyClipboard();
?
hMem = GlobalAlloc(GMEM_MOVEABLE, ?);
memcpy(GlobalLock(hMem), ?, ?);
GlobalUnlock(hMem);
SetClipboardData(CF_DIB, hMem);
CloseClipboard();

Open in new window

Thanks to anyone that helps.
0
Comment
Question by:Member_2_7966362
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 41723559
the code is like the following for a 24-bit bitmapfile (not compressed).

#include <windows.h>
#include <sys/stat.h>
#include <fstream>
#include <errno.h>

int BitmapFileToClipBoard(HWND hwnd, const char * szBitmapFilePath)
{
    struct stat fs = { 0 };
    if (!stat(szBitmapFilePath, &fs) == 0)
        return errno;   // error file does not exist
    std::ifstream ifs(szBitmapFilePath, std::ios::binary | std:ios::in);
    unsigned char * buf = new unsigned char[fs.st_size];
    if (!ifs.read((char*)buf, fs.st_size)
    {
        int err = errno;
        ifs.close();
        return err;
    }
    ifs.close();
    BITMAPINFO * pDib = (BITMAPINFO *)buf;
 
    HDC hdc = CreateCompatibleDC(NULL);
    HBITMAP hBitmap = CreateDIBitmap(hdc, 
         &(pDib->bmiHeader),
         CBM_INIT,
         pDib->bmiColors,
         pDib,
         DIB_RGB_COLORS);
     if (OpenClipBoard(hwnd))
     {
          SetClipboardData(CF_BITMAP, hBitmap); 
          CloseClipboard();
          DeleteDC(hdc);
          return 0;
    }
    return -1;
}

Open in new window


the code is not tested and extracted from much bigger code such that i may have forgotten some necessary things. but you may got the idea from the above and use it as a starter.

Sara
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Six Sigma Control Plans

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question