1.新建一个继承Image的基类,因为要在Image对象上画图,所以下面的代码必须放入基类中,如果放在加载图片的主窗体上,背景会变成黑色。
protected override Visual GetVisualChild(int index) { return visuals[index]; } protected override int VisualChildrenCount { get { return visuals.Count; } }
下面是新建的基类代码
public class DrawingImages : Image { public List<Visual> visuals = new List<Visual>(); protected override Visual GetVisualChild(int index) { return this.visuals[index]; } protected override int VisualChildrenCount { get { return visuals.Count; } } public void AddVisual(Visual visual) { this.visuals.Add(visual); base.AddVisualChild(visual); base.AddLogicalChild(visual); } public void DeleteVisual(Visual visual) { this.visuals.Remove(visual); base.RemoveVisualChild(visual); base.RemoveLogicalChild(visual); } public void DeleteVisualAll() { foreach (var item in visuals) { base.RemoveVisualChild(item); base.RemoveLogicalChild(item); } this.visuals.Clear(); } }
2.主窗体代码
xaml窗体代码默认即可。后台代码如下:
/// <summary> /// DrawingImage.xaml 的交互逻辑 /// </summary> public partial class DrawingImage : Window { private DrawingImages img_ = new DrawingImages(); public DrawingImage() { InitializeComponent(); this.Content = img_; img_.Stretch = Stretch.Fill; img_.MouseLeftButtonDown += new MouseButtonEventHandler(img__MouseLeftButtonDown); img_.MouseMove += new MouseEventHandler(img__MouseMove); img_.MouseLeftButtonUp += new MouseButtonEventHandler(img__MouseLeftButtonUp); img_.MouseDown += new MouseButtonEventHandler(img__MouseDown); InitDrawImage(); } void img__MouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton==MouseButton.Right) img_.DeleteVisualAll(); } private void InitDrawImage() { int X = (int)this.Width; int Y = (int)this.Height; DrawingVisual dv = new DrawingVisual(); using (DrawingContext dc = dv.RenderOpen()) { Rect rect = new Rect(new Point(0, 0), new Size(X, Y)); Brush bc = new SolidColorBrush(Colors.LightBlue); Pen pen = new Pen(Brushes.Pink, 1); dc.DrawRectangle(bc, pen, rect); dc.DrawText(new FormattedText("LightBlue", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("微软雅黑"), (int)Y / 32, Brushes.White), new Point((int)X / 25, (int)Y / 21)); } var rtbitmap = new RenderTargetBitmap(X, Y, 0.0, 0.0, PixelFormats.Default); rtbitmap.Render(dv); img_.Source = rtbitmap; } private Point? _startPoint; private DrawingVisual selectionSquare; void img__MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { _startPoint = e.GetPosition(this.img_); selectionSquare = new DrawingVisual(); this.img_.AddVisual(selectionSquare); this.img_.CaptureMouse(); } private Brush selectionSquareBrush = Brushes.Transparent; private Pen selectionSquarePen = new Pen(Brushes.LightPink, 2); private void DrawSelectionSquare(Point point1, Point point2) { if (selectionSquare == null) return; using (DrawingContext dc = selectionSquare.RenderOpen()) { dc.DrawRectangle(selectionSquareBrush, selectionSquarePen, new Rect(point1, point2)); } } void img__MouseMove(object sender, MouseEventArgs e) { if (_startPoint == null) return; Point endPoint = e.GetPosition(this.img_); DrawSelectionSquare((Point)_startPoint, endPoint); } void img__MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this.img_.ReleaseMouseCapture(); _startPoint = null; } }