Images from SQL Server

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Images from SQL Server



    Hi

    I am trying to display in an aspx page an image from the employees table in
    the sql server 2000 Northhwind database



    "Select photo from employees where employeeid = 1"





    This is as far a I got


    ================================================== ==========================
    =================

    Dim myConnection As New SqlConnection(Constants.SQLConnectionString)

    Dim myCommand As New SqlCommand("Select photo from Employees where
    EmployeeID =1" , myConnection)



    Dim bytes() As Byte



    Try

    myConnection.Open()

    Dim dr As SqlDataReader

    dr = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)



    Do While (dr.Read())



    'Response.BinaryWrite(bytes()) myDataReader.Item("Photo"))

    bytes = dr("photo")





    Loop



    myConnection.Close()

    Response.Write("Person info successfully retrieved!")

    Catch SQLexc As SqlException

    lblError.Text = SQLexc.Message

    end try




    ================================================== ==========================
    =================

    I am at a loss with what to do with the byte array next.

    All I could think of was write it to a file then reference that as the URL
    for an image control

    But I am sure there is a better way

    Any help regarding this would be great



    Cheers

    James










    James Lang Guest

  2. Similar Questions and Discussions

    1. store/retrieve images to server
      Hi All, I want to retrieve images from server and send them back to Servlet without using FileUpload. It's an urgent requirement please help me out....
    2. condenseWhite & Server resized images
      Hi, I was finally able to get my RSS feed via PHP and AS3 to display everything in my Flash TextArea. I need to use condenseWhite set to "true" to...
    3. Upload images into Sql Server through CFML
      Does CFML have the capability to upload image files into SQL server?i just give the imae file name and it should upload the image into the SQL...
    4. Server control that renders images
      On Fri, 23 Apr 2004 15:35:36 -0400, Stephen Walch wrote: you can use TStreamImage for image rendering, or look at it's source (Delphi.NET)....
    5. Saving Images within a Server Control
      I would like to save images within the development of a control so it would be displayed in certain tags without the developer having to point to the...
  3. #2

    Default Re: Images from SQL Server

    James Lang wrote:
    > Hi
    >
    > I am trying to display in an aspx page an image from the employees
    > table in the sql server 2000 Northwind database
    >
    > "Select photo from employees where employeeid = 1"
    >
    > This is as far a I got
    >
    <cut>
    >
    > I am at a loss with what to do with the byte array next.
    >
    > All I could think of was write it to a file then reference that as
    > the URL for an image control
    >
    > But I am sure there is a better way
    >
    > Any help regarding this would be great
    You're almost there.

    I removed the bytes array from your code, and I brought the
    Response.BinaryWrite back in. You also need to set the
    Response.ContentType.

    ************************************************** ***************
    <%@ Page Language="VB" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.OleDb" %>
    <script runat="server">
    Public Sub Page_Load(sender As Object, e As EventArgs)
    Dim myConnection As New SqlConnection(Constants.SQLConnectionString)
    Dim myCommand As New SqlCommand("Select photo from Employees where
    EmployeeID =1" , myConnection)
    Try
    myConnection.Open()
    Dim dr As SqlDataReader
    dr =
    myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
    Do While (dr.Read())
    Response.ContentType = "image/jpg"
    Response.BinaryWrite(myDataReader.Item("Photo"))
    Loop
    myConnection.Close()
    Catch SQLexc As SqlException
    lblError.Text = SQLexc.Message
    end try
    End Sub
    </script>
    ************************************************** ****************

    Save this code in a separate file, let's say "photo.aspx".
    Note that the file doesn't contain any HTML tags, only code.
    This file will now act as a jpg or gif image.

    Therefore, just reference this file from your main file:
    <img src="photo.aspx">

    Later you can extend this to something like:
    <img src="photo.aspx?id=5">

    --

    Jos Branders


    Jos Guest

  4. #3

    Default Re: Images from SQL Server

    A Big Thanks to you Jos

    I had lots of problems getting this to work but as it turns out there is a
    78bit header to the bitmap images in the employees table

    Once I stripped that of it worked great although it was trial and error for
    about 4 hours. ( Creating my own table, then adding images to it worked
    fine so nothing wrong with the code logic but the Northwind images still did
    not work so I created an new windows app and wrote the images to a file
    "test.bmp" but they would not open. Light at the end of the tunnel time I
    thought there must be something wrong with the binary stream. Opening other
    bitmap files and viewing there binary contents indicated there was
    additional bytes at the start of the stream. I removed them an hay presto I
    had an image

    Using and ASPX page as the source of the image was a something I never
    thought of and now I know the technique I can use it for all sorts of stuff

    Here is the code I used if anyone else wants to play with the Northwind DB

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim id As Integer = Request.QueryString("ID")
    Dim myConnection As New SqlConnection(Constants.SQLConnectionString)
    Dim myCommand As New SqlCommand("Select photo from Employees where
    EmployeeID =" & ID, myConnection)
    Try
    myConnection.Open()
    Dim dr As SqlDataReader
    dr = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
    Do While (dr.Read())
    bytes = dr("photo")
    Dim i As Integer
    Dim x(bytes.GetUpperBound(0) - 78) As Byte

    For i = 78 To bytes.GetUpperBound(0)
    x(i - 78) = (bytes(i))
    Next i
    Response.BinaryWrite(x)
    Loop


    myConnection.Close()
    Catch SQLexc As SqlException
    lblError.Text = SQLexc.Message
    End Try

    End Sub

    Once again thanks Jos

    Cheers
    James

    "Jos" <josnospambranders@fastmail.fm> wrote in message
    news:uBf3GDfRDHA.3192@TK2MSFTNGP10.phx.gbl...
    > James Lang wrote:
    > > Hi
    > >
    > > I am trying to display in an aspx page an image from the employees
    > > table in the sql server 2000 Northwind database
    > >
    > > "Select photo from employees where employeeid = 1"
    > >
    > > This is as far a I got
    > >
    > <cut>
    > >
    > > I am at a loss with what to do with the byte array next.
    > >
    > > All I could think of was write it to a file then reference that as
    > > the URL for an image control
    > >
    > > But I am sure there is a better way
    > >
    > > Any help regarding this would be great
    >
    > You're almost there.
    >
    > I removed the bytes array from your code, and I brought the
    > Response.BinaryWrite back in. You also need to set the
    > Response.ContentType.
    >
    > ************************************************** ***************
    > <%@ Page Language="VB" %>
    > <%@ import Namespace="System.Data" %>
    > <%@ import Namespace="System.Data.OleDb" %>
    > <script runat="server">
    > Public Sub Page_Load(sender As Object, e As EventArgs)
    > Dim myConnection As New SqlConnection(Constants.SQLConnectionString)
    > Dim myCommand As New SqlCommand("Select photo from Employees where
    > EmployeeID =1" , myConnection)
    > Try
    > myConnection.Open()
    > Dim dr As SqlDataReader
    > dr =
    > myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
    > Do While (dr.Read())
    > Response.ContentType = "image/jpg"
    > Response.BinaryWrite(myDataReader.Item("Photo"))
    > Loop
    > myConnection.Close()
    > Catch SQLexc As SqlException
    > lblError.Text = SQLexc.Message
    > end try
    > End Sub
    > </script>
    > ************************************************** ****************
    >
    > Save this code in a separate file, let's say "photo.aspx".
    > Note that the file doesn't contain any HTML tags, only code.
    > This file will now act as a jpg or gif image.
    >
    > Therefore, just reference this file from your main file:
    > <img src="photo.aspx">
    >
    > Later you can extend this to something like:
    > <img src="photo.aspx?id=5">
    >
    > --
    >
    > Jos Branders
    >
    >

    James Lang Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139