Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Input
Namespace WriteableBitmapDemo
Friend Class Program
Private Shared writeableBitmap As WriteableBitmap
Private Shared w As Window
Private Shared i As Image
<STAThread()> _
Shared Sub Main(ByVal args() As String)
i = New Image()
RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.NearestNeighbor)
RenderOptions.SetEdgeMode(i, EdgeMode.Aliased)
w = New Window()
w.Content = i
w.Show()
writeableBitmap = New WriteableBitmap(CInt(w.ActualWidth), CInt(w.ActualHeight), 96, 96, PixelFormats.Bgr32, Nothing)
i.Source = writeableBitmap
i.Stretch = Stretch.None
i.HorizontalAlignment = HorizontalAlignment.Left
i.VerticalAlignment = VerticalAlignment.Top
AddHandler i.MouseMove, AddressOf i_MouseMove
AddHandler i.MouseLeftButtonDown, AddressOf i_MouseLeftButtonDown
AddHandler i.MouseRightButtonDown, AddressOf i_MouseRightButtonDown
AddHandler w.MouseWheel, AddressOf w_MouseWheel
Dim app As New Application()
app.Run()
End Sub
' The DrawPixel method updates the WriteableBitmap by using
' unsafe code to write a pixel into the back buffer.
Private Shared Sub DrawPixel(ByVal e As MouseEventArgs)
Dim column As Integer = CInt(e.GetPosition(i).X)
Dim row As Integer = CInt(e.GetPosition(i).Y)
' Reserve the back buffer for updates.
writeableBitmap.Lock()
' Get a pointer to the back buffer.
Dim pBackBuffer As IntPtr = writeableBitmap.BackBuffer
' Find the address of the pixel to draw.
Dim backBufferOffset As Int32 = (row * writeableBitmap.BackBufferStride) + (column * 4)
' Compute the pixel's color.
Dim color_data As Integer = 255 << 16 ' R
color_data = color_data Or 128 << 8 ' G
color_data = color_data Or 255 << 0 ' B
' Assign the color data to the pixel.
System.Runtime.InteropServices.Marshal.WriteInt32(pBackBuffer, backBufferOffset, color_data)
' Specify the area of the bitmap that changed.
writeableBitmap.AddDirtyRect(New Int32Rect(column, row, 1, 1))
' Release the back buffer and make it available for display.
writeableBitmap.Unlock()
End Sub
Private Shared Sub ErasePixel(ByVal e As MouseEventArgs)
Dim ColorData() As Byte = {0, 0, 0, 0} ' B G R
Dim rect As New Int32Rect(CInt(e.GetPosition(i).X), CInt(e.GetPosition(i).Y), 1, 1)
writeableBitmap.WritePixels(rect, ColorData, 4, 0)
End Sub
Private Shared Sub i_MouseRightButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
ErasePixel(e)
End Sub
Private Shared Sub i_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
DrawPixel(e)
End Sub
Private Shared Sub i_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If e.LeftButton = MouseButtonState.Pressed Then
DrawPixel(e)
ElseIf e.RightButton = MouseButtonState.Pressed Then
ErasePixel(e)
End If
End Sub
Private Shared Sub w_MouseWheel(ByVal sender As Object, ByVal e As MouseWheelEventArgs)
Dim m As System.Windows.Media.Matrix = i.RenderTransform.Value
If e.Delta > 0 Then
m.ScaleAt(1.5, 1.5, e.GetPosition(w).X, e.GetPosition(w).Y)
Else
m.ScaleAt(1.0 / 1.5, 1.0 / 1.5, e.GetPosition(w).X, e.GetPosition(w).Y)
End If
i.RenderTransform = New MatrixTransform(m)
End Sub
End Class
End Namespace