{"id":5005,"date":"2024-10-02T22:01:01","date_gmt":"2024-10-02T14:01:01","guid":{"rendered":""},"modified":"2024-10-02T22:01:01","modified_gmt":"2024-10-02T14:01:01","slug":"wpf brush_\u4ec0\u4e48\u662fWPF","status":"publish","type":"post","link":"https:\/\/mushiming.com\/5005.html","title":{"rendered":"wpf brush_\u4ec0\u4e48\u662fWPF"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u8f6c\u8f7d https:\/\/www.cnblogs.com\/seekdream\/p\/5239149.html<\/p>\n

\u4ec0\u4e48\u662fWPF\u4e2d\u7684\u753b\u5237\uff0c\u987e\u540d\u601d\u4e49\u5c4f\u5e55\u4e0a\u53ef\u89c1\u7684\u6240\u6709\u5185\u5bb9\u90fd\u53ef\u89c1\uff0c\u56e0\u4e3a\u5b83\u662f\u7531\u753b\u7b14\u7ed8\u5236\u7684\u3002 \u4f8b\u5982\uff0c\u753b\u7b14\u7528\u4e8e\u63cf\u8ff0\u6309\u94ae\u7684\u80cc\u666f\u3001\u6587\u672c\u7684\u524d\u666f\u548c\u5f62\u72b6\u7684\u586b\u5145\u6548\u679c\u9996\u5148\u6211\u4eec\u6765\u770b\u770bWPF\u4e2d\u5305\u542b\u54ea\u4e9b\u7c7b\u578b\u7684\u753b\u5237\uff0c\u4e0b\u9762\u7684\u4e00\u7ec4\u56fe\u7247\u4e2d\u63cf\u8ff0\u4e86\u6211\u4eec\u5e38\u7528\u7684\u753b\u5237\u7c7b\u578b\uff0c\u540e\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u7ec4\u5177\u4f53\u7684\u793a\u4f8b\u6765\u8bb2\u8ff0\u6bcf\u4e00\u79cd\u753b\u5237\u7684\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u3002\"wpf<\/p>\n

\u56fe\u4e00 \u5e38\u89c1\u7684\u753b\u5237\u7c7b\u578b<\/p>\n

1 SolidColorBrush<\/p>\n

SolidColorBrush\u4f7f\u7528\u7eaf\u8272\u7ed8\u5236\u533a\u57df Color \u3002 \u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6307\u5b9a\u7684 Color SolidColorBrush \uff1a\u4f8b\u5982\uff0c\u53ef\u4ee5\u6307\u5b9a\u5176 alpha\u3001\u7ea2\u8272\u3001\u84dd\u8272\u548c\u7eff\u8272\u901a\u9053\uff0c\u6216\u4f7f\u7528\u7c7b\u63d0\u4f9b\u7684\u9884\u5b9a\u4e49\u989c\u8272\u4e4b\u4e00 Colors \u3002<\/p>\n

\u4e0b\u9762\u7684\u793a\u4f8b\u4f7f\u7528 SolidColorBrush \u7ed8\u5236 Fill \u7684 Rectangle \u3002 \u4e0b\u56fe\u663e\u793a\u4e86\u7ed8\u5236\u7684\u77e9\u5f62<\/p>\n

1.1<\/span> \u901a\u8fc7C#\u4ee3\u7801\u76f4\u63a5\u8fdb\u884c\u521b\u5efa Rectangle exampleRectangle =<\/span> new Rectangle<\/span>(<\/span>)<\/span>;<\/span> exampleRectangle.<\/span>Width =<\/span> 75<\/span>;<\/span> exampleRectangle.<\/span>Height =<\/span> 75<\/span>;<\/span> \/\/ Create a SolidColorBrush and use it to<\/span> \/\/ paint the rectangle.<\/span> SolidColorBrush myBrush =<\/span> new SolidColorBrush<\/span>(<\/span>Colors.<\/span>Red)<\/span>;<\/span> exampleRectangle.<\/span>Fill =<\/span> myBrush;<\/span> 1.2<\/span> \u5728xaml\u4e2d\u76f4\u63a5\u8fdb\u884c\u521b\u5efa <<\/span>Rectangle Width=<\/span>\"75\"<\/span> Height=<\/span>\"75\"<\/span>><\/span> <<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>SolidColorBrush Color=<\/span>\"Red\"<\/span> \/<\/span>><\/span> <<\/span>\/<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>\/<\/span>Rectangle><\/span> <\/code><\/pre>\n

2 GradientBrush<\/p>\n

\u6e10\u53d8\u753b\u5237\u4e3b\u8981\u5305\u62ec\u7ebf\u6027\u6e10\u53d8\u7ed8\u5236\u548c\u5f84\u5411\u6e10\u53d8\u753b\u5237\u7ed8\u5236\uff0c\u4e0b\u9762\u5206\u522b\u6765\u4ecb\u7ecd\u7740\u4e24\u79cd\u7c7b\u578b\u3002<\/p>\n

LinearGradientBrush\u4f7f\u7528\u7ebf\u6027\u6e10\u53d8\u7ed8\u5236\u533a\u57df\u3002 \u7ebf\u6027\u6e10\u53d8\u5728\u7ebf\u6761\uff08\u6e10\u53d8\u8f74\uff09\u4e2d\u6df7\u5408\u4e86\u4e24\u79cd\u6216\u591a\u79cd\u989c\u8272\u3002 \u4f7f\u7528 GradientStop \u5bf9\u8c61\u53ef\u4ee5\u6307\u5b9a\u6e10\u53d8\u4e2d\u7684\u989c\u8272\u53ca\u5176\u4f4d\u7f6e\u3002<\/p>\n

\u4e0b\u9762\u7684\u793a\u4f8b\u4f7f\u7528 LinearGradientBrush \u7ed8\u5236 Fill \u7684 Rectangle \u3002 \u4e0b\u56fe\u663e\u793a\u4e86\u7ed8\u5236\u7684\u77e9\u5f62\u3002<\/p>\n

2.1<\/span> \u4f7f\u7528C#\u4ee3\u7801\u8fdb\u884c\u521b\u5efa Rectangle exampleRectangle =<\/span> new Rectangle<\/span>(<\/span>)<\/span>;<\/span> exampleRectangle.<\/span>Width =<\/span> 75<\/span>;<\/span> exampleRectangle.<\/span>Height =<\/span> 75<\/span>;<\/span> \/\/ Create a LinearGradientBrush and use it to<\/span> \/\/ paint the rectangle.<\/span> LinearGradientBrush myBrush =<\/span> new LinearGradientBrush<\/span>(<\/span>)<\/span>;<\/span> myBrush.<\/span>GradientStops.<\/span>Add<\/span>(<\/span>new GradientStop<\/span>(<\/span>Colors.<\/span>Yellow,<\/span> 0.0<\/span>)<\/span>)<\/span>;<\/span> myBrush.<\/span>GradientStops.<\/span>Add<\/span>(<\/span>new GradientStop<\/span>(<\/span>Colors.<\/span>Orange,<\/span> 0.5<\/span>)<\/span>)<\/span>;<\/span> myBrush.<\/span>GradientStops.<\/span>Add<\/span>(<\/span>new GradientStop<\/span>(<\/span>Colors.<\/span>Red,<\/span> 1.0<\/span>)<\/span>)<\/span>;<\/span> exampleRectangle.<\/span>Fill =<\/span> myBrush;<\/span> \u3000\u30002.2<\/span> \u4f7f\u7528xaml\u8fdb\u884c\u521b\u5efa <<\/span>Rectangle Width=<\/span>\"75\"<\/span> Height=<\/span>\"75\"<\/span>><\/span> <<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>LinearGradientBrush><\/span> <<\/span>GradientStop Color=<\/span>\"Yellow\"<\/span> Offset=<\/span>\"0.0\"<\/span> \/<\/span>><\/span> <<\/span>GradientStop Color=<\/span>\"Orange\"<\/span> Offset=<\/span>\"0.5\"<\/span> \/<\/span>><\/span> <<\/span>GradientStop Color=<\/span>\"Red\"<\/span> Offset=<\/span>\"1.0\"<\/span> \/<\/span>><\/span> <<\/span>\/<\/span>LinearGradientBrush><\/span> <<\/span>\/<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>\/<\/span>Rectangle><\/span>\u3000 <\/code><\/pre>\n

\u5f84\u5411\u6e10\u53d8\u753b\u5237\u548c\u8fd9\u4e2a\u7c7b\u4f3c\uff0c\u8fd9\u91cc\u5c31\u4e0d\u5728\u8fdb\u884c\u5355\u72ec\u8bb2\u8ff0\u3002
3 ImageBrush<\/p>\n

\u8fd9\u4e2a\u662f\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528\u5230\u7684\u4e00\u79cd\u7c7b\u578b\uff0c\u6bd4\u5982\u6211\u4eec\u7ecf\u5e38\u4f1a\u5c06\u4e00\u526f\u56fe\u7247\u6765\u586b\u5145\u4e00\u4e2aRectangle\u7684\u5185\u90e8<\/p>\n

\u56fe\u4e09 ImageBrush<\/p>\n

 3.1<\/span> \u4f7f\u7528C#\u4ee3\u7801\u521b\u5efa Rectangle exampleRectangle =<\/span> new Rectangle<\/span>(<\/span>)<\/span>;<\/span> exampleRectangle.<\/span>Width =<\/span> 75<\/span>;<\/span> exampleRectangle.<\/span>Height =<\/span> 75<\/span>;<\/span> \/\/ Create an ImageBrush and use it to<\/span> \/\/ paint the rectangle.<\/span> ImageBrush myBrush =<\/span> new ImageBrush<\/span>(<\/span>)<\/span>;<\/span> myBrush.<\/span>ImageSource =<\/span> new BitmapImage<\/span>(<\/span>new Uri<\/span>(<\/span>@\"sampleImages\\pinkcherries.jpg\"<\/span>,<\/span> UriKind.<\/span>Relative)<\/span>)<\/span>;<\/span> exampleRectangle.<\/span>Fill =<\/span> myBrush;<\/span> 3.2<\/span> \u4f7f\u7528xaml\u4ee3\u7801\u8fdb\u884c\u521b\u5efa <<\/span>Rectangle Width=<\/span>\"75\"<\/span> Height=<\/span>\"75\"<\/span>><\/span> <<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>ImageBrush ImageSource=<\/span>\"sampleImages\\pinkcherries.jpg\"<\/span> \/<\/span>><\/span> <<\/span>\/<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>\/<\/span>Rectangle><\/span> \u3000\u3000 <\/code><\/pre>\n

4 DrawingBrush<\/p>\n

DrawingBrush\u4f7f\u7528\u7ed8\u5236\u533a\u57df Drawing \u3002 Drawing\u53ef\u4ee5\u5305\u542b\u5f62\u72b6\u3001\u56fe\u50cf\u3001\u6587\u672c\u548c\u5a92\u4f53\u3002<\/p>\n

\u4e0b\u9762\u7684\u793a\u4f8b\u4f7f\u7528 DrawingBrush \u7ed8\u5236 Fill \u7684 Rectangle \u3002 \u4e0b\u56fe\u663e\u793a\u4e86\u7ed8\u5236\u7684\u77e9\u5f62,\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u3002<\/p>\n

4.1<\/span> \u4f7f\u7528C#\u4ee3\u7801\u5b9e\u73b0 Rectangle exampleRectangle =<\/span> new Rectangle<\/span>(<\/span>)<\/span>;<\/span> exampleRectangle.<\/span>Width =<\/span> 75<\/span>;<\/span> exampleRectangle.<\/span>Height =<\/span> 75<\/span>;<\/span> \/\/ Create a DrawingBrush and use it to<\/span> \/\/ paint the rectangle.<\/span> DrawingBrush myBrush =<\/span> new DrawingBrush<\/span>(<\/span>)<\/span>;<\/span> GeometryDrawing backgroundSquare =<\/span> new GeometryDrawing<\/span>(<\/span> Brushes.<\/span>White,<\/span> null,<\/span> new RectangleGeometry<\/span>(<\/span>new Rect<\/span>(<\/span>0<\/span>,<\/span> 0<\/span>,<\/span> 100<\/span>,<\/span> 100<\/span>)<\/span>)<\/span>)<\/span>;<\/span> GeometryGroup aGeometryGroup =<\/span> new GeometryGroup<\/span>(<\/span>)<\/span>;<\/span> aGeometryGroup.<\/span>Children.<\/span>Add<\/span>(<\/span>new RectangleGeometry<\/span>(<\/span>new Rect<\/span>(<\/span>0<\/span>,<\/span> 0<\/span>,<\/span> 50<\/span>,<\/span> 50<\/span>)<\/span>)<\/span>)<\/span>;<\/span> aGeometryGroup.<\/span>Children.<\/span>Add<\/span>(<\/span>new RectangleGeometry<\/span>(<\/span>new Rect<\/span>(<\/span>50<\/span>,<\/span> 50<\/span>,<\/span> 50<\/span>,<\/span> 50<\/span>)<\/span>)<\/span>)<\/span>;<\/span> LinearGradientBrush checkerBrush =<\/span> new LinearGradientBrush<\/span>(<\/span>)<\/span>;<\/span> checkerBrush.<\/span>GradientStops.<\/span>Add<\/span>(<\/span>new GradientStop<\/span>(<\/span>Colors.<\/span>Black,<\/span> 0.0<\/span>)<\/span>)<\/span>;<\/span> checkerBrush.<\/span>GradientStops.<\/span>Add<\/span>(<\/span>new GradientStop<\/span>(<\/span>Colors.<\/span>Gray,<\/span> 1.0<\/span>)<\/span>)<\/span>;<\/span> GeometryDrawing checkers =<\/span> new GeometryDrawing<\/span>(<\/span>checkerBrush,<\/span> null,<\/span> aGeometryGroup)<\/span>;<\/span> DrawingGroup checkersDrawingGroup =<\/span> new DrawingGroup<\/span>(<\/span>)<\/span>;<\/span> checkersDrawingGroup.<\/span>Children.<\/span>Add<\/span>(<\/span>backgroundSquare)<\/span>;<\/span> checkersDrawingGroup.<\/span>Children.<\/span>Add<\/span>(<\/span>checkers)<\/span>;<\/span> myBrush.<\/span>Drawing =<\/span> checkersDrawingGroup;<\/span> myBrush.<\/span>Viewport =<\/span> new Rect<\/span>(<\/span>0<\/span>,<\/span> 0<\/span>,<\/span> 0.25<\/span>,<\/span> 0.25<\/span>)<\/span>;<\/span> myBrush.<\/span>TileMode =<\/span> TileMode.<\/span>Tile;<\/span> exampleRectangle.<\/span>Fill =<\/span> myBrush;<\/span> 4.2<\/span> \u4f7f\u7528xaml\u8fdb\u884c\u521b\u5efa \u8fd9\u79cd\u65b9\u5f0f\u662f\u6211\u4eec\u6bd4\u8f83\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u6765\u5177\u4f53\u770b\u770b <<\/span>Rectangle Width=<\/span>\"75\"<\/span> Height=<\/span>\"75\"<\/span>><\/span> <<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>DrawingBrush Viewport=<\/span>\"0,0,0.25,0.25\"<\/span> TileMode=<\/span>\"Tile\"<\/span>><\/span> <<\/span>DrawingBrush.<\/span>Drawing><\/span> <<\/span>DrawingGroup><\/span> <<\/span>GeometryDrawing Brush=<\/span>\"White\"<\/span>><\/span> <<\/span>GeometryDrawing.<\/span>Geometry><\/span> <<\/span>RectangleGeometry Rect=<\/span>\"0,0,100,100\"<\/span> \/<\/span>><\/span> <<\/span>\/<\/span>GeometryDrawing.<\/span>Geometry><\/span> <<\/span>\/<\/span>GeometryDrawing><\/span> <<\/span>GeometryDrawing><\/span> <<\/span>GeometryDrawing.<\/span>Geometry><\/span> <<\/span>GeometryGroup><\/span> <<\/span>RectangleGeometry Rect=<\/span>\"0,0,50,50\"<\/span> \/<\/span>><\/span> <<\/span>RectangleGeometry Rect=<\/span>\"50,50,50,50\"<\/span> \/<\/span>><\/span> <<\/span>\/<\/span>GeometryGroup><\/span> <<\/span>\/<\/span>GeometryDrawing.<\/span>Geometry><\/span> <<\/span>GeometryDrawing.<\/span>Brush><\/span> <<\/span>LinearGradientBrush><\/span> <<\/span>GradientStop Offset=<\/span>\"0.0\"<\/span> Color=<\/span>\"Black\"<\/span> \/<\/span>><\/span> <<\/span>GradientStop Offset=<\/span>\"1.0\"<\/span> Color=<\/span>\"Gray\"<\/span> \/<\/span>><\/span> <<\/span>\/<\/span>LinearGradientBrush><\/span> <<\/span>\/<\/span>GeometryDrawing.<\/span>Brush><\/span> <<\/span>\/<\/span>GeometryDrawing><\/span> <<\/span>\/<\/span>DrawingGroup><\/span> <<\/span>\/<\/span>DrawingBrush.<\/span>Drawing><\/span> <<\/span>\/<\/span>DrawingBrush><\/span> <<\/span>\/<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>\/<\/span>Rectangle><\/span> <\/code><\/pre>\n

5 VisualBrush<\/p>\n

\u8fd9\u4e2a\u662f\u6211\u4eec\u53ef\u80fd\u4f7f\u7528\u4e0d\u592a\u591a\u5e76\u4e14\u4e0d\u662f\u975e\u5e38\u6570\u91cf\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u8fd9\u91cc\u5bf9\u8fd9\u4e2a\u8fdb\u884c\u91cd\u70b9\u7684\u89e3\u91ca\u4e00\u4e0b\uff0c\u9996\u5148\u770b\u770bVisualBrush\u7684\u89e3\u91ca\uff0cmsdn\u4e0a\u9762\u7684\u89e3\u91ca\u662f\u4f7f\u7528 Visual \u7ed8\u5236\u533a\u57df\uff0c\u90a3\u4e48\u6211\u4eec\u518d\u6765\u770b\u770b\u4ec0\u4e48\u662fVisual\u5462\uff1f\u5b98\u65b9\u7684\u89e3\u91ca\u662f\uff1a\u83b7\u53d6\u6216\u8bbe\u7f6e\u753b\u7b14\u7684\u5185\u5bb9\uff0cVisual \u662f\u76f4\u63a5\u7ee7\u627f\u81eaDependencyObject\uff0cUIElement\u4e5f\u662f\u76f4\u63a5\u7ee7\u627f\u81eaVisual\uff0c\u6211\u4eec\u5e38\u89c1\u7684\u63a7\u4ef6\u90fd\u662f\u7ee7\u627f\u81eaVisual\uff0cVisual\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u7ed1\u5b9a\uff0c\u6bd4\u5982\u6211\u4eec\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u8fd9\u6837\u5199\uff1a<\/p>\n

1
\u3000\u3000
\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u628a\u4e00\u4e2a\u7ee7\u627f\u81eaVisual\u7684\u63a7\u4ef6\uff08\u6b64\u5904\u4e3aTodayPeriod\u7684\u4e00\u4e2aUserControl\uff09\u901a\u8fc7\u7ed1\u5b9a\u7684\u65b9\u5f0f\u7ed1\u5b9a\u5230Visual\u4e2d\uff0c\u6211\u4eec\u518d\u6765\u770b\u770bVisualBrush\u8fd9\u4e2a\u7c7b\uff0c\u9996\u5148\u6765\u770b\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\uff1a <\/p>\n

System.Object<\/p>\n

System.Windows.Threading.DispatcherObject System.Windows.DependencyObject System.Windows.Freezable System.Windows.Media.Animation.Animatable System.Windows.Media.Brush System.Windows.Media.TileBrush System.Windows.Media.VisualBrush <\/code><\/pre>\n

\u6240\u4ee5\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6eBorder\u7684Background\u4e3aVisualBrush\uff0c\u6211\u4eec\u5728\u5f88\u591a\u5730\u65b9\u90fd\u662f\u53ef\u4ee5\u4f7f\u7528VisualBrush\u7684\uff0c\u8fd9\u91cc\u9700\u8981\u6211\u4eec\u5e73\u65f6\u5728\u4f7f\u7528\u7684\u65f6\u5019\u4e0d\u65ad\u53bb\u53cd\u601d\u53bb\u603b\u7ed3\u3002<\/p>\n

\u6269\u5c55\u5e94\u7528<\/p>\n

\u53e6\u5916\u4e00\u4e2a\u5e38\u7528\u5230VisualBrush\u7684\u5730\u65b9\u5c31\u662f\u4f7f\u7528DrawingContext\u7684\u65f6\u5019\uff0c\u56e0\u4e3aDrawingContext\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662fBrush\uff0c\u8fd9\u91cc\u6211\u4eec\u7ecf\u5e38\u53ef\u4ee5\u4f7f\u7528VisualBrush\u4f5c\u4e3a\u5bf9\u8c61\u4f20\u9012\u8fdb\u53bb\uff0c\u6bd4\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u6211\u4eec\u9700\u8981\u5c06WPF\u4e2d\u7684MainWindow\u4fdd\u5b58\u4e3a\u4e00\u5f20\u56fe\u7247\u4fdd\u5b58\u5230\u672c\u5730\uff0c\u8fd9\u4e2a\u65f6\u5019VisualBrush\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\uff0c\u6211\u4eec\u6765\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n

public static<\/span> void<\/span> CreateBitmapFromVisual<\/span>(<\/span>Visual target,<\/span> string fileName)<\/span> { \n   <\/span> if<\/span> (<\/span>target ==<\/span> null ||<\/span> string.<\/span>IsNullOrEmpty<\/span>(<\/span>fileName)<\/span>)<\/span> { \n   <\/span> return<\/span>;<\/span> }<\/span> Rect bounds =<\/span> VisualTreeHelper.<\/span>GetDescendantBounds<\/span>(<\/span>target)<\/span>;<\/span> RenderTargetBitmap renderTarget =<\/span> new RenderTargetBitmap<\/span>(<\/span>(<\/span>Int32)<\/span>bounds.<\/span>Width,<\/span> (<\/span>Int32)<\/span>bounds.<\/span>Height,<\/span> 96<\/span>,<\/span> 96<\/span>,<\/span> PixelFormats.<\/span>Pbgra32)<\/span>;<\/span> DrawingVisual visual =<\/span> new DrawingVisual<\/span>(<\/span>)<\/span>;<\/span> using<\/span> (<\/span>DrawingContext context =<\/span> visual.<\/span>RenderOpen<\/span>(<\/span>)<\/span>)<\/span> { \n   <\/span> VisualBrush visualBrush =<\/span> new VisualBrush<\/span>(<\/span>target)<\/span>;<\/span> context.<\/span>DrawRectangle<\/span>(<\/span>visualBrush,<\/span> null,<\/span> new Rect<\/span>(<\/span>new Point<\/span>(<\/span>)<\/span>,<\/span> bounds.<\/span>Size)<\/span>)<\/span>;<\/span> }<\/span> renderTarget.<\/span>Render<\/span>(<\/span>visual)<\/span>;<\/span> PngBitmapEncoder bitmapEncoder =<\/span> new PngBitmapEncoder<\/span>(<\/span>)<\/span>;<\/span> bitmapEncoder.<\/span>Frames.<\/span>Add<\/span>(<\/span>BitmapFrame.<\/span>Create<\/span>(<\/span>renderTarget)<\/span>)<\/span>;<\/span> using<\/span> (<\/span>Stream stm =<\/span> File.<\/span>Create<\/span>(<\/span>fileName)<\/span>)<\/span> { \n   <\/span> bitmapEncoder.<\/span>Save<\/span>(<\/span>stm)<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

\u8fd9\u91cc\u4f20\u5165\u7684\u53c2\u6570\u662ftarget\u548cfileName,\u524d\u8005\u662f\u4e00\u4e2aVisual,\u901a\u8fc7\u7ee7\u627f\u5173\u7cfb\u6211\u4eec\u77e5\u9053\u5927\u90e8\u5206\u7684\u63a7\u4ef6\u90fd\u662f\u4eceVisual\u7ee7\u627f\u4e0b\u6765\u7684\uff0c\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u6211\u4eec\u9996\u5148\u521b\u5efaDrawingVisual\u5bf9\u8c61\uff0c\u7136\u540e\u7528\u8fd9\u4e2a\u5bf9\u8c61\u521b\u5efadrawingContext\uff0c\u7136\u540e\u4f7f\u7528drawingContext\u7684DrawRectangle\u65b9\u6cd5\u6765\u7ed8\u5236\u5bf9\u8c61\uff0c\u8fd9\u91cc\u4f20\u5165\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u5305\u542b\u4f20\u5165target\u5bf9\u8c61\u7684VisualBrush\u5bf9\u8c61\uff0c\u901a\u8fc7\u8fd9\u4e2a\u8fc7\u7a0b\u6211\u4eec\u5c31\u80fd\u51c6\u786e\u5730\u5c06\u8bd5\u56fe\u5bf9\u8c61\u4fdd\u5b58\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u56fe\u7247\u4e86\u3002<\/p>\n

\u6211\u4eec\u518d\u4ee5\u524d\u9762\u7684Rectangle\u4e3a\u793a\u4f8b\u6765\u8fdb\u884c\u8bf4\u660e\u4ece\u800c\u52a0\u6df1\u5bf9\u6574\u4e2a\u77e5\u8bc6\u70b9\u7684\u638c\u63e1\u7a0b\u5ea6\uff0c\u6211\u4eec\u9996\u5148\u6765\u770b\u4e0b\u9762\u7684\u622a\u56fe\u3002<\/p>\n

\u56fe\u4e94 VisualBrush<\/p>\n

5.1<\/span> C#\u4ee3\u7801\u7684\u5b9e\u73b0 Rectangle exampleRectangle =<\/span> new Rectangle<\/span>(<\/span>)<\/span>;<\/span> exampleRectangle.<\/span>Width =<\/span> 75<\/span>;<\/span> exampleRectangle.<\/span>Height =<\/span> 75<\/span>;<\/span> \/\/ Create a VisualBrush and use it<\/span> \/\/ to paint the rectangle.<\/span> VisualBrush myBrush =<\/span> new VisualBrush<\/span>(<\/span>)<\/span>;<\/span> \/\/<\/span> \/\/ Create the brush's contents.<\/span> \/\/<\/span> StackPanel aPanel =<\/span> new StackPanel<\/span>(<\/span>)<\/span>;<\/span> \/\/ Create a DrawingBrush and use it to<\/span> \/\/ paint the panel.<\/span> DrawingBrush myDrawingBrushBrush =<\/span> new DrawingBrush<\/span>(<\/span>)<\/span>;<\/span> GeometryGroup aGeometryGroup =<\/span> new GeometryGroup<\/span>(<\/span>)<\/span>;<\/span> aGeometryGroup.<\/span>Children.<\/span>Add<\/span>(<\/span>new RectangleGeometry<\/span>(<\/span>new Rect<\/span>(<\/span>0<\/span>,<\/span> 0<\/span>,<\/span> 50<\/span>,<\/span> 50<\/span>)<\/span>)<\/span>)<\/span>;<\/span> aGeometryGroup.<\/span>Children.<\/span>Add<\/span>(<\/span>new RectangleGeometry<\/span>(<\/span>new Rect<\/span>(<\/span>50<\/span>,<\/span> 50<\/span>,<\/span> 50<\/span>,<\/span> 50<\/span>)<\/span>)<\/span>)<\/span>;<\/span> RadialGradientBrush checkerBrush =<\/span> new RadialGradientBrush<\/span>(<\/span>)<\/span>;<\/span> checkerBrush.<\/span>GradientStops.<\/span>Add<\/span>(<\/span>new GradientStop<\/span>(<\/span>Colors.<\/span>MediumBlue,<\/span> 0.0<\/span>)<\/span>)<\/span>;<\/span> checkerBrush.<\/span>GradientStops.<\/span>Add<\/span>(<\/span>new GradientStop<\/span>(<\/span>Colors.<\/span>White,<\/span> 1.0<\/span>)<\/span>)<\/span>;<\/span> GeometryDrawing checkers =<\/span> new GeometryDrawing<\/span>(<\/span>checkerBrush,<\/span> null,<\/span> aGeometryGroup)<\/span>;<\/span> myDrawingBrushBrush.<\/span>Drawing =<\/span> checkers;<\/span> aPanel.<\/span>Background =<\/span> myDrawingBrushBrush;<\/span> \/\/ Create some text.<\/span> TextBlock someText =<\/span> new TextBlock<\/span>(<\/span>)<\/span>;<\/span> someText.<\/span>Text =<\/span> \"Hello, World\"<\/span>;<\/span> FontSizeConverter fSizeConverter =<\/span> new FontSizeConverter<\/span>(<\/span>)<\/span>;<\/span> someText.<\/span>FontSize =<\/span> (<\/span>double<\/span>)<\/span>fSizeConverter.<\/span>ConvertFromString<\/span>(<\/span>\"10pt\"<\/span>)<\/span>;<\/span> someText.<\/span>Margin =<\/span> new Thickness<\/span>(<\/span>10<\/span>)<\/span>;<\/span> aPanel.<\/span>Children.<\/span>Add<\/span>(<\/span>someText)<\/span>;<\/span> myBrush.<\/span>Visual =<\/span> aPanel;<\/span> exampleRectangle.<\/span>Fill =<\/span> myBrush;<\/span> 5.2<\/span> \u4f7f\u7528xaml\u5b9e\u73b0 <<\/span>Rectangle Width=<\/span>\"75\"<\/span> Height=<\/span>\"75\"<\/span>><\/span> <<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>VisualBrush TileMode=<\/span>\"Tile\"<\/span>><\/span> <<\/span>VisualBrush.<\/span>Visual><\/span> <<\/span>StackPanel><\/span> <<\/span>StackPanel.<\/span>Background><\/span> <<\/span>DrawingBrush><\/span> <<\/span>DrawingBrush.<\/span>Drawing><\/span> <<\/span>GeometryDrawing><\/span> <<\/span>GeometryDrawing.<\/span>Brush><\/span> <<\/span>RadialGradientBrush><\/span> <<\/span>GradientStop Color=<\/span>\"MediumBlue\"<\/span> Offset=<\/span>\"0.0\"<\/span> \/<\/span>><\/span> <<\/span>GradientStop Color=<\/span>\"White\"<\/span> Offset=<\/span>\"1.0\"<\/span> \/<\/span>><\/span> <<\/span>\/<\/span>RadialGradientBrush><\/span> <<\/span>\/<\/span>GeometryDrawing.<\/span>Brush><\/span> <<\/span>GeometryDrawing.<\/span>Geometry><\/span> <<\/span>GeometryGroup><\/span> <<\/span>RectangleGeometry Rect=<\/span>\"0,0,50,50\"<\/span> \/<\/span>><\/span> <<\/span>RectangleGeometry Rect=<\/span>\"50,50,50,50\"<\/span> \/<\/span>><\/span> <<\/span>\/<\/span>GeometryGroup><\/span> <<\/span>\/<\/span>GeometryDrawing.<\/span>Geometry><\/span> <<\/span>\/<\/span>GeometryDrawing><\/span> <<\/span>\/<\/span>DrawingBrush.<\/span>Drawing><\/span> <<\/span>\/<\/span>DrawingBrush><\/span> <<\/span>\/<\/span>StackPanel.<\/span>Background><\/span> <<\/span>TextBlock FontSize=<\/span>\"10pt\"<\/span> Margin=<\/span>\"10\"<\/span>><\/span>Hello,<\/span> World!<\/span><<\/span>\/<\/span>TextBlock><\/span> <<\/span>\/<\/span>StackPanel><\/span> <<\/span>\/<\/span>VisualBrush.<\/span>Visual><\/span> <<\/span>\/<\/span>VisualBrush><\/span> <<\/span>\/<\/span>Rectangle.<\/span>Fill><\/span> <<\/span>\/<\/span>Rectangle><\/span> <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"wpf brush_\u4ec0\u4e48\u662fWPF\u80cc\u666f\u4ec0\u4e48\u662fWPF\u4e2d\u7684\u753b\u5237\uff0c\u987e\u540d\u601d\u4e49\u5c4f\u5e55\u4e0a\u53ef\u89c1\u7684\u6240\u6709\u5185\u5bb9\u90fd\u53ef\u89c1\uff0c\u56e0\u4e3a\u5b83\u662f\u7531\u753b\u7b14\u7ed8\u5236\u7684","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5005"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=5005"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5005\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=5005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=5005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=5005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}