将位图转换为Pixbuf

本文关键字:Pixbuf 转换 位图 | 更新日期: 2025-05-08 12:31:43

我计划在gtk图像中显示我生成的位图。

但它只支持Pixbuf。如何将位图转换为Pixbuf。

我可以使用TypeConverter吗?

将位图转换为Pixbuf

以不同的方式完成。除了转换,我已经重新加载了图像。

protected void OnBtnGenClicked (object sender, EventArgs e)
{
    Bitmap bmp = new Bitmap(300, 300);
    using (Graphics g = Graphics.FromImage(bmp))
    {
        Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Point);
        g.Clear(Color.White);
        g.DrawString(txtMessage.Text, font, Brushes.Black, 0, 0);
    }
    bmp.Save("d:/image.png", ImageFormat.Png);
    var buffer = System.IO.File.ReadAllBytes ("d:/image.png");
    var pixbuf = new Gdk.Pixbuf (buffer);
    image1.Pixbuf = pixbuf;
}