From NPLOT Charting Library for .NET

Main: WPFNotes

WPF Notes

Notes on some WPF related things it took me a while to figure out how to do.

Setting the source of a WPF image to an embedded resource.

  Image titleImage = new titleImage(); // could also be XAML element
  System.IO.Stream file = 
  BitmapFrame fr = BitmapFrame.Create(file);
  titleImage.Source = fr;

Changing the opacity using an ImageBrush when drawing on a DrawingContext

  DrawingBrush db = new DrawingBrush();
  db.AlignmentX = AlignmentX.Left;
  db.AlignmentY = AlignmentY.Top;
  db.Stretch = Stretch.None;

  DrawingVisual dv = new DrawingVisual();
  DrawingContext dc2 = dv.RenderOpen();
  dc2.DrawRectangle( new SolidColorBrush(Color.FromArgb(30,0,0,0)), null, new Rect(0,0,50,100));
  dc2.DrawRectangle( new SolidColorBrush(Color.FromArgb(200,0,0,0)), null, new Rect(50,0,100,100));

  RenderTargetBitmap renderBitmap = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Default);

  ImageDrawing id = new ImageDrawing(renderBitmap, new Rect(0, 0, 100, 100));

  db.Drawing = id;

  dc.DrawRectangle(Brushes.Green, null, new Rect(20, 20, 100, 100));

Rendering to a DrawingVisual in a background thread.

	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : System.Windows.Window
		public delegate void myDelegate();

		public void CreateDrawing()
			DrawingVisual dv = new DrawingVisual();
			DrawingContext dc = dv.RenderOpen();
			dc.DrawLine(new Pen(Brushes.Black, 2.0), new Point(2, 2), new Point(200, 200));
			di = new DrawingImage(dv.Drawing);

		private DrawingImage di;

		public void CreateDrawingCompleted( IAsyncResult res )
			this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new myDelegate(DrawingToUI));

		public void DrawingToUI()
			myImage.Source = di;

		public Window1()

			myDelegate d = new myDelegate(CreateDrawing);
			d.BeginInvoke(CreateDrawingCompleted, null);
Retrieved from
Page last modified on June 17, 2007, at 05:15 AM EST