---
title: "Write image with WPF"
canonical: https://www.graphicsmill.com/samples/Codecs/CustomCodecs/WriteImageWithWPF_cs
product: "Graphics Mill"
source: "Codecs/CustomCodecs/WriteImageWithWPF.cs"
---

# Write image with WPF

Demonstrates writing an image from a pipeline with the custom image writer.

## Instruction

Demonstrate how to pass an image to a pipeline using a custom image writer.

## Usings

```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Media.Imaging;
using Aurigma.GraphicsMill.Codecs;
```

## Code Snippet

```csharp
public sealed class WPFImageWriter : IImageWriter
{
    private readonly Stream stream;
    private WriteableBitmap writeableBitmap;
    private int offset = 0;

    public WPFImageWriter(Stream stream)
    {
        this.stream = stream;
    }

    public void Init(ImageParams imageParams)
    {
        this.writeableBitmap = new WriteableBitmap(
            imageParams.Width,
            imageParams.Height,
            (double)imageParams.DpiX,
            (double)imageParams.DpiY,
            ConvertFormat(imageParams.PixelFormat),
            null);
    }

    public void WriteStripe(Bitmap stripe)
    {
        try
        {
            this.writeableBitmap.Lock();

            IntPtr backBufferLine = this.writeableBitmap.BackBuffer + (this.writeableBitmap.BackBufferStride * this.offset);

            IntPtr stripeLine = stripe.Scan0;

            unsafe
            {
                for (int i = 0; i < stripe.Height; i++)
                {
                    Buffer.MemoryCopy(stripeLine.ToPointer(), backBufferLine.ToPointer(), this.writeableBitmap.BackBufferStride, stripe.Stride);
                    backBufferLine += this.writeableBitmap.BackBufferStride;
                    stripeLine += stripe.Stride;
                }
            }
        }
        finally
        {
            this.writeableBitmap.Unlock();
        }

        this.offset += stripe.Height;

        if (this.offset == this.writeableBitmap.PixelHeight)
        {
            var encoder = new TiffBitmapEncoder();

            encoder.Compression = TiffCompressOption.Zip;
            encoder.Frames.Add(BitmapFrame.Create(this.writeableBitmap));

            encoder.Save(this.stream);
        }
    }

    /// <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];
    }
}

internal class WriteImageWithWPF
{
    public static void Run()
    {
        using (var reader = ImageReader.Create("flower.jpg"))
        using (var fs = new FileStream("WroteByWPF.tif", FileMode.Create, FileAccess.Write))
        using (var writer = new CustomImageWriter(new WPFImageWriter(fs)))
        {
            Pipeline.Run(reader + writer);
        }
    }
}
```

## Explanation

Here is a C# program that utilizes a custom image writer to save an image using the memory-friendly Pipeline API:

This code defines `WPFImageWriter`, a custom image writer that processes image data in stripes and saves it as a TIFF file with ZIP compression. The `WriteImageWithWPF.Run()` method reads a JPEG image and writes it to `WroteByWPF.tif` using the Pipeline API.

## Tags

- [Format Conversion](<https://www.graphicsmill.com/samples/tags/format-conversion_md>): Converting images between formats
- [Metadata](<https://www.graphicsmill.com/samples/tags/metadata_md>): Reading and writing image metadata
- [Pipeline](<https://www.graphicsmill.com/samples/tags/pipeline_md>): Working with images via the Pipeline API
- [Read](<https://www.graphicsmill.com/samples/tags/read_md>): Reading images with codecs
- [Stream](<https://www.graphicsmill.com/samples/tags/stream_md>): Reading and writing images from streams
- [TIFF](<https://www.graphicsmill.com/samples/tags/tiff_md>): Reading and writing TIFF images
- [WPF](<https://www.graphicsmill.com/samples/tags/wpf_md>): Interoperability and conversion with WPF images
- [Write](<https://www.graphicsmill.com/samples/tags/write_md>): Writing images with codecs

## Input

- [flower.jpg](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/flower_jpg>) (download)

## Output

- [WroteByWPF.tif](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/WroteByWPF_tif>) (download)

## Used APIs

- [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.Codecs.CustomImageWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_CustomImageWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.IImageWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_IImageWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.Create(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_ImageReader_Create_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.ImageParams](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_ImageParams.htm.md>)
- [Aurigma.GraphicsMill.ImageParams.DpiX](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_ImageParams_DpiX.htm.md>)
- [Aurigma.GraphicsMill.ImageParams.DpiY](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_ImageParams_DpiY.htm.md>)
- [Aurigma.GraphicsMill.ImageParams.PixelFormat](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_ImageParams_PixelFormat.htm.md>)
- [Aurigma.GraphicsMill.UnsupportedPixelFormatException](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_UnsupportedPixelFormatException.htm.md>)
