?
Solved

Display image on web page directly from byte array.

Posted on 2009-05-04
9
Medium Priority
?
1,308 Views
Last Modified: 2012-06-21
I have successfully managed to upload images into an image field in SQL Server 2005.  I can retrieve those images in Byte array format.  I want to be able to display that image file as an image in the web page without going to file first.  In other words, stream it directly down to the browser and display the image in an <img> tag or <input type="image" or any other tags that takes some kind of src="" parameter.  So far, the only thing I have been able to conceive is making an ASPX page that reads the database, modifies the MIME type, and streams down the information that way.  Is this the best way to do something like this or is there something better?  I am using ASP.NET 3.5 and SQL Server 2005.
0
Comment
Question by:j_s_kelley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:Cebik
ID: 24297402
In windows form i think it will be looks like this..

Stream stream = new MemoryStream(bitmapInByteArray);
Image image = Image.FromStream(stream);

Open in new window

0
 
LVL 6

Expert Comment

by:RPCIT
ID: 24297409
I think what your doing would be against the "rules" of common browsers.  To do it your way I think you would have to install some kind of active-x control on the client (probably not what you want).  In essence..  HTML really hasn't changed that much on the browser side.. you have to have a file on the server.. and the browser can read HTML to download that file and display it in the browser.

I'm thinking that you don't want to go through a file because of managing the temp files.  What I have done is create my files with the session ID as part of the name (so it will be unique per session) and then I just clean out all the old sessions periodically (every 10 times a session is created or something)
0
 
LVL 4

Author Comment

by:j_s_kelley
ID: 24297425
Thanks for the info but as the question refers to I am using ASP.NET 3.5.  This is a web page, not a windows form.  This will eventually become an <img> tag or an <input Type="image> tag.  I need the SRC for this.  Normally, it is a file.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:j_s_kelley
ID: 24297456
To RPCIT, I did want to get away from temp files which is why I put the info in the DB to begin with.  I would also like to apply this technique to pages which may pull up many small images (say a catalog).  This would be a nightmare of file access on the server.  I also have other security considerations imposed by the network admins (I know, I know...)
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 24297466
I have a similar issue with sending PDF's, and I found it not a problem to use a separate ASPX to send the content by modifying the headers. However, I still use an intermediary file, but at first glance at docs it looks like you can use Response.BinaryWrite(byte[]).


Response.ClearHeaders();
Response.ContentType = "image/jpeg";
Response.Clear();
Reponse.BinaryWrite(imageBytes);
Response.BufferOutput = true;

Open in new window

0
 
LVL 4

Author Closing Comment

by:j_s_kelley
ID: 31577653
Thanks.  This is the way I am going to do it.  I removes the need for the temp file.  Plus, you have some extra code in there to make the transfer cleaner so I am going to modify my page to do what you are doing.  Thanks.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 24297573
This little test worked for me...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<img src="GetPicture.aspx?SomePicID" />
</body>
</html>
public partial class GetPicture : System.Web.UI.Page
{
	private byte[] GetImage(string ImageId)
	{
		byte[] buffer;
		FileStream fs = File.Open(Server.MapPath("~/test.gif"), FileMode.Open);
		buffer = new byte[fs.Length];
		fs.Read(buffer, 0, (int)fs.Length);
		fs.Close();
		return buffer;
	}
 
    protected void Page_Load(object sender, EventArgs e)
    {
		byte[] imageBytes = GetImage(Request.QueryString["ImageID"]);
 
		Response.ClearHeaders();
		Response.ContentType = "image/gif";
 
		Response.Clear();
 
		Response.BinaryWrite(imageBytes);
    }
}

Open in new window

0
 
LVL 6

Expert Comment

by:RPCIT
ID: 24297579
I like that tgerbert... I will have to play with it
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 24297592
Of course, you'd need to replace the GetImage() function with your code that retrieves the bytes from the database! ;)

Also, you can refer to <img src="/yourpage.aspx?PicID=PictureDBId"> to re-use the same ASPX for all your images.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question