RSS Forums RSS
Please support our C# advertiser: Programming Forums
Views: 7905 | Replies: 7
Reply
Join Date: Mar 2005
Location: Ottawa, Ontario, Canada
Posts: 959
Reputation: belama is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
belama's Avatar
belama belama is offline Offline
Posting Shark

Download image from DB and view it...

  #1  
Jun 6th, 2005
Is there a way to download an image (binary) from a database in sql server and insert it on a page as an image control? I want to add the image to a page when it loads. I can't figure how to do it and I am not sure if it is possible to do.

thx
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2003
Location: Bamberg, Germany
Posts: 117
Reputation: Iron_Cross is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 2
Iron_Cross's Avatar
Iron_Cross Iron_Cross is offline Offline
Junior Poster

Re: Download image from DB and view it...

  #2  
Jun 14th, 2005
I've never actually done that, but I'm guessing you're going to have to create some sort of BinaryStream then create a file out of that, then use the BitMap.LoadFromFile() method to load the picture....I'm not actualy sure though.
elitehackers.info
Today's Penny-Arcade!
Pain is weakness leaving the body!
Reply With Quote  
Join Date: Mar 2005
Location: Ottawa, Ontario, Canada
Posts: 959
Reputation: belama is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
belama's Avatar
belama belama is offline Offline
Posting Shark

Re: Download image from DB and view it...

  #3  
Jun 14th, 2005
I found a way to do it. I uploaded all the thousands of images to the DB. Now to show them up, I download the one I need to the server, then just show it on the page. It's fine like this but if anybody would know if it is possible to show the image on a page without copying the file to a drive, it would be nice... thx
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Download image from DB and view it...

  #4  
Jun 14th, 2005
You set the image into the HTTP response stream, setting the proper MIME type, of course. Do an internet search for "image response stream".

This question might have been answered sooner if it were posted in the Web Development | ASP.NET forum.
Reply With Quote  
Join Date: Mar 2005
Location: Ottawa, Ontario, Canada
Posts: 959
Reputation: belama is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
belama's Avatar
belama belama is offline Offline
Posting Shark

Re: Download image from DB and view it...

  #5  
Jun 14th, 2005
Originally Posted by tgreer
You set the image into the HTTP response stream, setting the proper MIME type, of course. Do an internet search for "image response stream".

This question might have been answered sooner if it were posted in the Web Development | ASP.NET forum.
I tried something like that but if you are talking about what I think you are, only the image would be showing in the browser and not the image in a page if you know what I mean. I want to be able to generate the rest of the page not just the image.

Youre right I should've posted this in the .NET forum.
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Download image from DB and view it...

  #6  
Jun 14th, 2005
No, the technique is to create a webform/aspx application that retrieves an image from the database, and streams it out.

Then, the image CONSUMER application would have an img tag. The "src" attribute is set to the image PRODUCER application.

So if you a red and a blue image, you might have a src as such:

<img src=:http://www.belama.org/imageSelect.aspx?img=red">

The imageSelect.aspx would use the querystring to retrieve the "red" image from the database and stream it back.
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Download image from DB and view it...

  #7  
Jun 14th, 2005
Something like this, in the code-behind. This is code that creates an in-memory graphic, but you could just as well retrieve the image from a database:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace charts
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Image Image1;

private void Page_Load(object sender, System.EventArgs e)
{
DisplayLine();
Image1.ImageUrl = Server.MapPath("")+"temp.jpg";

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DisplayLine()
{
Bitmap b =new Bitmap(250,250);
b.Save(Server.MapPath("")+"temp.jpg",ImageFormat.Jpeg);
Graphics g = Graphics.FromImage(b);
g.Clear(Color.White);
Pen p = new Pen(Color.Blue);
g.DrawLine(p,10,240,240,240);
b.Save(Server.MapPath("")+"temp.jpg",ImageFormat.Jpeg);
}
}
}

That code SAVES the image. To SERVE the image, instead, you could use:

Response.ContentType = "image/jpeg";
b.Save(Response.OutputStream, ImageFormat.Jpeg);

Then to use this code inside of another HTML and/or WebForm page, set the img src to the image producing page.
Reply With Quote  
Join Date: Mar 2005
Location: Ottawa, Ontario, Canada
Posts: 959
Reputation: belama is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
belama's Avatar
belama belama is offline Offline
Posting Shark

Re: Download image from DB and view it...

  #8  
Jun 15th, 2005
Thanks I'll check it out later.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:28 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC