tags:

views:

185

answers:

2

I have a DLL that is going to return an object(bitmap) and I have to pass it to the browser.

All in memory, no disk access.

I know how to do it with asp.net webform but I have no clue with MVC.

with webform,

since I have control over the dll, I inherit the class with the webcontrol.image.

in the aspx page I create a simple img link like:

   <img src="Handler1.ashx>

in the Handler1.ashx I have code like:

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

(I removed the cleaning stuff/useless code to keep it short)

edit

solution is

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

I have no problem running cleaning code on everything except the memorystream, is there a chance for a memoryleak there? (I think yes)

A: 

Use a generic handler and save the bitmap (.Save) with Response.OutputStream

ScottE
Looks like you've already done that!
ScottE
So it would be the same as webform?
Fredou
+3  A: 

Use controller action which returns FileResult with Controller.File() method.

Alexander Prokofyev
very nice solution: gives you a controller method you can unit test.
russau
still have a question about memory leak, if you know
Fredou