title: Draw text on WriteableBitmap
body: <p>I would like to draw some text on a <code>WriteableBitmap</code>. This doesn't seem possible, but I found converting WriteableBitmap to Bitmap in C# <a href="https://stackoverflow.com/questions/17298034/converting-writeablebitmap-to-bitmap-in-c-sharp">here</a> and <a href="http://tech.pro/tutorial/654/csharp-snippet-tutorial-how-to-draw-text-on-an-image" rel="nofollow noreferrer">this</a>. </p>
  
  <p>Here's the call (with conversion method included, too):</p>
  
  <pre><code>     // Draw dino names...
              Graphics g = Graphics.FromImage(BitmapFromWriteableBitmap(writeableBitmap));
              g.DrawString(Dinosaurs[i].PersonalName, new Font("Tahoma", 40), System.Drawing.Brushes.White, new PointF((float)Dinosaurs[i].Head.X, (float)Dinosaurs[i].Head.Y));
  
          } // for i
  
      }// DrawDinos2d
  
      private System.Drawing.Bitmap BitmapFromWriteableBitmap(WriteableBitmap writeBmp)
      {
          System.Drawing.Bitmap bmp;
          using (MemoryStream outStream = new MemoryStream())
          {
              BitmapEncoder enc = new BmpBitmapEncoder();
              enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
              enc.Save(outStream);
              bmp = new System.Drawing.Bitmap(outStream);
          }
          return bmp;
      }
  </code></pre>
  
  <p>I've checked in the debugger and <code>g</code> has a good values (not null) but nothing is drawing on the screen. I should also mention that the string that is being passed to <code>DrawString</code> is good and the X,Y of the point is good (positive integers about 1000,800). I think it's either drawing off the screen or maybe the X,Y coordinates aren't translating correctly.</p>
  
  <p>Any ideas? Thanks.</p>
  
  <p>Here's the XAML (if this helps):</p>
  
  <pre><code>&lt;Window x:Class="DinosaurIsland.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Dinosaur Island" Height="600" Width="600" WindowState="Normal"  Icon="/DinosaurIsland;component/Icon1.ico" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" WindowStartupLocation="CenterOwner"&gt;
  &lt;Window.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;DataTemplate DataType="{x:Type BitmapImage}"&gt;
              &lt;Image Source="{Binding}" /&gt;
          &lt;/DataTemplate&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="Resources.xaml"/&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Window.Resources&gt;
  
  &lt;DockPanel&gt;
      &lt;Menu x:Name="MainMenu" DockPanel.Dock="Top"&gt;
          &lt;MenuItem Header="_File"&gt;
              &lt;MenuItem Header="_Open Dinosaur Island 'snapshot' file..." x:Name="OpenSnapshotFile" Click="OpenSnapshotFile_click" /&gt;
              &lt;MenuItem Header="_Save"/&gt;
              &lt;MenuItem Header="_Exit" x:Name="ExitApp" Click="ExitAppClick" /&gt;
          &lt;/MenuItem&gt;
          &lt;MenuItem Header="_Height Map"&gt;
              &lt;MenuItem Header="Load Height Map..." Name="LoadHeightMap" Click="LoadHeightMapClick" /&gt;
              &lt;Separator /&gt;
              &lt;MenuItem Header="Display Height Map" x:Name="DisplayHeightMap" Click="DisplayHeightMapClick" /&gt;
          &lt;/MenuItem&gt;
          &lt;MenuItem Header="Terrain"&gt;
              &lt;MenuItem Header="Load Terrain Map..." x:Name="LoadTerrainMap" Click="LoadTerrainMap_Click" /&gt;
              &lt;MenuItem Header="Draw Terrain..." x:Name="DrawTerrain" Click="DisplayTerrainPaintBoxClick" /&gt;
              &lt;MenuItem Header="Save Terrain Map..." x:Name="SaveTerrainMap" Click="SaveTerrainMap_Click"/&gt;
              &lt;MenuItem Header="Get Terrain Data From BMP..." x:Name="TerrainFromBMP" Click="TerrainFromBMP_Click" /&gt;
              &lt;Separator /&gt;
              &lt;MenuItem Header="Adjust Terrain Transparency..." x:Name="AdjustTerrainTransparency"  Click="AdjustTerrainTransparency_Click"/&gt;
              &lt;MenuItem Header="Display Terrain Map" x:Name="DisplayTerrainMap"  Click="DisplayTerrainMap_Click"/&gt;
          &lt;/MenuItem&gt;
  
          &lt;MenuItem Header="_Vegetation"&gt;
              &lt;MenuItem Header="Plant Vegetation..." x:Name="PlantVegetation" Click="PlantVegetation_Click" /&gt;
              &lt;Separator /&gt;
              &lt;MenuItem Header="Load Vegetation Map..." x:Name="LoadVegetation" Click="LoadVegetation_Click" /&gt;
              &lt;MenuItem Header="Save Vegetation Map..." x:Name="SaveVegetation" Click="SaveVegetation_Click" /&gt;
              &lt;Separator /&gt;
              &lt;MenuItem Header="Display Vegetation"  Click="DisplayVegetation_Click" /&gt;
  
          &lt;/MenuItem&gt;
  
          &lt;MenuItem Header="Dinosaurs"&gt;
              &lt;MenuItem Header="Edit / Place Dinosaurs..." x:Name="EditDinosaurs" Click="EditDinosaurs_Click" /&gt;
              &lt;Separator /&gt;
              &lt;MenuItem Header="Load Dinosaur Map" Name="LoadDinosaurnMap" Click="LoadDinosaurs_Click"/&gt;
              &lt;MenuItem Header="Save Dinosaur Map" Name="SaveDinosaurMap" Click="SaveDinosaurs_Click"/&gt;
              &lt;Separator /&gt;
              &lt;MenuItem Header="Terrain/Slope Effect..." Name="TerrainSlope"  Click="TerrainSlope_Click"/&gt;
              &lt;MenuItem Header="Probability of Smelling Dinosaur..." Name="SmellProbability"  Click="SmellProbability_Click"/&gt;
          &lt;/MenuItem&gt;
  
          &lt;MenuItem Header="Time"&gt;
              &lt;MenuItem Header="Start..." x:Name="AdvanceTime" Click="StartTime_Click" /&gt;
              &lt;MenuItem Header="Stop..." x:Name="StopTime" Click="StopTime_Click" /&gt;
              &lt;Separator /&gt;
              &lt;MenuItem Header="Adjust Time Step..." x:Name="AdjustTimeStep"  Click="AdjustTimeStep_Click"/&gt;
          &lt;/MenuItem&gt;
  
          &lt;MenuItem Header="Debug"&gt;
              &lt;MenuItem Header="A* trace" Name="AStarTrace" Click="AStarTrace_Click" /&gt;
              &lt;MenuItem Header="Show Dinosaur Goals" Name="DinoGoals" Click="DinoGoals_Click" /&gt;
              &lt;MenuItem Header="Show Dinosaur Path" Name="DinoPath" IsChecked="False"  Click="DinoPath_Click" /&gt;
              &lt;MenuItem Header="Show Predator Scent Path" Name="StinkPath" IsChecked="False"  Click="StinkPath_Click" /&gt;
              &lt;MenuItem Header="Show Dinosaur Vision Angles" Name="VisionAngles" IsChecked="True"  Click="VisionAngles_Click" /&gt;
              &lt;MenuItem Header="Show Dinosaur Axis" Name="DinoAxis" IsChecked="True"  Click="DinoAxis_Click" /&gt;
          &lt;/MenuItem&gt;
  
          &lt;MenuItem Header="Help"&gt;
              &lt;MenuItem Header="About Dinosaur Island" Name="AboutDinosaurIsland" Click="AboutDinoIslandClick" /&gt;
          &lt;/MenuItem&gt;
      &lt;/Menu&gt;
  
      &lt;StatusBar DockPanel.Dock="Bottom"&gt;
          &lt;TextBlock Name="StatusBarField1"&gt;Location = X,Y&lt;/TextBlock&gt;
          &lt;Separator/&gt;
          &lt;TextBlock Name="StatusBarField2"&gt;Elevation = X&lt;/TextBlock&gt;
          &lt;Separator/&gt;
          &lt;TextBlock Name="StatusBarField3"&gt;Terrain = None&lt;/TextBlock&gt;
          &lt;Separator/&gt;
          &lt;TextBlock Name="StatusBarField4"&gt;Plants = None&lt;/TextBlock&gt;
          &lt;Separator/&gt;
          &lt;TextBlock Name="StatusBarField5"&gt;Dinosaurs = None&lt;/TextBlock&gt;
          &lt;Separator/&gt;
          &lt;TextBlock Name="StatusBarField6"&gt;Zoom&lt;/TextBlock&gt;
          &lt;Separator/&gt;
          &lt;TextBlock Name="StatusBarField7"&gt;Time 0:00&lt;/TextBlock&gt;
          &lt;Separator/&gt;
          &lt;TextBlock Name="StatusBarField8"&gt;Wind direction: 000&lt;/TextBlock&gt;
          &lt;Separator/&gt;
          &lt;TextBlock Name="StatusBarField9"&gt;Speed: 000&lt;/TextBlock&gt;
      &lt;/StatusBar&gt;
  
      &lt;Label DockPanel.Dock="Bottom" Content="Scale = 2000 meters" Height="23" HorizontalAlignment="Center"  Name="HorizScaleDisplayText"  Width="127" /&gt;
      &lt;Label DockPanel.Dock="Bottom" Content="└───────────────────────────────┴───────────────────────────────────┘" Height="20" HorizontalAlignment="Center"  Name="HorizScaleDisplayLine"  Width="423" /&gt;
      &lt;Grid&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="Auto"/&gt;
          &lt;ColumnDefinition Width="*"/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;Slider Grid.Column="0" Orientation="Vertical" HorizontalAlignment="Left" Minimum="1" x:Name="slider"/&gt;
      &lt;ScrollViewer Name="scrollViewer" Grid.Column="1" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" Margin="0,0,0,6"&gt;
  
          &lt;Grid Name="grid" Width="400" Height="400" RenderTransformOrigin="0.5,0.5"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="37*" /&gt;
                      &lt;RowDefinition Height="363*" /&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="297*" /&gt;
                      &lt;ColumnDefinition Width="103*" /&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Grid.LayoutTransform&gt;
                  &lt;TransformGroup&gt;
                      &lt;ScaleTransform x:Name="scaleTransform"/&gt;
                  &lt;/TransformGroup&gt;
              &lt;/Grid.LayoutTransform&gt;
              &lt;Viewbox x:Name="viewBox" Margin="-35,-12,-22,22" Grid.ColumnSpan="2" Grid.RowSpan="2"&gt;
                      &lt;ContentPresenter x:Name="contentPresenter" Width="350" Height="350" &gt;
                          &lt;ContentPresenter.Content&gt;
                              &lt;Image x:Name="image" Width="350" Height="350"&gt;
                                  &lt;Image.Source &gt;
                                      &lt;BitmapImage x:Name="HeightMapImage" UriSource="DinoIslandLogo.bmp" /&gt;
                                  &lt;/Image.Source&gt;
                              &lt;/Image&gt;
                          &lt;/ContentPresenter.Content&gt;
                      &lt;/ContentPresenter&gt;
                  &lt;/Viewbox&gt;
              &lt;/Grid&gt;
          &lt;/ScrollViewer&gt;
      &lt;/Grid&gt;
  &lt;/DockPanel&gt;
  </code></pre>
  
  <p></p>
  
  <p>And this is how I display the bitmap:</p>
  
  <pre><code>this.image.Source = writeableBitmap;
  </code></pre>