[Gtk]利用gdk_pixbuf_copy_area對圖片添加邊框

來源:互聯網
上載者:User

 下面的代碼實現在圖片上添加橙色外邊框的效果(選中效果)
    PHOTO_WIDTH、PHOTO_HEIGHT分別代表圖片高度和寬度(包括橙色邊框)
    PHOTO_LINE_WIDTH代表橙色邊框的寬度
    pName是圖片名

    //由檔案產生除去橙色邊框的源pixbuf
    rel_pixbuf = gdk_pixbuf_new_from_file_at_scale (pName,
            PHOTO_WIDTH - 2*PHOTO_LINE_WIDTH,
            PHOTO_HEIGHT - 2*PHOTO_LINE_WIDTH,
            FALSE, NULL);

    //產生空的目標pixbuf,該pixbuf大小是最終圖片的大小(PHOTO_WIDTH,PHOTO_HEIGHT)
    file_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
            gdk_pixbuf_get_has_alpha(rel_pixbuf),
            gdk_pixbuf_get_bits_per_sample(rel_pixbuf),
            PHOTO_WIDTH, PHOTO_HEIGHT);

    //向file_pixbuf填充橙色
    gdk_pixbuf_fill(file_pixbuf, 0xFF8F02FF);//橙色,alpha非透明

    //把整個rel_pixbuf拷入從PHOTO_LINE_WIDTH,PHOTO_LINE_WIDTH開始的
    //目標緩衝區file_pixbuf
    gdk_pixbuf_copy_area(rel_pixbuf, 0, 0,
            PHOTO_WIDTH - 2*PHOTO_LINE_WIDTH,
            PHOTO_HEIGHT - 2*PHOTO_LINE_WIDTH,
            file_pixbuf, PHOTO_LINE_WIDTH, PHOTO_LINE_WIDTH);

    //至此已經完成圖片加邊框的效果,下面進行美化

    //空pixbuf,大小為1,1,用於填充透明色
    white_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
            gdk_pixbuf_get_has_alpha(rel_pixbuf),
            gdk_pixbuf_get_bits_per_sample(rel_pixbuf),
            1, 1);
    //向white_pixbuf填充透明白色
    gdk_pixbuf_fill(white_pixbuf, 0xffffff00);//white, alpha 透明

    //向file_pixbuf邊框填入一個像素寬的透明色,出圓角效果
    gdk_pixbuf_copy_area(white_pixbuf, 0, 0, 1, 1,
                         file_pixbuf, 0, 0);
    gdk_pixbuf_copy_area(white_pixbuf, 0, 0, 1, 1,
                         file_pixbuf, 0, PHOTO_HEIGHT -1);
    gdk_pixbuf_copy_area(white_pixbuf, 0, 0, 1, 1,
                         file_pixbuf, PHOTO_WIDTH - 1, 0);
    gdk_pixbuf_copy_area(white_pixbuf, 0, 0, 1, 1,
                         file_pixbuf, PHOTO_WIDTH - 1, PHOTO_HEIGHT - 1);

    gtk_image_set_from_pixbuf(GTK_IMAGE(image), file_pixbuf);

    g_object_unref (file_pixbuf);
    g_object_unref (rel_pixbuf);
    g_object_unref (white_pixbuf);

    file_pixbuf = NULL;
    rel_pixbuf = NULL;
    white_pixbuf = NULL;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.