Cairo 可以直接在 Gtk.Pixbuf 上绘图吗?

发布于 2024-12-06 08:09:06 字数 595 浏览 1 评论 0原文

我有一个很好的 C# 工作系统,可以在渲染方法中使用 Cairo 命令进行绘制。然而,有时我想绘制成像素图,而不是在屏幕需要更新时动态绘制。例如,目前我有:

public override void render(Cairo.Context g) {
  g.Save();
  g.Translate(x, y);
  g.Rotate(_rotation);
  g.Scale(_scaleFactor, _scaleFactor);
  g.Scale(1.0, ((double)_yRadius)/((double)_xRadius));
  g.LineWidth = border;
  g.Arc(x1, y2, _xRadius, 0.0, 2.0 * Math.PI); 
  g.ClosePath();
}

但如果我选择,我希望将 Cairo 命令渲染到 Gtk.Pixbuf。比如:

 g = GetContextFromPixbuf(pixbuf);
 render(g);

这可能吗?如果我不必将上下文转回 pixbuf,但 cairo 绘图将直接转到 pixbuf,那就太好了。任何有关此的提示将不胜感激!

I have a fine, working system in C# that draws with Cairo commands in a render method. However, sometimes I would like to draw into a pixmap, rather than dynamically when the screen needs to be updated. For example, currently I have:

public override void render(Cairo.Context g) {
  g.Save();
  g.Translate(x, y);
  g.Rotate(_rotation);
  g.Scale(_scaleFactor, _scaleFactor);
  g.Scale(1.0, ((double)_yRadius)/((double)_xRadius));
  g.LineWidth = border;
  g.Arc(x1, y2, _xRadius, 0.0, 2.0 * Math.PI); 
  g.ClosePath();
}

But I would like, if I choose, to render the Cairo commands to a Gtk.Pixbuf. Something like:

 g = GetContextFromPixbuf(pixbuf);
 render(g);

Is that possible? It would be great if I didn't have to turn the context back into a pixbuf, but that the cairo drawing would go directly to the pixbuf. Any hints on this would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光下的微笑 2024-12-13 08:09:06

答案实际上非常简单:渲染对象时,将它们渲染到从保存的表面创建的上下文中。然后,当您渲染窗口时,插入基于相同保存表面的上下文。

创建表面:

  surface = new Cairo.ImageSurface(Cairo.Format.Argb32, width, height);

将形状渲染到表面:

using (Cairo.Context g = new Cairo.Context(surface)) {
  shape.render(g); // Cairo drawing commands
}

渲染窗口:

  g.Save();
  g.SetSourceSurface(surface, 0, 0);
  g.Paint();
  g.Restore();
      ... // other Cairo drawing commands

就是这样!

The answer is actually quite easy: when you render the objects, render them to a context created from a saved surface. Then when you render the window, insert a context based on the same saved surface.

Create a surface:

  surface = new Cairo.ImageSurface(Cairo.Format.Argb32, width, height);

Render a shape to the surface:

using (Cairo.Context g = new Cairo.Context(surface)) {
  shape.render(g); // Cairo drawing commands
}

Render the window:

  g.Save();
  g.SetSourceSurface(surface, 0, 0);
  g.Paint();
  g.Restore();
      ... // other Cairo drawing commands

That's it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文