簡體   English   中英

C#將System.Drawing.Rectangle轉換為System.Drawing.Bitmap上的Ellipse

[英]C# System.Drawing.Rectangle into Ellipse on System.Drawing.Bitmap

我有一個面部識別庫,可以給我一個矩形陣列。 現在,我正在使用這種方式繪制矩形。

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);

看起來會更像:

在此處輸入圖片說明

橢圓的輪廓接觸矩形角的地方。

基於我上面使用的方法,繪制矩形很容易,但是對於橢圓,則需要我知道橢圓中的所有點。 只是想知道是否有什么可以使我的生活更輕松。

不要嘗試直接繪制到位圖,可以創建一個更高級別的對象,稱為Graphics ,它可以為您提供各種出色的繪圖工具。 它也將比逐像素繪制快得多。

您可以通過調用Graphics.FromImage並傳入Bitmap來為給定的Bitmap創建一個Graphics 您必須記住,盡管調用了Dispose on the Graphics,否則會泄漏資源。

一旦擁有位圖的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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM