Convert To WPF

Interoperability Bitmap WPF

Converts Graphics Mill Bitmap to WPF BitmapSource.

Сode Snippet

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 ConvertToWpf
{
    public static void Run()
    {
        using (var bitmap = new Bitmap("Chicago.jpg"))
        {
            var bitmapSource = bitmap.ToBitmapSource();

            var encoder = new TiffBitmapEncoder();
            encoder.Compression = TiffCompressOption.Zip;
            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

            using (var fs = new FileStream("ChicagoSavedWithWPF.tif", FileMode.Create, FileAccess.ReadWrite, FileShare.Write))
            {
                encoder.Save(fs);
            }
        }
    }
}

Input

Chicago.jpg

Output

ChicagoSavedWithWPF.tif

Download

For AI-assisted development: Download Graphics Mill Code Samples XML Catalog