GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All gists
  • Back to GitHub
  • Sign in
  • Sign up
  • Code
  • Revisions 2
  • Stars 7
  • Forks 2
  • Terms
  • Privacy
  • Security
  • Status
  • Community
  • Docs

This view

  • Original page
  • New address

GeezerVison is showing gist.github.com — example how to put RGBA image in clipboard

Instantly share code, notes, and snippets.

@mmozeiko

mmozeiko/image2clipboard.c

Last active June 24, 2025 18:19
Show Gist options
  • Download ZIP
  • Star 7 (7) You must be signed in to star a gist
  • Fork 2 (2) You must be signed in to fork a gist
  • Clone this repository at <script src="https://gist.github.com/mmozeiko/bd79c7d256eecd1eb89441f3e0a3ad17.js"></script>
  • Save mmozeiko/bd79c7d256eecd1eb89441f3e0a3ad17 to your computer and use it in GitHub Desktop.
Clone this repository at <script src="https://gist.github.com/mmozeiko/bd79c7d256eecd1eb89441f3e0a3ad17.js"></script>
Save mmozeiko/bd79c7d256eecd1eb89441f3e0a3ad17 to your computer and use it in GitHub Desktop.
Download ZIP
example how to put RGBA image in clipboard
Raw
image2clipboard.c
#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" // get from https://raw.githubusercontent.com/nothings/stb/master/stb_image.h
#include <windows.h>
int main(int argc, char* argv[])
{
int w, h;
stbi_uc* data = stbi_load(argv[1], &w, &h, NULL, 4);
BITMAPV5HEADER header = {
.bV5Size = sizeof(header),
.bV5Width = w,
.bV5Height = h, // could be negative to vflip, but some applications do not like it
.bV5Planes = 1,
.bV5BitCount = 32,
.bV5Compression = BI_BITFIELDS,
.bV5RedMask = 0x000000ff, // update masks for whatever RGBA byte order you have
.bV5GreenMask = 0x0000ff00,
.bV5BlueMask = 0x00ff0000,
.bV5AlphaMask = 0xff000000,
.bV5CSType = LCS_WINDOWS_COLOR_SPACE, // required for alpha support
};
HGLOBAL global = GlobalAlloc(GMEM_MOVEABLE, sizeof(header) + w*h*4);
if (global) {
BYTE* buffer = GlobalLock(global);
if (buffer) {
CopyMemory(buffer, &header, sizeof(header));
// vflip the bitmap manually, for better compatibility
for (int i=0; i<h; i++) {
CopyMemory(buffer + sizeof(header) + i*w*4, data + (h-1-i)*w*4, w*4);
}
GlobalUnlock(global);
}
if (OpenClipboard(NULL)) {
EmptyClipboard();
SetClipboardData(CF_DIBV5, global);
CloseClipboard();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You are visitor 01402639 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.