'x:'特殊ファイル形式の使用
スリルのないスクリーングラブが必要な場合。のソース引数として「 x:root 」を渡すことで、ImageMagickのインポートコマンドを呼び出すことができますMagickReadImage
。x:形式では、pid
またはウィンドウラベルを渡すことにより、完全なスクリーンショットまたは単一のウィンドウを使用できます。追加の修飾子は、リージョンとページングをキャプチャできます。
#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;
}
追加の「magick」ライブラリを使用する
wand
ライブラリの外部では、&メソッドmagick/xwindow.h
を使用して画像をインポートできます。これらのメソッドがImageMagickのソースファイルwand/import.cでどのように機能するかを調べることができます。XImportImage
XImportInfo
XGetImportInfo
XおよびPixelIteratorを直接操作する
MagickWandにはPixelWandも含まれています。これは、メモリ内のイメージポインタを反復処理できます。もう少し作業が必要ですが、柔軟性が高くなります。
#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がディスプレイシステムに接続できることを確認してください。いくつかのCLIコマンドを試して、import
ローカルインストールが機能していることを確認してください。この質問は良い例です。
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