Thread: Saving clipboard image to jpg

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    Saving clipboard image to jpg

    Hi Everyone.
    I have an image in my clipboard - from alt-printscreen and I want to save it to a jpg - or better I want to atleast be able to access the data as jpg formatted data so i can then do something with it.

    I have come across openClipboard() which seems perfect, however I am using codeblocks and gcc compiler. I have imported windows.h but cant find a good example of how I would use the data in the clipboard and save it to a file (or use the data).

    Can anyone give me an example of using clipboard data please (in windows)

    thanks

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    If you open the basic microsoft program 'Paint', you should be able to immediately paste the image into it as a bmp image. Then just use any other image manipulation software to convert.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you google win32 clipboard api, the first result explains (almost) everything. you probably should have tried this first.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your question is a little bit vague. Why must it be jpg? What exactly do you want to do with it? Do you plan on doing something with it programatically, like your program will actually perform some transformations on the screenshot (blur, stretch, crop, etc)? Or do you want to just save it to a file to use in another program like GIMP or whatever (those usually have a "paste from clipboard" option).

    I Googled "openclipboard example" and got these results: https://www.google.com/search?q=openclipboard+example. The second link takes you to a MS site with lots of information and examples about using the clipboard: Using the Clipboard (Windows). It also discusses clipboard data types which, as far as I can tell, doesn't include JPEG. If you do alt-printscreen, presumably it will store the result in a CF_BMP or similar format (admittedly, I'm having trouble finding what format is used by default for alt-printscreen). You could find this out by emptying the clipboard, hitting alt-printscreen, then cycling through the clipboard formats looking for which one has data.

    Once you know which data type the screenshot is stored in, get a handle to that data with GetClipboard data. A HANDLE is basically, a void * to the clipboard data in the specified format. That should be a pointer to a block of memory containing all the image data. If you fopen in binary mode, fwrite all the data in the handle, then fclose the file you should have a valid BMP file. You would have to look at the BMP header info to find out the color depth (bytes per pixel) and total image size (number of pixels) to know exactly how much data to write to the file.

    If you really want JPEG, you will have to find a way to convert it -- there are third party libraries for this, but you can also write your own, though that is more work and more error-prone. Note, Wikipedia has a great article on the BMP format, which should help you understand what data you're getting from the clipboard. There are good articles on JPEG format as well, though it's more complex than BMP. Consider other formats like GIF and PNG as well if you don't really need JPEG.

    In case you just want to have the functionality and don't care to actually code this yourself, a google search suggested there are some third party utilities that allow "paste clipboard to jpg".

    Note, I'm not a Windows programmer, so I could very well be missing something (or several things).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Win32 specific question moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I would look at ImageMagick. There is an example on the website which shows how to read images and save them using the C interface:
    ImageMagick: MagickWand, C API for ImageMagick

    Also in the source there is a inline function called ReadCLIPBOARDImage which seems to do exactly what you want. It reads from the Windows clipboard and saves an Image* which ImageMagick can use. Adapting this into the above example lets you take the clipboard as input and produce a thumbnail JPEG image as output (or do whatever you want using the ImageMagick API):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <wand/MagickWand.h>
    #include <wand/magick-image.h>
    #include <magick/image.h>
    #include <magick/methods.h>
    
    #include <windows.h>
    #include <wingdi.h>
    #include "magick/quantum-private.h"
    #include "magick/exception-private.h"
    #include "ReadCLIPBOARDImage.inline.h"
    
    int main()
    {
        // Acquire Image from Windows Clipboard.
        ImageInfo *ii = AcquireImageInfo();
        ExceptionInfo *ie = AcquireExceptionInfo();
        Image *img = ReadCLIPBOARDImage(ii, ie);
    
        // Create MagickWand from image.
        MagickWandGenesis();
        MagickWand *magick_wand;
        magick_wand = NewMagickWandFromImage(img);
    
        // Scale the image down to a thumbnail.
        MagickResetIterator(magick_wand);
        while (MagickNextImage(magick_wand) != MagickFalse) {
            MagickResizeImage(magick_wand, 106, 80, LanczosFilter, 1.0);
        }
    
        // Write the image to a file.
        MagickStatusType status;
        status = MagickWriteImages(magick_wand, "clipboard_thumbnail.jpg", MagickTrue);
        if (status == MagickFalse) {
            fprintf(stderr, "Could not write file.\n");
        }
    
        // Clean up.
        DestroyImageInfo(ii);
        DestroyExceptionInfo(ie);
        DestroyMagickWand(magick_wand);
        MagickWandTerminus();
        return 0;
    }
    The file READClipboardImage.inline.h is a copy of the function of the same name from the source archive. It is a static inline function. The "-private.h" header files are also from the source archive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need for an image saving code (e.g. BMP)
    By apawamajawa in forum Linux Programming
    Replies: 2
    Last Post: 08-14-2011, 12:55 AM
  2. Replies: 4
    Last Post: 03-13-2010, 05:10 AM
  3. Replies: 13
    Last Post: 11-20-2009, 04:43 PM
  4. saving bmp image files
    By aniramg69 in forum C Programming
    Replies: 10
    Last Post: 12-30-2008, 10:08 AM
  5. Problems with Image Magick [Unable to Quantisize Image]
    By Maragato in forum C Programming
    Replies: 1
    Last Post: 09-18-2006, 10:41 PM