Click here to Skip to main content
15,892,643 members
Articles / Web Development / ASP.NET

Binary streaming of large images from Database

Rate me:
Please Sign up or sign in to vote.
2.54/5 (13 votes)
29 Jun 20042 min read 166K   2.1K   44  
Explains how to perform drawing on a large image retrieved from the database, and then stream the image with the "Open/Save" option.
' This file contains just two functions
' 1. GetImageFromDB
' 2. DownloadImage
'
' Place these 2 functions inside ur vb file in ur asp.net application.
' Please make appropriate changes to suit ur application needs

    ' This function returns the image retrieved from the database
    Private Function GetImageFromDB(ByVal ImageID As Integer) As Image

        ' Get the Connection string
        Dim strConnection As String
        strConnection = "Specify Connection string here"

        Dim conn As New SqlConnection(strConnection)
        Dim sqlCommand As String
        Dim strImage As String
        Dim image As Image

        Try
            sqlCommand = "SELECT Image FROM ImageTable WHERE ImageID = " + ImageID.ToString

            Dim cmd As New SqlCommand(sqlCommand)
            cmd.Connection = conn
            conn.Open()
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            While (dr.Read())

                Dim byt As Byte()
                byt = dr.Item(strImage)
                Dim bmp As New Bitmap(New System.IO.MemoryStream(byt))
                image = bmp

            End While
        Catch ex As Exception
            ' Log Error
            Response.Write(ex.Message)
        Finally
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Try

        Return image

    End Function


    ' Download the image on to the client
    Public Sub DownloadImage(ByVal ImageID As Integer)

        Dim image As Image
        Dim bmpNew As Bitmap
        Dim imageNew As Image
        Dim objGraphics As Graphics

        Dim objFont As Font = New Font("Verdana", 12, FontStyle.Bold)
        Dim objbrush As Brush = Brushes.Black

        Try

            ' Get the Image from DB
            image = GetImageFromDB(ImageID)


            Try
                objGraphics = Graphics.FromImage(image)
            Catch e As Exception
                ' The image is in indexed pixel format
                ' Create a temp bitmap

                bmpNew = New Bitmap(image.Width, image.Height)
                imageNew = bmpNew

                objGraphics = Graphics.FromImage(imageNew)

                '' Draw the contents of old bitmap to new bitmap	
                objGraphics.DrawImage(image, New Rectangle(0, 0, imageNew.Width, imageNew.Height), _
                                        0, 0, image.Width, image.Height, GraphicsUnit.Pixel)

                image = imageNew

            End Try

            ' writing a simple Text over the image
            Dim rect As New Drawing.RectangleF(10, 10, 200, 50)
            objGraphics.DrawString("Simple Text", objFont, objbrush, rect)

            ' Set the content type and return the image
            Response.ContentType = "image/jpeg"
            'image.Save(Response.OutputStream, ImageFormat.Jpeg)

            Dim ms As MemoryStream = New MemoryStream()
            image.Save(ms, Imaging.ImageFormat.Jpeg)
            Dim bytImage(ms.Length) As Byte
            bytImage = ms.ToArray()
            ms.Close()
            
            ms = New MemoryStream(bytImage)
            Response.Clear()
            Response.AddHeader("Content-Type", "binary/octet-stream")
            Response.AddHeader("Content-Length", bytImage.Length.ToString())
            Response.AddHeader("Content-Disposition", "attachment; filename= DownloadedImage.jpg; size=" + bytImage.Length.ToString())
            Response.Flush()


            Dim chunkSize As Integer = 1024

            Dim i As Integer
            For i = 0 To bytImage.Length Step chunkSize
                If (Not Response.IsClientConnected) Then
                    Exit For
                End If

                Dim size As Integer = chunkSize
                If (i + chunkSize >= bytImage.Length) Then
                    size = (bytImage.Length - i)
                End If

                Dim chunk(size - 1) As Byte
                ms.Read(chunk, 0, size)
                Response.BinaryWrite(chunk)
                Response.Flush()
            Next
            ms.Close()
            bytImage.Clear(bytImage, 0, bytImage.Length)

        Catch ex As Exception
            ' Log exception
            Response.Write(ex.Message)
        Finally

            ' Kill the objects
            bmpNew.Dispose()
            imageNew.Dispose()
            image.Dispose()
            objGraphics.Dispose()

        End Try

    End Sub

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions