Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old December 29th, 2006, 07:47 AM
Zath's Avatar
Zath Zath is offline
Gratefully Deadicated
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Location: The Great Machine
Posts: 473 Zath User rank is First Lieutenant (10000 - 20000 Reputation Level)Zath User rank is First Lieutenant (10000 - 20000 Reputation Level)Zath User rank is First Lieutenant (10000 - 20000 Reputation Level)Zath User rank is First Lieutenant (10000 - 20000 Reputation Level)Zath User rank is First Lieutenant (10000 - 20000 Reputation Level)Zath User rank is First Lieutenant (10000 - 20000 Reputation Level)Zath User rank is First Lieutenant (10000 - 20000 Reputation Level)Zath User rank is First Lieutenant (10000 - 20000 Reputation Level)  Folding Points: 52633 Folding Title: Beginner FolderFolding Points: 52633 Folding Title: Beginner FolderFolding Points: 52633 Folding Title: Beginner Folder
Time spent in forums: 5 Days 18 h 14 m 12 sec
Reputation Power: 196
HttpHandler for Images

This sample uses an httpHandler to retrieve an image stored in a sql server database and shows the image on your webform.

The handler I call dataImgHandler:

Code:
<%@ WebHandler Language="VB" Class="dataImgHandler" %>

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.data

Public Class dataImgHandler : Implements IHttpHandler
       
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            Dim myImage As Image = GetBitmap(context)
            context.Response.ContentType = "image/jpeg"
            myImage.Save(context.Response.OutputStream, ImageFormat.Jpeg)
        Catch
            'If the image field in the database is null, it throws exception
            'So, pass the path to the transparent.gif image
            
            Try
                Dim req As HttpRequest = context.Request
                Dim path As String = req.PhysicalPath
                If ((Not (req.UrlReferrer) Is Nothing) AndAlso (req.UrlReferrer.Host.Length > 0)) Then
                    'load a transparent gif 
                    path = context.Server.MapPath("~/images/transparent.gif")
                End If
                Dim contentType As String = Nothing
                contentType = "image/gif"
                context.Response.StatusCode = 200
                context.Response.ContentType = contentType
                context.Response.WriteFile(path)
            Catch
            
            End Try
            
        End Try
    End Sub
    
    Private Function GetBitmap(ByVal context As HttpContext) As Bitmap
        Dim oBitmap As Bitmap = Nothing

        'pass the database guid to get the image row
        Dim guid As String = System.Web.HttpContext.Current.Request.QueryString  ("guid")
        
        'get the byte data from the image field
        Dim myByteImg = 'get data from database here - select imgField1 from mytable where guid = guid
        
         Dim bytPhotoData() As Byte = myByteImg 
         Dim stmPhotoData As New MemoryStream(bytPhotoData)
          
         oBitmap = Image.FromStream(stmPhotoData)
         Return oBitmap  
              
    End Function

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

End Class


And, to get the image into an image control from the code behind aspx.vb page:
Code:
        Image1.ImageUrl = "dataImgHandler.ashx?guid=" & guid


I pass a varaible, guid, to let me know what image I need from the database but could have easily used a session.

Happy Coding!

Zath
Comments on this post
lewy agrees!

Last edited by Zath : December 29th, 2006 at 08:38 AM. Reason: Showed more code to stream image

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > HttpHandler for Images


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway