I have already received the byte [] of a jpeg image but it won't display. I am using asp.net 2.0. I was wondering what the correct way of displaying an image from a database is (I have seen the asp:DynamicImage but when I try to use it, on the build it says it is an unknown server control). Here is my code:
File: DisplayImage.aspx
//Here is the code that I use to call from the control <asp:Image id="eventImage" runat="server"/>
eventImage.ImageUrl = "~/User/Image.asp ImagePath=" + evnt.ImagePath;
File: Image.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="CollisonDatabase" %>
<%
Response.Expires = 0;
Response.Buffer = true;
Response.Clear();
string imagePath = Request.QueryString.Get("ImagePath");
CollisonDatabase.Database db = new CollisonDatabase.Database();
byte[] bytes = db.GetImage(imagePath);
Response.ContentType = "Image/JPEG";
Response.BinaryWrite(bytes);
Response.End();
%>

How to Display Image From Byte
VbArch
and an answer to the question
Jeff G87474
MavecO
Ok I changed it to this and only the one I tried to view gave that exception because, It really wasn't in the db, but the others don't display the or give the null exception image.
<%
@ Page Language="C#" %><%
@ Import Namespace="CollisonDatabase" %><%
Response.Expires = 0;
Response.Buffer =
true;Response.Clear();
string imagePath = Request.QueryString.Get("ImagePath");CollisonDatabase.
Database db = new CollisonDatabase.Database(); byte[] bytes = db.GetImage(imagePath); if (bytes != null){
Response.ContentType =
"image/jpeg";Response.BinaryWrite(bytes);
Response.End();
}
%>
Simeon Antonov
no that size is correct heh ... it still holds a variable amount of data.
Greg
BobN
have you verified sizes etc being correct between the data in the db and the actual files (i.e. that the db based data is actually valid (and they are actually in JPEG format )
gedmac
JosephDev
well just one quick thing I noticed that could be causing you problems ..
"~/User/Image.asp ImagePath=" + evnt.ImagePath
your image page is not image.asp but image.aspx (according to the above code sample).
also what happens if you go the image link directly (i.e. copy/paste picture link into the browser .. do you get an exception )
Cheers,
Greg
bigrockshow
I would try the following just to test things out ... write a test function to insert a known good image (i.e. the google image etc) from a file ... then look at the data in the db to make sure it matches up ... if it does then try to display it.
I am not following the fact that you use "Image Size 16" perhaps you could post the code in question.
Greg
Matt ms user
Fred Malone
Samster
ok ...
what happens a red X
How are you storing the data in the db Are you doing anything odd like base63 encoding it that you may not be doing on the way out
msauper
And it is displaying a red x
Have you verified that the data in the db is valid data in jpeg form I have code almost identical to this up and running without issue.
Delphi
Grigory Bushuev
File:AddImage.aspx
//inside is this code
//path = "The Path that I want to call it";
addImage(path, FileUpload1.FileBytes );
File: Database.cs
addImage(
string imagePath, byte[] imgBytes){
try{
if (imagePath != "NONE"){
SqlConnection conn = new SqlConnection();activateConnection(
ref conn); SqlCommand cmd = new SqlCommand();cmd.CommandType = System.Data.
CommandType.StoredProcedure;cmd.CommandText =
"carterwjeff.addImage";cmd.Connection = conn;
cmd.Parameters.AddWithValue(
"@imagepath", imagePath);cmd.Parameters.AddWithValue(
"@imgbyte", imgBytes); if (Convert.ToInt32(cmd.ExecuteNonQuery()) > 0){
_result +=
" Image Successfully Created."; return true;}
else{
_result +=
" Image was not successfully created."; return false;}