Search code examples
asp.netvb.netimagearrayscodeplex

Displaying images derived from a byte-array dynamically on a page


I'm calling a webservice that is returning an unknown quantity of images in the form of an a collection of byte arrays. (I can't change this)

I need to display each image on a single aspx webpage.

I'm currently using the Microsoft.Web.GeneratedImage control; http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449 to display the images.

The issue i have is that since the control calls a seperate code file to load the image content, i am using session state to store the bytearray, which i'm not overly happy about.

Here's some code from my test project;

     Private Sub btnGetChart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetChart.Click
    Dim reportHub As New HubWrapper

    Dim repCharts() As ReportHub.Chart = reportHub.ReportHubChart(Me.ddlReports.SelectedValue, ViewState("params"))

    For Each chart As ReportHub.Chart In repCharts
      Dim sessionKey As String = "img" & System.Guid.NewGuid().ToString

      Dim imgParam As New Microsoft.Web.ImageParameter()
      imgParam.Name = "sessionVar"
      imgParam.Value = sessionKey

      Session(sessionKey) = chart.ChartData

      Dim img As New Microsoft.Web.GeneratedImage
      img.ImageHandlerUrl = "~/chartImageHandler.ashx"
      img.Parameters.Add(imgParam)

      phChart.Controls.Add(img)
    Next
  End Sub

<%@ WebHandler Language="VB" Class="chartImageHandler" %>

Imports System
Imports System.Collections.Specialized
Imports System.Drawing
Imports System.Web
Imports Microsoft.Web

Public Class chartImageHandler
  Inherits ImageHandler

  Implements IRequiresSessionState

  Public Sub New()
    MyBase.New()
    'Set caching settings and add image transformations here
    'EnableServerCache = True

  End Sub

  Public Overrides Function GenerateImage(ByVal parameters As NameValueCollection) As ImageInfo
    Dim byteArry As Byte() = CType(HttpContext.Current.Session(parameters("sessionVar")), Byte())

    HttpContext.Current.Session.Remove(parameters("sessionVar"))

    Return New ImageInfo(byteArry)

  End Function
End Class

What's the most elegant way of achieving this?

Any input welcome!!!

EDIT: Additional info;

  • The images from the webservice are coming from SQL Reporting Services.
  • The images would generally be changing all the time (no real caching requirement).
  • Images would be user specific.
  • The webservice is only called once per page.
  • I'm actually disposing of the image from session state after the generated image uses it, as the image won't be needed to be viewed again for a while.

Solution

  • You can embed the image data directly into the page source using the data: URI scheme. Here's the Wikipedia description. That doesn't work with IE versions prior to 8 though, which may make it unacceptable for your situation.