943,519 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 10792
  • C# RSS
Jun 6th, 2005
0

Download image from DB and view it...

Expand Post »
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
Similar Threads
Reputation Points: 19
Solved Threads: 2
Posting Shark
belama is offline Offline
962 posts
since Mar 2005
Jun 14th, 2005
0

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

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.
Reputation Points: 46
Solved Threads: 2
Junior Poster
Iron_Cross is offline Offline
117 posts
since Jul 2003
Jun 14th, 2005
0

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

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
Reputation Points: 19
Solved Threads: 2
Posting Shark
belama is offline Offline
962 posts
since Mar 2005
Jun 14th, 2005
0

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

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.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jun 14th, 2005
0

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

Quote 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.
Reputation Points: 19
Solved Threads: 2
Posting Shark
belama is offline Offline
962 posts
since Mar 2005
Jun 14th, 2005
0

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

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.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jun 14th, 2005
0

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

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:

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Web;
  8. using System.Web.SessionState;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.HtmlControls;
  12.  
  13. namespace charts
  14. {
  15. /// <summary>
  16. /// Summary description for WebForm1.
  17. /// </summary>
  18. public class WebForm1 : System.Web.UI.Page
  19. {
  20. protected System.Web.UI.WebControls.Image Image1;
  21.  
  22. private void Page_Load(object sender, System.EventArgs e)
  23. {
  24. DisplayLine();
  25. Image1.ImageUrl = Server.MapPath("")+"temp.jpg";
  26.  
  27. }
  28.  
  29. #region Web Form Designer generated code
  30. override protected void OnInit(EventArgs e)
  31. {
  32. //
  33. // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  34. //
  35. InitializeComponent();
  36. base.OnInit(e);
  37. }
  38.  
  39. /// <summary>
  40. /// Required method for Designer support - do not modify
  41. /// the contents of this method with the code editor.
  42. /// </summary>
  43. private void InitializeComponent()
  44. {
  45. this.Load += new System.EventHandler(this.Page_Load);
  46.  
  47. }
  48. #endregion
  49.  
  50. private void DisplayLine()
  51. {
  52. Bitmap b =new Bitmap(250,250);
  53. b.Save(Server.MapPath("")+"temp.jpg",ImageFormat.Jpeg);
  54. Graphics g = Graphics.FromImage(b);
  55. g.Clear(Color.White);
  56. Pen p = new Pen(Color.Blue);
  57. g.DrawLine(p,10,240,240,240);
  58. b.Save(Server.MapPath("")+"temp.jpg",ImageFormat.Jpeg);
  59. }
  60. }
  61. }

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

C# Syntax (Toggle Plain Text)
  1. Response.ContentType = "image/jpeg";
  2. 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.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jun 15th, 2005
0

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

Thanks I'll check it out later.
Reputation Points: 19
Solved Threads: 2
Posting Shark
belama is offline Offline
962 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Creating a GUI using C#
Next Thread in C# Forum Timeline: setw() function in C#?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC