title: Simple example of using DrawImage in VB WPF
body: <p>I was hoping to figure this out on my own, but after hours of Googling I have countless examples that I can't get to work.  Here's the scenario.  Hopefully someone can provide a simple solution.</p>
  
  <p>I have a VB WPF application.  It communicates with a particular web service to get a Base64 string for an image.  I am able to convert this string to a <strong>System.Drawing.Image</strong> object using the following code:</p>
  
  <pre><code>Public Function Base64ToImage(ByVal base64str As String) As System.Drawing.Image
      'Setup image and get data stream together
      Dim img As System.Drawing.Image
      Dim MS As System.IO.MemoryStream = New System.IO.MemoryStream
      Dim b64 As String = base64str.Replace(" ", "+")
      Dim b() As Byte
  
      'Converts the base64 encoded msg to image data
      b = Convert.FromBase64String(b64)
      MS = New System.IO.MemoryStream(b)
  
      'creates image
      img = System.Drawing.Image.FromStream(MS)
  
      Return img
  End Function
  </code></pre>
  
  <p>I then open a popup window and all I want to do is display this image in the popup window.  The examples I have found rely on using the <strong>PaintEventArgs</strong>, but I'm not sure how that works and it doesn't seem relevant in this case.  The best I've been able to do is to get the image to display on the screen, but it's not actually attached to the popup window.  I did that using the following code, which is a method inside the popup window class:</p>
  
  <pre><code>Dim img as System.Drawing.Image = Base64ToImage(base64string)
  Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromHwnd(New Interop.WindowInteropHelper(Me).Handle)
  gr.DrawImage(img, 10, 10, 500, 800)
  
  gr.Dispose()
  </code></pre>
  
  <p>This displayed the image, but it seemed to appear at position 10,10 of the screen, not of the popup window.</p>