0

I have an .aspx page (asp.net) with the following code:

    <%@ Page ContentType = "image/gif"%>
    <%@ Import Namespace = "System.Drawing" %>
    <%@ Import Namespace = "System.Drawing.Imaging" %>

    <Script Runat = "Server">

    Sub Page_Load
      Dim objBitmap As Bitmap
      Dim objGraphics As Graphics
      objBitmap = New Bitmap(200, 200)
      objGraphics = Graphics.FromImage(objBitmap)
      objGraphics.DrawLine(new Pen(Color.Red), 0, 0, 200, 200)
      objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
      objBitmap.Dispose()
      objGraphics.Dispose()
    End Sub

    </Script>

But what shows up on the page is junk text - strange characters, as follows:

GIF89a���3f���++3+f+�+�+�UU3UfU�U�U���3�f��������3�f��������3�fՙ������3�f{��a����q�4��w����k����[�������ѻ�������럿� �<�� !lQ@;

How do I get the image to show up correctly? (eventually, I want to place the image within a table cell)

swabygw
  • 813
  • 1
  • 10
  • 22
  • You do not need the extensiveness of a an .aspx form to serve an image: an .ashx handler would be more appropriate. For some context: [Display Image using ashx Handler](http://stackoverflow.com/questions/8733875/display-image-using-ashx-handler). – Andrew Morton Mar 13 '15 at 23:03
  • Your problem is you are sending data to the browser but you haven't told it what type of data. What you are getting isn't junk or strange characters. You're getting the browser interpreting the bytes you sent it. Because you failed to tell it this is a gif image, you get this. Set the Response.ContentType = "image/gif" Info at: https://msdn.microsoft.com/en-us/library/system.web.httpresponse.contenttype%28v=vs.110%29.aspx – Mark Fitzpatrick Mar 14 '15 at 01:56

1 Answers1

0

Using an .aspx page is quite expensive in terms of processor usage - the "page lifecycle" involves several events, such as Page_Load - compared to what you need, which is just sending a Content-Type (and just a few other headers, of course) and the data.

If you used an .aspx page, you would have to clear the headers which have already been generated for you otherwise the browser would be told to receive something like "text/html".

As a mock-up, I made a handler "GetImage.ashx" with this code:

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Web
Imports System.Web.Services

Public Class Handler1
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        context.Response.ContentType = "image/png" ' set correct MIME type here '
        Using objBitmap As Bitmap = New Bitmap(200, 200)
            Using objGraphics As Graphics = Graphics.FromImage(objBitmap)
                objGraphics.DrawLine(New Pen(Color.Red), 0, 0, 200, 200)
                objBitmap.Save(context.Response.OutputStream, ImageFormat.Png)
            End Using
        End Using

    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

End Class

which generates the image in a browser as you intended.

Simply use a URL for the image in the fashion of <img src="http://127.0.0.1/webtest/getimage.ashx" alt="">.

For a more up-to-date way, with plenty of explanation, please see Back to Basics: Dynamic Image Generation, ASP.NET Controllers, Routing, IHttpHandlers, and runAllManagedModulesForAllRequests.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Thanks - I figured it had something to do with the headers. This solution definitely creates the image, but my ultimate goal was to place it in a TableCell. I'm a bit of a novice with HttpHandlers, but how can I turn it into an object and code something like: myTblCell.Controls.Add(myImage), without fiddling with the web.config file (where myImage is the graphic drawing)? – swabygw Mar 14 '15 at 03:19
  • @swabygw All you need to do is use the URL of the handler in the same way as you put in a URL to the image, so that the rendered HTML ends up like ``. – Andrew Morton Mar 14 '15 at 10:22