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

'Declaration
Public NotInheritable Class WriteableBitmap
	Inherits BitmapSource
'Usage
Dim instance As WriteableBitmap

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.

Imports System
Imports System.IO
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Threading
Imports System.Threading

Namespace SDKSample

    Public Class app
        Inherits Application
        Private myWindow As Window


        Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs)
            MyBase.OnStartup(e)
            CreateAndShowMainWindow()

        End Sub 'OnStartup

        Private Sub CreateAndShowMainWindow()
            ' Create the application's main window
            myWindow = New Window()
            myWindow.Title = "Writeable Bitmap"
            myWindow.Height = 200
            myWindow.Width = 200

            ' Define the Image element
            _random.Stretch = Stretch.None
            _random.Margin = New Thickness(20)

            ' Define a StackPanel to host Controls
            Dim myStackPanel As New StackPanel()
            myStackPanel.Orientation = Orientation.Vertical
            myStackPanel.Height = 200
            myStackPanel.VerticalAlignment = VerticalAlignment.Top
            myStackPanel.HorizontalAlignment = HorizontalAlignment.Center

            ' Add the Image to the parent StackPanel
            myStackPanel.Children.Add(_random)

            ' Add the StackPanel as the Content of the Parent Window Object
            myWindow.Content = myStackPanel
            myWindow.Show()

            ' DispatcherTimer setup
            ' The DispatcherTimer will be used to update _random every
            '    second with a new random set of colors.
            Dim dispatcherTimer As New DispatcherTimer()
            AddHandler dispatcherTimer.Tick, AddressOf dispatcherTimer_Tick
            dispatcherTimer.IsEnabled = True
            dispatcherTimer.Interval = New TimeSpan(0, 0, 1)
            dispatcherTimer.Start()

        End Sub 'CreateAndShowMainWindow

        '  System.Windows.Threading.DispatcherTimer.Tick handler
        '
        '  Updates the Image element with new random colors
        Private Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
            'Update the color array with new random colors
            Dim value As New Random()
            value.NextBytes(_colorArray)

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

            'Set the Image source.
            _random.Source = _wb

        End Sub 'dispatcherTimer_Tick 

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

        ' Create a byte array for a the entire size of bitmap.
        Private Shared _arraySize As Integer = _stride * _wb.PixelHeight
        Private Shared _colorArray(_arraySize) As Byte
    End Class 'app 

    ' Define a static entry class

    Friend NotInheritable Class EntryClass

        <System.STAThread()> _
        Public Shared Sub Main()
            Dim app As New app()
            app.Run()

        End Sub 'Main
    End Class 'EntryClass
End Namespace 'SDKSample

System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.DependencyObject
       System.Windows.Freezable
         System.Windows.Media.Animation.Animatable
           System.Windows.Media.ImageSource
             System.Windows.Media.Imaging.BitmapSource
              System.Windows.Media.Imaging.WriteableBitmap

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

Community Additions

ADD
Show: