jingwood / d2dlib

A .NET library for hardware-accelerated, high performance, immediate mode rendering via Direct2D.
MIT License
245 stars 43 forks source link

Drawing a bitmap in a circle? #43

Open BergChristian opened 3 years ago

BergChristian commented 3 years ago

Hi!

Great lib. Just starting to convert my controls to use this. Love it!

I am trying to draw a bitmap inside a circle. Would that be possible?

Thanks Christian

jingwood commented 3 years ago

Thanks! Hope this lib helps you. Do you mean draw a masked bitmap? I think it is easy by making some improvements, I will take a look.

BergChristian commented 3 years ago

Hi, Yes

In GDI+ I can create a brush and then fill a circle with that brush to paint an image inside the circle (or really any path). Looking for something similar here.

Right now it is only a circle which I need :).

jingwood commented 3 years ago

Now some APIs to support this have been added.

Direct2D uses layers to make clipping mask, for an ellipse mask, you can use the following code:

protected override void OnRender(D2DGraphics g)
{
  // create an ellipse geometry, which is the clipping target region
  using (var path = this.Device.CreateEllipseGeometry(ellipseOrigin, ellipseSize))
  {
    // push a layer to apply the clip mask
    using (var layer = g.PushLayer(path))
    {
      // draw masked content, ---- start from here -----
      g.FillRectangle(ClientRectangle, new D2DColor(.7f, .7f, .7f));

      g.DrawText("Text drawed via Direct2D API (d2dlib)", D2DColor.Blue, "Arial", 24, 140, 180);
      // masked content, ---- end at here ----

      // finally pop the layer to exit the mask rendering
      g.PopLayer();
    }
  }
}

Full example source code

There is the result: image

BergChristian commented 3 years ago

Thanks! That is really awesome! I guess it can be used for other geometrical As well such as a rounded rectangle?

anyway. Love this lib and when and if I can contribute I will do my best!

jingwood commented 3 years ago

Glad to hear that!

Yes, any path geometries are supported. You can also combine multiple paths by pushing nested layers.

Any contributions are welcome, feel free to share it!