3

私はGTKにやや慣れておらず、カイロには非常に慣れていません。背景として PNG を使用し、文字と数字を含む複数の PNG ファイルを背景 PNG に合成する必要があるアプリケーションを作成することを任されました。など。役に立つと思われるヒント、チュートリアル、コード サンプルはありますか? GTK の場合と同様に、Cairo のドキュメントは、図形を描くよりも複雑なことをしようとする初心者にとっては、やや不足しているように見えます。

4

2 に答える 2

9

この簡単な例を見てください。カイロのみを使用します。

#include <cairo.h>

int main()
{
    //Load a few images from files
    cairo_surface_t *surf1 = cairo_image_surface_create_from_png("a.png");
    cairo_surface_t *surf2 = cairo_image_surface_create_from_png("b.png");

    //Create the background image
    cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 100, 100);

    //Create the cairo context
    cairo_t *cr = cairo_create(img);

    //Initialize the image to black transparent
    cairo_set_source_rgba(cr, 0,0,0, 1);
    cairo_paint(cr);

    //Paint one image
    cairo_set_source_surface(cr, surf1, 0, 0);
    cairo_paint(cr);

    //Paint the other image
    cairo_set_source_surface(cr, surf2, 50, 50);
    cairo_paint(cr);

    //Destroy the cairo context and/or flush the destination image
    cairo_surface_flush(img);
    cairo_destroy(cr);

    //And write the results into a new file
    cairo_surface_write_to_png(img, "result.png");

    //Be tidy and collect your trash
    cairo_surface_destroy(img);
    cairo_surface_destroy(surf1);
    cairo_surface_destroy(surf2);

    return 0;

}
于 2012-06-13T17:03:07.990 に答える
4

ロドリゴありがとう、これはとても良い例です!

すべてのC#プログラマーにとって、これはC#に変換されたまったく同じ例です。SetSourceRGBA()に小さな変更を1つだけ加えました。これにより、黒ではなく透明な背景画像を使用できるようになります。

Using Cairo;

    //Load a few images from files
    ImageSurface surf1 = new ImageSurface("a.png");
    ImageSurface surf2 = new ImageSurface("b.png");

    //Create the background image
    ImageSurface img = new ImageSurface(Format.Argb32, 200, 200);

    //Create the cairo context
    Context cr = new Context(img);

    //Initialize the image to black transparent
    // cr.SetSourceRGBA(255,255,255,1); // This will create the background image with white background
    // cr.SetSourceRGBA(0,0,0,1);       // This will create the background image with black background
    cr.SetSourceRGBA(0,0,0,0);          // This creates the background image with transparent background
    cr.Paint ();


    //Paint one image
    cr.SetSourceSurface(surf1,0,0);
    cr.Paint();

    //Paint the other image
    cr.SetSourceSurface(surf2, 25, 50);
    cr.Paint();

    //Destroy the cairo context and/or flush the destination image
    img.Flush();

    //And write the results into a new file
    img.WriteToPng("result.png");

    //Be tidy and collect your trash
    img.Dispose();
    img.Destroy();
    surf1.Destroy ();
    surf2.Destroy();
于 2012-08-30T21:00:30.313 に答える