对UIElement进行刷新
Size tSize = new Size(tPenNode.Width, tPenNode.Height); tPenNode.Measure(tSize); tPenNode.Arrange(new Rect(new Point(0, 0), tSize)); DrawingVisual tDrawingVisual = new DrawingVisual(); using (DrawingContext context = tDrawingVisual.RenderOpen()) { VisualBrush tVisualBrush = new VisualBrush(tPenNode); tVisualBrush.Stretch = Stretch.Fill; context.DrawRectangle(tVisualBrush, null, new Rect(0, 0, 80, 80)); context.Close(); } RenderTargetBitmap tRenderTargetBitmap = new RenderTargetBitmap(80,80, 96, 96, PixelFormats.Default); tRenderTargetBitmap.Render(tDrawingVisual); using (FileStream tFileStream = new FileStream("C:\\1.png", FileMode.Create, FileAccess.Write)) { PngBitmapEncoder tPngBitmapEncoder = new PngBitmapEncoder(); tPngBitmapEncoder.Interlace = PngInterlaceOption.On; tPngBitmapEncoder.Frames.Add(BitmapFrame.Create(tRenderTargetBitmap)); tPngBitmapEncoder.Save(tFileStream); tFileStream.Close(); }