Solved

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

Posted on 2016-07-19
  • C++
  • Windows 7
  • MS Development-Other
  • Programming
  • Images and Photos
  • +1
1
130 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
1 Comment
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is about my first experience with programming Arduino.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now