问题
I'm using GCC 4.7.2, C89, and ImageMagick-6.8.0-4.
I am trying to take a screen shot using the ImageMagick API. I have downloaded, compiled, and installed the headers and libraries.
However, I can't see in the documentation the API calls to do the screen shot. I will be linking and including the headers so that I can call the API from my C program. I just want the name of the function I will have to use to do this.
Can anyone point me in the right direction? Can ImageMagick take screenshots?
回答1:
Using 'x:' Special File Format
If you want a no-thrill screen grab; you can invoke ImageMagick's import command by passing "x:root" as the source argument in MagickReadImage
. The x: format allows for a full screenshot or a single window by passing a pid
or window label. Additional modifiers can capture regions & paging.
#include <wand/MagickWand.h>
int main(int argc, char **argv)
{
MagickWandGenesis();
MagickWand *wand = NULL;
wand = NewMagickWand();
MagickReadImage(wand,"x:root"); // <-- Invoke ImportImageCommand
MagickWriteImage(wand,"screen_shot.png");
if(wand)wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}
Use Additional 'magick' Libraries
Outside of wand
libraries, magick/xwindow.h
allows you to import images by use of XImportImage
, XImportInfo
& XGetImportInfo
methods. You can examine how these methods work in ImageMagick's source files wand/import.c.
Working Directly with X and Pixel Iterator
MagickWand also includes PixelWand; which, can iterate over an image-pointer in memory. A little more work, but greater flexibility.
#include <stdio.h>
#include <wand/MagickWand.h>
#include <X11/Xlib.h>
int main(int argc, char **argv) {
int x,y;
unsigned long pixel;
char hex[128];
// X11 items
Display *display = XOpenDisplay(NULL);
Window root = DefaultRootWindow(display);
XWindowAttributes attr;
XGetWindowAttributes(display, root, &attr);
// MagickWand items
MagickWand *wand = NULL;
PixelWand *pwand = NULL;
PixelIterator *pitr = NULL;
PixelWand **wand_pixels = NULL;
// Set-up Wand
MagickWandGenesis();
pwand = NewPixelWand();
PixelSetColor(pwand,"white"); // Set default;
wand = NewMagickWand();
MagickNewImage(wand,attr.width,attr.height,pwand);
pitr = NewPixelIterator(wand);
// Get image from display server
XImage *image = XGetImage(display,root, 0,0 ,
attr.width, attr.height,
XAllPlanes(), ZPixmap);
unsigned long nwands; // May also be size_t
for (y=0; y < image->height; y++) {
wand_pixels=PixelGetNextIteratorRow(pitr,&nwands);
for ( x=0; x < image->width; x++) {
pixel = XGetPixel(image,x,y);
sprintf(hex, "#%02x%02x%02x",
pixel>>16, // Red
(pixel&0x00ff00)>>8, // Green
pixel&0x0000ff // Blue
);
PixelSetColor(wand_pixels[x],hex);
}
(void) PixelSyncIterator(pitr);
}
// Write to disk
MagickWriteImages(wand,"screen_test.png");
// Clean-Up
XDestroyImage(image);
pitr=DestroyPixelIterator(pitr);
wand=DestroyMagickWand(wand);
MagickWandTerminus();
XCloseDisplay(display);
return 0;
}
Helpful Hints
Ensure that ImageMagick is able to connect to your display system. Try a few import
CLI commands to verify your local install is working. This question is a good example.
import -display localhost:0.0 -window root test_out.png
import -display ::0 -window root test_out.png
import -display :0.0 -window root test_out.png
import -display :0 -window root test_out.png
回答2:
cli program import
from imagemagick can take screenshots by running like this:
import -window root screenshot.png
So input image can be specified as file or as -window root
回答3:
- type: import fileName.fileType (Example: import screen.png)
- A cross cursor will appear: Click and drag it over the part of the screen you wish to capture (Be sure the terminal window does not cover what you wish to capture)
来源:https://stackoverflow.com/questions/13219710/taking-a-screenshot-using-imagemagick-api