SqlConnection conn = new SqlConnection(connectionString); conn.Open(); SqlCommand cmd = new SqlCommand("select image from table where id=1", conn); // or whatever byte[] blob = (byte[])cmd.ExecuteScalar(); MemoryStream ms = new MemoryStream(); ms.Write(blob, 0, blob.Length); Image img = Image.FromStream(ms); img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg); // EDIT extra line added conn.Close();
vulpes, does this retrieve any perticular image (whose ID=1)from database? If that so, then can I take that ID from any TextBox and put it in DML? Can that be done?
This is just intended as a specimen 'select' query where you select a field called 'image' from a record in 'table' whose 'id' field contains 1. But, yes, you could take the id from a textbox and embed that into the query. If 'id' is a string rather than an integer in the database, then you'll need to surround it with single quotes, of course.
Thanks for that, although I am getting the following error:
Cannot implicitly convert type 'System.Drawing.Image' to 'System.Web.UI.WebControls.Image'
for Image1 = System.Drawing.Image.FromStream(ms);
I have directly referenced it to System.Drawing.Image as it didnt know whether to use that or using System.Web.UI.WebControls;
Any ideas?
Thanks.
If it's an ASP.NET application, then I think you'll need to save the image to a file first. You can then load it into a 'System.Web.UI.WebControls.Image' control by setting its ImageURL property to the file path. I've edited my answer to include an extra line to save the image to a file. The 'Image' class in the code is the System.Drawing.Image class, as you surmised.
2 answers
Anyone?
answered 2 years ago by:
0
Something like this:
answered 2 years ago by:
17279
226
vulpes, does this retrieve any perticular image (whose ID=1)from database? If that so, then can I take that ID from any TextBox and put it in DML? Can that be done?
17279
This is just intended as a specimen 'select' query where you select a field called 'image' from a record in 'table' whose 'id' field contains 1. But, yes, you could take the id from a textbox and embed that into the query. If 'id' is a string rather than an integer in the database, then you'll need to surround it with single quotes, of course.
0
Thanks for that, although I am getting the following error: Cannot implicitly convert type 'System.Drawing.Image' to 'System.Web.UI.WebControls.Image' for Image1 = System.Drawing.Image.FromStream(ms); I have directly referenced it to System.Drawing.Image as it didnt know whether to use that or using System.Web.UI.WebControls; Any ideas? Thanks.
17279
If it's an ASP.NET application, then I think you'll need to save the image to a file first. You can then load it into a 'System.Web.UI.WebControls.Image' control by setting its ImageURL property to the file path. I've edited my answer to include an extra line to save the image to a file. The 'Image' class in the code is the System.Drawing.Image class, as you surmised.