DrawingVisual和DrawingContext的简单应用(在Image上画图)
2013-06-13 10:57
489 查看
1.新建一个继承Image的基类,因为要在Image对象上画图,所以下面的代码必须放入基类中,如果放在加载图片的主窗体上,背景会变成黑色。
下面是新建的基类代码
View Code
protected override Visual GetVisualChild(int index) { return visuals[index]; } protected override int VisualChildrenCount { get { return visuals.Count; } }
下面是新建的基类代码
/// <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; } }
View Code
相关文章推荐
- 37.第三方(SDWebImage)的简单应用
- (简单图片浏览器)imageSwitcher与gallery结合应用
- 【android基础】之Android获取网络上的图片结合ImageView的简单应用
- Android中canvas画图的简单应用
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- delphi7 TRzCheckTree TImageList 简单应用
- Android-Universal-Image-Loader-master 框架简单应用
- java图像界面开发简单实例-ImageIO、JFileChooser、JMenu、JPanel的应用
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- 数据结构实验之串三:KMP应用 简单应用
- 树状数组的简单应用(求逆序对个数)
- power Designer 简单应用
- 配置文件简单应用
- makefile--工程管应用理器的简单
- JAVA中日志-log4j简单应用
- Java中Regex简单应用
- CGContextDrawImage画图
- Ajax的简单配置与应用.
- 数据结构实验之串一:KMP简单应用