【问题标题】:C# System.Drawing.Rectangle into Ellipse on System.Drawing.BitmapC# System.Drawing.Rectangle 到 System.Drawing.Bitmap 上的 Ellipse
【发布时间】:2016-04-10 01:08:00
【问题描述】:

我有一个正在工作的面部识别库,它为我提供了一个矩形数组。我现在就是用这种方式画矩形。

foreach (Rectangle box in boxes)
{
     for (int x = box.X; x <= box.X + box.Width; x++)
     {
          for (int y = box.Y; y <= box.Y + box.Height; y++)
          {
               outputbmp.SetPixel(x, y, Color.FromKnownColor(KnownColor.Red));
          }
     }
}

我正在寻找简单的东西:

Ellipse ellipse = new Ellipse(box); //cast rect to ellipse
outputbmp.DrawEllipse(ellipse);

看起来更像:

椭圆的轮廓与矩形角接触的位置。

根据我上面使用的方法,绘制一个矩形很容易,但对于椭圆,它需要我知道椭圆中的所有点。只是想知道是否有什么可以让我的生活更轻松。

【问题讨论】:

    标签: c# bitmap face-detection


    【解决方案1】:

    不要试图直接在位图上绘制,你可以创建一个更高级别的对象,称为Graphics,它为你提供了各种精彩的绘图工具。它也将比逐像素绘制要快得多。

    您可以通过调用Graphics.FromImage 并传入位图来为给定的Bitmap 创建Graphics。不过一定要记得在 Graphics 上调用 Dispose,否则会泄漏资源。

    一旦您的位图有一个 Graphics 实例,您就可以调用 DrawEllipse 并完全按照您的预期传递边界。

    来自MSDN

    private void DrawEllipseInt(Graphics g)
    {
        // Create pen.
        Pen blackPen = new Pen(Color.Black, 3);
    
        // Create location and size of ellipse.
        int x = 0;
        int y = 0;
        int width = 200;
        int height = 100;
    
        // Draw ellipse to screen.
        g.DrawEllipse(blackPen, x, y, width, height);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2011-08-07
      相关资源
      最近更新 更多