public static void DisposeOverlayImage() { if (OverlayImage != null) { OverlayImage.Dispose(); OverlayImage = null; } }
/// <summary> /// Redraws the OverlayImage and calls Invalidate on the PictureBox. /// Removes the OverlayImage if not in EDIT mode or if there is no /// HitLine and no HitPoint. /// </summary> public void redrawOverlay() { Debug.WriteLine("redrawOverlay: EDIT:" + " HitLine=" + ((HitLine == null) ? "null" : "\"" + HitLine.Desc + "\"") + " HitPoint=" + ((HitPoint == null) ? "null" : "X=" + HitPoint.Point.X)); if (ActiveMode != Mode.EDIT) { if (OverlayImage != null) { OverlayImage.Dispose(); OverlayImage = null; } pictureBox.Invalidate(); return; } if (HitLine == null && HitPoint == null) { if (OverlayImage != null) { OverlayImage.Dispose(); OverlayImage = null; } pictureBox.Invalidate(); return; } OverlayImage = new Bitmap(Image.Width, Image.Height); float size = HIT_TOLERANCE; float size2 = HIT_TOLERANCE * 1.5f; using (var brush = new SolidBrush(SELECT_COLOR)) using (var littlePen = new Pen(SELECT_COLOR, LINE_WIDTH)) using (var littlePenBlack = new Pen(Color.Black, 2)) using (Graphics g = Graphics.FromImage(OverlayImage)) { g.Clear(Color.Transparent); if (HitLine != null) { // Draw new color over the lines Point[] points = HitLine.Points.ToArray(); g.DrawLines(littlePen, points); foreach (Point point in HitLine.Points) { RectangleF rect = centeredSquare(point, size); g.FillEllipse(brush, rect); } } if (HitPoint != null) { RectangleF rect = centeredSquare(HitPoint.Point, size2); g.DrawEllipse(littlePenBlack, rect); } } pictureBox.Invalidate(); }