Table of Contents

Class WPFImageHelper

Namespace
VisioForge.Core.UI.WPF
Assembly
VisioForge.Core.dll

Static utility class providing image format conversion between WinForms and WPF image types.

This helper class bridges the gap between System.Drawing.Bitmap (WinForms) and WPF's BitmapSource, enabling seamless image data exchange between different UI frameworks within the same application. Essential for mixed-mode applications using both WinForms and WPF components.

Key features:

  • Bidirectional conversion between Bitmap and BitmapSource
  • Proper native resource management with automatic cleanup
  • Performance-optimized conversion using native Windows APIs
  • Memory-safe operations with proper handle disposal
  • Support for various pixel formats and color depths

Performance characteristics:

  • Uses native GDI+ and WPF interop for optimal performance
  • Minimizes memory allocations through efficient resource handling
  • Proper disposal of native handles to prevent memory leaks
  • Optimized for frequent conversions in real-time applications

Thread safety: All methods are thread-safe and can be called from any thread. Native resource management ensures proper cleanup regardless of calling thread.

public static class WPFImageHelper

Inheritance

Inherited Members

Methods

ToBitmap(BitmapSource)

Converts a WPF BitmapSource to WinForms Bitmap with efficient memory management.

This extension method provides reverse conversion from WPF's BitmapSource to System.Drawing.Bitmap format, enabling use of WPF images in GDI+ operations.

Conversion process:

  1. Creates a BMP encoder for lossless format conversion
  2. Encodes the BitmapSource to a memory stream
  3. Creates a new Bitmap from the encoded stream data
  4. Properly manages memory stream disposal

Performance considerations:

  • Uses BMP format for lossless conversion
  • Memory stream provides efficient in-memory conversion
  • Preserves all pixel data and format information
  • Automatic resource cleanup with using statements

Thread safety: Safe to call from any thread.

public static Bitmap ToBitmap(this BitmapSource bitmapSource)

Parameters

bitmapSource BitmapSource

The source WPF BitmapSource to convert, or null.

Returns

Bitmap

A WinForms Bitmap containing the converted image data, or null if input is null.

ToBitmapSource(Bitmap)

Converts a WinForms Bitmap to WPF BitmapSource with proper resource management.

This extension method provides efficient conversion from System.Drawing.Bitmap to WPF's BitmapSource format, enabling display of GDI+ images in WPF controls.

Conversion process:

  1. Extracts the native bitmap handle from the GDI+ Bitmap
  2. Creates a WPF BitmapSource using CreateBitmapSourceFromHBitmap
  3. Properly disposes of the native handle to prevent memory leaks
  4. Returns the WPF-compatible BitmapSource

Performance considerations:

  • Uses native Windows APIs for optimal conversion speed
  • Automatic resource cleanup prevents memory leaks
  • Supports all common pixel formats and bit depths
  • Preserves image quality and metadata

Thread safety: Safe to call from any thread.

public static BitmapSource ToBitmapSource(this Bitmap bitmap)

Parameters

bitmap Bitmap

The source WinForms bitmap to convert, or null.

Returns

BitmapSource

A WPF BitmapSource containing the converted image data, or null if input is null.