/// <summary>Captures a snapshot of the source element as a bitmap image. The snapshot is created based on the specified rendering parameters.</summary> /// <param name="sourceElement">The source element.</param> /// <param name="bitmapSize">The bitmap size.</param> /// <param name="scalingMode">The bitmap scaling mode.</param> /// <param name="bitmapDpi">The bitmap dpi.</param> /// <param name="pixelFormat">The bitmap pixel format.</param> /// <returns>The snapshot of the source element.</returns> public static BitmapSource CaptureSnapshot(FrameworkElement sourceElement, Size bitmapSize, BitmapScalingMode scalingMode, Vector bitmapDpi, PixelFormat pixelFormat) { if (sourceElement == null || bitmapSize.IsZero()) return null; var snapshot = new RenderTargetBitmap((int)bitmapSize.Width, (int)bitmapSize.Height, bitmapDpi.X, bitmapDpi.Y, pixelFormat); sourceElement.SetValue(RenderOptions.BitmapScalingModeProperty, scalingMode); snapshot.Render(sourceElement); sourceElement.ClearValue(RenderOptions.BitmapScalingModeProperty); snapshot.Freeze(); return snapshot; }