Loading an image into an Image control

  • Thread starter Thread starter Jake
  • Start date Start date
J

Jake

I am a beginner in C# and web controls. I need a way to load an image
which is stored in an Access (mdb) field into an Image control added
at runtime. I get the image (jpg format) dynamically at runtime then
I create an Image control (from WebControls collection) and add it to
the page also at runtime. The ImageUrl property needs a URL string
but I only have the image itself extracted with a data RecordSet
(recordset["picturefield"] for example.) Can someone please help?
 
Jake said:
I am a beginner in C# and web controls. I need a way to load an image
which is stored in an Access (mdb) field into an Image control added
at runtime. I get the image (jpg format) dynamically at runtime then
I create an Image control (from WebControls collection) and add it to
the page also at runtime. The ImageUrl property needs a URL string
but I only have the image itself extracted with a data RecordSet
(recordset["picturefield"] for example.) Can someone please help?

This works for me in VB. I hope you can figure it out in C#.

Create a file called "image.aspx" with this code:
----------------------------------------------------------
<%@ 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 strConn As String = "Provider=Microsoft.OLEDB.Jet.4.0" & _
Page.Server.MapPath("database\db1.mdb")
Dim strImageID as String = Request.QueryString("id")

Dim cn As New OleDbConnection(strConn)
Dim strSQL As String ="Select * from myTable Where ID=" & strImageID
Dim myCommand As New OleDbCommand(strSQL, cn)
Try
cn.Open()
Dim dr as OleDbDataReader
dr = myCommand.ExecuteReader()
If (dr.Read())
If(Not TypeOf dr.Item("Image") Is DBNull) Then
Response.ContentType = "image/jpg"
Response.BinaryWrite(dr.Item("Image"))
Else
cn.Close()
Server.Transfer("images/noimage.gif")
End If
End If
Catch exc As OleDbException
Trace.Write(exc.Message)
Finally
cn.Close()
End Try
End Sub

</script>
 
Thank you Jos. It works. But I have one more complication. The image
I am loading is in the page_load event of the same webform where I need
the image control to be.

webform_1 page_load()
{
...
/* I receive the image from the database here */
img = recordset["picture_field"]
...
/* if I use Response.BinaryWrite() here, it will
* direct the stream into this webform (webform_1) */
...
/* the image needs to go into an Image control here */
}

How can I re-direct Response.BinaryWrite() stream to another webform
(webform_2) so I can then set the Image control url to webform_2?
 
Jake said:
Thank you Jos. It works. But I have one more complication. The image
I am loading is in the page_load event of the same webform where I need
the image control to be.

webform_1 page_load()
{
...
/* I receive the image from the database here */
img = recordset["picture_field"]
...
/* if I use Response.BinaryWrite() here, it will
* direct the stream into this webform (webform_1) */
...
/* the image needs to go into an Image control here */
}

How can I re-direct Response.BinaryWrite() stream to another webform
(webform_2) so I can then set the Image control url to webform_2?

Hello Jake,

This is not possible. At that point webform_2 is not instantiated
yet on the server, because the web browser will only send the
request for the image to webform_2 after it receives the
response from webform_1. You _could_ use the Session object
from webform_1 to temporarily store the image data and then
get it from there in webform_2, but this is rather dirty and
should only be used if it's necessary for performance reasons.
Also this trick can only be used if the image isn't too big.

My preference would be to just let webform_1 do nothing
with the image field and just let it set the URL for the
image to webform_2 and let webform_2 do a query to the
database again to get the image.

Best regards,

Eric
 
Back
Top