I am creating a winforms application and need to be able to load an image and add 2 intersecting rectangles to the image. I then want to only highlight the intersection between the two. Are there any libraries that can help me achieve this?

  • Are you bound to use Winforms?
    – jmizv
    Jul 8 at 12:46

You don't need a 3rd party library to do this. Some built-in libraries will do this nicely.


  1. Draw each rectangle on top of the existing image
  2. Find the intersection of the two rectangles
  3. Draw a third rectangle (perhaps filled with an alpha blend)

Draw a rectangle:

        Rectangle ee = new Rectangle(10, 10, 30, 30);           
        using (Pen pen = new Pen(Color.Red, 2)) {
            e.Graphics.DrawRectangle(pen, ee);

Find intersection of two rectangles:

... use the Rectangle.Intersect method from System.Drawing ...

Returns a third Rectangle structure that represents the intersection of two other Rectangle structures. If there is no intersection, an empty Rectangle is returned.

Draw a rectangle with transparency / alpha:

var alpha = 128; // this is your transparency between 0-255
using (Graphics g = Graphics.FromImage(pb.Image))
    using(Brush brush = new SolidBrush(Color.FromArgb(alpha, red, green, blue)))
        g.FillRectangle(brush , x, y, width, height);



You can use LEADTOOLS Imaging Pro SDK technology in your application. https://www.leadtools.com/sdk/products/imaging-pro

You can use the LeadRect struct to see whether two rectangles intersect or not. Either using the Intersect or IntersectsWith methods. The code below adds two rectangles as annotations on a loaded image, finds the intersecting area, then adds a third rectangle annotation highlighting the intersection.

// Load the image
using (RasterCodecs _codecs = new RasterCodecs())
using (RasterImage _image = _codecs.Load(@"FILE PATH TO SOURCE IMAGE"))
    // Create the annotation container to store the annotation objects
    AnnContainer _container = new AnnContainer();
    _container.Mapper.MapResolutions(_image.XResolution, _image.YResolution, _image.XResolution, _image.YResolution);
    _container.Size = _container.Mapper.SizeToContainerCoordinates(_image.ImageSize.ToLeadSizeD());

    // Annotation unit is 1/720 of an inch
    double inch = 720.0;

    // Add 1st rectangle as annotation
    AnnRectangleObject _1stAnnRect = new AnnRectangleObject();
    _1stAnnRect.Rect = new LeadRectD(1 * inch, 2 * inch, 1 * inch, 3 * inch);
    _1stAnnRect.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("Green"), LeadLengthD.Create(1));

    // Add 2nd rectangle as annotation
    AnnRectangleObject _2ndAnnRect = new AnnRectangleObject();
    _2ndAnnRect.Rect = new LeadRectD(1 * inch, 2 * inch, 3 * inch, 2 * inch);
    _2ndAnnRect.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("Red"), LeadLengthD.Create(1));

    // Get the intersecting rectangle
    LeadRectD _intersectingRect = LeadRectD.IntersectRects(_1stAnnRect.Rect, _2ndAnnRect.Rect);

    // Add intersecting rectangle as annotation to the container
    AnnRectangleObject _intersectingAnnRect = new AnnRectangleObject();
    _intersectingAnnRect.Rect = _intersectingRect;
    _intersectingAnnRect.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("Blue"), LeadLengthD.Create(1));
    _intersectingAnnRect.Fill = AnnSolidColorBrush.Create("Yellow");

    // Optional, burn annotations to image
    AnnWinFormsRenderingEngine _renderingEngine = new AnnWinFormsRenderingEngine();
    using (var burnedImage = _renderingEngine.RenderOnImage(_container, _image))
        _codecs.Save(burnedImage, @"C:\Temp\test.jpg", RasterImageFormat.Jpeg, 0);

Here is a screenshot of the expected result: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.