How to draw an outline of a region in graphics in .net core?

Mariam_S 0 Reputation points
2023-04-05T13:27:00.7366667+00:00

How to draw an outline around a Region that made by union and intersection between shapes like Rectangle and Ellipse. Note I wanna draw only the outline not fill the entire region with color.

public class ShapeVM
{

    
    public List<Point> Points { get; set; }
}
public string DrawShapes(List<ShapeVM> shapes) { 

var origBmp = new Bitmap(800, 700); Graphics g = Graphics.FromImage(origBmp);

g.Clear(Color.Transparent);    
var firstshape = shapes.FirstOrDefault();
    Region graphicsRegion = new Region(new Rectangle(firstshape.Points[0], new Size(firstshape.Points[1])));
    shapes.Remove(firstshape);

    foreach (var item in shapes)
    {
        if (item.ShapeType == ShapeTypeEnum.Rectangle)
        {
            Region rectangle = new Region(new Rectangle(item.Points[0], new Size(item.Points[1])));
            graphicsRegion.Union(rectangle);
            rectangle.Dispose();
        }
        else
        {
            Rectangle rectangle = new Rectangle(item.Points[0], new Size(item.Points[1]));
            GraphicsPath ellipse = new GraphicsPath();
            ellipse.AddEllipse(rectangle);
            graphicsRegion.Exclude(ellipse);
            ellipse.Dispose();
        }
    }

    g.FillRegion(regionFillBrush, graphicsRegion);
    graphicsRegion.Dispose();

    MemoryStream ms = new MemoryStream();
    origBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    byte[] byteImage = ms.ToArray();
    var imageString = $"data:image/png;base64,{Convert.ToBase64String(byteImage)}";

    g.Dispose();
    origBmp.Dispose();

    return imageString; //"/Files" + fileName;
}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,438 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,227 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,641 Reputation points
    2023-04-05T14:57:16.39+00:00

    Typically on a website this would done with JavaScript and a canvas. They’re a lot of graphics libraries to start with. Just google for one that meets you requirements. you will to know some geometry. You draw a dotted line on splines that represent the curve of the outline. You may also need an edge detection code. If the outline is just a rectangle, then just draw a rectangle with dotted border.