winapi - How to copy a picture from disk into the clipboard with win32? -


it's easy copy text clipboard win32 api, want copy picture disk (for example, d:\1.jpg) clipborad.

i search many webpages , can't find useful. please teach me how it.

and no mfc.

you can use gdi+ load image, hbitmap, , set clipboard data. gdi+ unicode only, if using old ansi functions have convert filename wide char. example in c++:

bool copyimage(const wchar_t* filename) {     bool result = false;     gdiplus::bitmap *gdibmp = gdiplus::bitmap::fromfile(filename);     if (gdibmp)     {         hbitmap hbitmap;         gdibmp->gethbitmap(0, &hbitmap);         if (openclipboard(null))         {             emptyclipboard();             dibsection ds;             if (getobject(hbitmap, sizeof(dibsection), &ds))             {                 hdc hdc = getdc(hwnd_desktop);                 //create compatible bitmap (get ddb dib)                 hbitmap hbitmap_ddb = createdibitmap(hdc, &ds.dsbmih, cbm_init,                     ds.dsbm.bmbits, (bitmapinfo*)&ds.dsbmih, dib_rgb_colors);                 releasedc(hwnd_desktop, hdc);                 setclipboarddata(cf_bitmap, hbitmap_ddb);                 deleteobject(hbitmap_ddb);                 result = true;             }             closeclipboard();         }          //cleanup:         deleteobject(hbitmap);           delete gdibmp;                   }     return result; } 

note microsoft recommends using cf_dib set bitmap clipboard data, doesn't work gdi+. example uses cf_bitmap instead.

gdi+ uses standard gdiplus.lib library. needs initialized follows:

#include <windows.h> #include <gdiplus.h>  #pragma comment(lib, "gdiplus")//visual studio specific  bool copyimage(const wchar_t* filename);  int main() {     //initialize gdiplus once:     gdiplus::gdiplusstartupinput gdiplusstartupinput;     ulong_ptr gdiplustoken;     gdiplus::gdiplusstartup(&gdiplustoken, &gdiplusstartupinput, null);      copyimage(l"d:\\1.jpg");      gdiplus::gdiplusshutdown(gdiplustoken); } 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -