---
title: "Convert From WPF"
canonical: https://www.graphicsmill.com/samples/Interoperability/WPFInteroperability/ConvertFromWPF_cs
product: "Graphics Mill"
source: "Interoperability/WPFInteroperability/ConvertFromWPF.cs"
---

# Convert From WPF

Converts WPF BitmapFrame to GraphicsMill Bitmap.

## Instruction

Write a C# program that converts WPF BitmapFrame loaded from "Chicago.jpg" using BitmapFrame.Create with PreservePixelFormat to GraphicsMill Bitmap via custom extension method ToAurigmaBitmap, which creates matching PixelFormat bitmap, copies pixels directly using CopyPixels to Scan0 with stride handling, sets DPI from WPF source, and saves as TIFF "ChicagoSavedGotFromWPF.tif" while supporting BGR24/RGB24, BGR32/RGB32, BGRA32/ARGB32, and Grayscale8/16 format mappings via bidirectional dictionary conversion.

## Usings

```csharp
using System.Collections.Generic;
using System.IO;
using System.Windows.Media.Imaging;
```

## Code Snippet

```csharp
public static class Extensions
{
    /// <summary>
    /// An extension to convert Graphics Mill Bitmap to WPF BitmapSource
    /// </summary>
    public static BitmapSource ToBitmapSource(this Bitmap bitmap)
    {
        return BitmapFrame.Create(
            bitmap.Width,
            bitmap.Height,
            bitmap.DpiX,
            bitmap.DpiY,
            ConvertFormat(bitmap.PixelFormat),
            null,
            bitmap.Scan0,
            bitmap.Stride * bitmap.Height,
            bitmap.Stride);
    }

    /// <summary>
    /// An extension to convert WPF BitmapFrame to Graphics Mill Bitmap
    /// </summary>
    public static Bitmap ToAurigmaBitmap(this BitmapFrame bitmapFrame)
    {
        var bitmap = new Bitmap(bitmapFrame.PixelWidth, bitmapFrame.PixelHeight, ConvertFormat(bitmapFrame.Format));

        bitmap.DpiX = (float)bitmapFrame.DpiX;
        bitmap.DpiY = (float)bitmapFrame.DpiY;

        bitmapFrame.CopyPixels(new System.Windows.Int32Rect(0, 0, bitmapFrame.PixelWidth, bitmapFrame.PixelHeight), bitmap.Scan0, bitmap.Stride * bitmap.Height, bitmap.Stride);

        return bitmap;
    }

    /// <summary>
    /// Converts WPF pixel format to corresponding one for Graphics Mill.
    /// </summary>
    private static PixelFormat ConvertFormat(System.Windows.Media.PixelFormat format)
    {
        var dict = new Dictionary<System.Windows.Media.PixelFormat, Aurigma.GraphicsMill.PixelFormat>
        {
            { System.Windows.Media.PixelFormats.Bgr24, Aurigma.GraphicsMill.PixelFormat.Format24bppRgb },
            { System.Windows.Media.PixelFormats.Bgr32, Aurigma.GraphicsMill.PixelFormat.Format32bppRgb },
            { System.Windows.Media.PixelFormats.Bgra32, Aurigma.GraphicsMill.PixelFormat.Format32bppArgb },
            { System.Windows.Media.PixelFormats.Gray8, Aurigma.GraphicsMill.PixelFormat.Format8bppGrayscale },
            { System.Windows.Media.PixelFormats.Gray16, Aurigma.GraphicsMill.PixelFormat.Format16bppGrayscale },
        };

        if (!dict.ContainsKey(format))
        {
            throw new Aurigma.GraphicsMill.UnsupportedPixelFormatException();
        }

        return dict[format];
    }

    /// <summary>
    /// Converts Graphics Mill pixel format to corresponding one for WPF.
    /// </summary>
    private static System.Windows.Media.PixelFormat ConvertFormat(Aurigma.GraphicsMill.PixelFormat format)
    {
        var dict = new Dictionary<Aurigma.GraphicsMill.PixelFormat, System.Windows.Media.PixelFormat>
    {
        { Aurigma.GraphicsMill.PixelFormat.Format24bppRgb, System.Windows.Media.PixelFormats.Bgr24 },
        { Aurigma.GraphicsMill.PixelFormat.Format32bppRgb, System.Windows.Media.PixelFormats.Bgr32 },
        { Aurigma.GraphicsMill.PixelFormat.Format32bppArgb, System.Windows.Media.PixelFormats.Bgra32 },
        { Aurigma.GraphicsMill.PixelFormat.Format8bppGrayscale, System.Windows.Media.PixelFormats.Gray8 },
        { Aurigma.GraphicsMill.PixelFormat.Format16bppGrayscale, System.Windows.Media.PixelFormats.Gray16 },
    };

        if (!dict.ContainsKey(format))
        {
            throw new Aurigma.GraphicsMill.UnsupportedPixelFormatException();
        }

        return dict[format];
    }
}

public class ConvertFromWpf
{
    public static void Run()
    {
        using (var fs = new FileStream("Chicago.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            /*
             There are not too many formats that can be converted with direct pixel copying.
             According to Microsoft documentation, with BitmapCreateOptions.None the PixelFormat
             of the image is chosen by the system depending on what the system determines will yield the best performance.
            */
            var bitmapFrame = BitmapFrame.Create(fs, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

            using (var bitmap = bitmapFrame.ToAurigmaBitmap())
            {
                bitmap.Save("ChicagoSavedGotFromWPF.tif");
            }
        }
    }
}
```

## Explanation

Below is a C# code sample that demonstrates WPF BitmapFrame to GraphicsMill Bitmap conversion with direct pixel copying:

The code implements bidirectional extension methods for WPF-GraphicsMill interoperability using direct pixel memory copying via CopyPixels/Scan0 without format conversion overhead. It maps WPF PixelFormats (Bgr24→Format24bppRgb, Bgra32→Format32bppArgb, Gray8→Format8bppGrayscale) through dictionary lookups with UnsupportedPixelFormatException for incompatible formats, preserves DPI metadata, and uses BitmapCreateOptions.PreservePixelFormat with BitmapCacheOption.OnLoad for WPF-side format fidelity. This zero-copy approach enables WPF UI integration with GraphicsMill processing pipelines while maintaining pixel data integrity and performance.

## Tags

- [Bitmap](<https://www.graphicsmill.com/samples/tags/bitmap_md>): Working with images via the Bitmap API
- [Interoperability](<https://www.graphicsmill.com/samples/tags/interoperability_md>): Interop with GDI+ and other libraries
- [WPF](<https://www.graphicsmill.com/samples/tags/wpf_md>): Interoperability and conversion with WPF images

## Input

- [Chicago.jpg](<https://www.graphicsmill.com/samples/Interoperability/compiled/Debug/Chicago_jpg>) (preview)

## Output

- [ChicagoSavedGotFromWPF.tif](<https://www.graphicsmill.com/samples/Interoperability/compiled/Debug/ChicagoSavedGotFromWPF_tif>) (download)

## Used APIs

- [Aurigma.GraphicsMill.Bitmap.DpiX](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_DpiX.htm.md>)
- [Aurigma.GraphicsMill.Bitmap.DpiY](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_DpiY.htm.md>)
- [Aurigma.GraphicsMill.Bitmap.PixelFormat](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_PixelFormat.htm.md>)
- [Aurigma.GraphicsMill.Bitmap.Scan0](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_Scan0.htm.md>)
- [Aurigma.GraphicsMill.Bitmap.Stride](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_Stride.htm.md>)
- [Aurigma.GraphicsMill.UnsupportedPixelFormatException](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_UnsupportedPixelFormatException.htm.md>)
