使用imagemagick API截屏 [英] Taking a screenshot using imagemagick API
问题描述
我正在使用GCC 4.7.2,C89和ImageMagick-6.8.0-4.
I'm using GCC 4.7.2, C89, and ImageMagick-6.8.0-4.
我正在尝试使用ImageMagick API进行屏幕截图.我已经下载,编译并安装了标头和库.
I am trying to take a screen shot using the ImageMagick API. I have downloaded, compiled, and installed the headers and libraries.
但是,我在文档中看不到API调用来进行截屏.我将链接并包括标头,以便可以从C程序调用API.我只想要执行此操作所要使用的函数的名称.
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.
有人能指出我正确的方向吗?ImageMagick可以截屏吗?
Can anyone point me in the right direction? Can ImageMagick take screenshots?
推荐答案
使用'x:'特殊文件格式
如果您不想要刺激性的屏幕抓取;您可以通过在 MagickReadImage
中将" x:root "作为源参数传递来调用ImageMagick的import命令. x:格式通过传递 pid
或窗口标签.其他修饰符可以捕获区域&分页.
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;
}
使用其他魔术"库
在 wand
库之外, magick/xwindow.h
允许您使用 XImportImage
, XImportInfo 导入图像代码>&
XGetImportInfo
方法.您可以在ImageMagick的源文件中检查这些方法的工作方式 wand/import.c
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.
MagickWand还包括PixelWand;可以迭代内存中的图像指针.要做更多的工作,但更大的灵活性.
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;
}
有用的提示
确保ImageMagick能够连接到您的显示系统.尝试使用几个 import
CLI命令来验证本地安装是否正常.这个问题是一个很好的例子.
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
这篇关于使用imagemagick API截屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!