title: if you have a DLL creating a bitmap in memory, how to return it to the browser?
body: <p>I have a DLL that is going to return an object(bitmap) and I have to pass it to the browser.</p>
  
  <p>All in memory, no disk access.</p>
  
  <p>I know how to do it with asp.net webform but I have no clue with MVC.</p>
  
  <p>with webform,</p>
  
  <p>since I have control over the dll, I inherit the class with the webcontrol.image.</p>
  
  <p>in the aspx page I create a simple img link like:</p>
  
  <pre><code>   &lt;img src="Handler1.ashx&gt;
  </code></pre>
  
  <p>in the Handler1.ashx I have code like:</p>
  
  <pre><code>Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
      context.Response.Clear()
      Dim bmp As Bitmap = Nothing
      Dim dll As New myDll.Class
  
      dll.drawPicture(bmp)
  
      context.Response.ContentType = "image/jpeg"
      bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg)
      context.Response.End()
  End Sub
  </code></pre>
  
  <p>(I removed the cleaning stuff/useless code to keep it short)</p>
  
  <p><strong><em>edit</em></strong></p>
  
  <p>solution is</p>
  
  <pre><code>Function img() As FileResult
      Dim bmp As Bitmap = Nothing
      Dim dll As New myDll.myClass
      dll.DrawPicture(bmp)
  
      Dim imgStream As New IO.MemoryStream
      bmp.Save(imgStream, ImageFormat.Png)
      imgStream.Position = 0
  
      bmp.Dispose()
      dll.Dispose()
      bmp = Nothing
      dll = Nothing
  
      Return File(imgStream.ToArray, "image/png")
  End Function
  </code></pre>
  
  <p>I have no problem running cleaning code on everything except the memorystream, is there a chance for a memoryleak there? (I think yes)</p>