WriteableBitmap Class

Provides a BitmapSource that can be written to and updated.

Namespace: System.Windows.Media.Imaging
Assembly: PresentationCore (in presentationcore.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public sealed class WriteableBitmap : BitmapSource
public final class WriteableBitmap extends BitmapSource
public final class WriteableBitmap extends BitmapSource
You cannot use this managed class in XAML.

The following example demonstrates how a WriteableBitmap can be used as the source of an Image and updated using a DispatcherTimer.

using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Threading;

namespace SDKSample
    public class app : Application
        Window mainWindow;

        protected override void OnStartup(StartupEventArgs e)
        private void CreateAndShowMainWindow()
            // Create the application's main window
            mainWindow = new Window();
            mainWindow.Title = "Writeable Bitmap";
            mainWindow.Height = 200;
            mainWindow.Width = 200;
            // Define the Image element
            _random.Stretch = Stretch.None;
            _random.Margin = new Thickness(20);

            // Define a StackPanel to host Controls
            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Orientation = Orientation.Vertical;
            myStackPanel.Height = 200;
            myStackPanel.VerticalAlignment = VerticalAlignment.Top;
            myStackPanel.HorizontalAlignment = HorizontalAlignment.Center;

            // Add the Image to the parent StackPanel

            // Add the StackPanel as the Content of the Parent Window Object
            mainWindow.Content = myStackPanel;

            // DispatcherTimer setup
            // The DispatcherTimer will be used to update _random every
            //    second with a new random set of colors.
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.IsEnabled = true;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        //  System.Windows.Threading.DispatcherTimer.Tick handler
        //  Updates the Image element with new random colors
        private void dispatcherTimer_Tick(object sender, EventArgs e)
            //Update the color array with new random colors
            Random value = new Random();

            //Update writeable bitmap with the colorArray to the image.
            _wb.WritePixels(_rect, _colorArray, _stride, 0);

            //Set the Image source.
            _random.Source = _wb;


        private Image _random = new Image();
        // Create the writeable bitmap will be used to write and update.
        private static WriteableBitmap _wb =
            new WriteableBitmap(100, 100, 96, 96, PixelFormats.Bgra32, null);
        // Define the rectangle of the writeable image we will modify. 
        // The size is that of the writeable bitmap.
        private static Int32Rect _rect = new Int32Rect(0, 0, _wb.PixelWidth, _wb.PixelHeight);
        // Calculate the number of bytes per pixel. 
        private static int _bytesPerPixel = (_wb.Format.BitsPerPixel + 7) / 8;
        // Stride is bytes per pixel times the number of pixels.
        // Stride is the byte width of a single rectangle row.
        private static int _stride = _wb.PixelWidth * _bytesPerPixel;

        // Create a byte array for a the entire size of bitmap.
        private static int _arraySize = _stride * _wb.PixelHeight;
        private static byte[] _colorArray = new byte[_arraySize];


    // Define a static entry class
    internal static class EntryClass
        private static void Main()
            app app = new app();


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0