?
Solved

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

Posted on 2016-07-19
1
Medium Priority
?
308 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

764 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