C# 在asp.net中从数据库中检索图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14935205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve image from database in asp.net
提问by Ahmad Abbasi
How to retrieve images from sql database in asp.net using c#.
如何使用c#从asp.net中的sql数据库中检索图像。
i want to retrieve the image file from database and then display the image in a tag.
我想从数据库中检索图像文件,然后在标签中显示图像。
i try this code but it is not working
我试试这个代码,但它不起作用
aspx
aspx
<asp:Image ID="Image1" runat="server" ImageUrl="" Height="150px" Width="165px" />
code behind
背后的代码
Byte[] bytes = (Byte[])ds.Tables[0].Rows[0]["image"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
How to give link to the ImageUrl=""
of this image???
怎么给ImageUrl=""
这个图片的链接???
采纳答案by ????
Create a generic http handler
as follows
创建一个generic http handler
如下
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 empno;
if (context.Request.QueryString["id"] != null)
empno = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
Stream strm = ShowEmpImage(empno);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}
public Stream ShowEmpImage(int empno)
{
string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID";
SqlCommand cmd = new SqlCommand(sql,connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", empno);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
and display image as follow
并显示图像如下
Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
There are some links below
Showing image in GridView from the database?
How to show a image in database in the image control of Asp.net?
Display image from database in ASP.net with C#
http://www.dotnetcurry.com/ShowArticle.aspx?ID=129
下面有一些链接
从数据库中在 GridView中显示图像?
如何在Asp.net的图片控件中显示数据库中的图片?
使用 C# 在 ASP.net 中显示数据库中的图像
http://www.dotnetcurry.com/ShowArticle.aspx?ID=129
回答by Stefano Altieri
I don't think this is the right approach. You should not embed image into html, and this is not the right way anyway.
我不认为这是正确的方法。您不应该将图像嵌入到 html 中,无论如何这都不是正确的方法。
I suggest adding an ashx (generic handler) and use it to generate the image from query string, then ini the page use something like
我建议添加一个 ashx(通用处理程序)并使用它从查询字符串生成图像,然后在页面中使用类似的东西
<asp:Image ImageUrl='GetImage.ashx?id=12345' ... />
回答by Abdul Khaliq
<asp:Image ID="ImgProfilePic" runat="server" />
byte[] imagem = (byte[])(dr["IMG"]);
string PROFILE_PIC = Convert.ToBase64String(imagem);
ImgProfilePic.ImageUrl = String.Format("data:image/jpg;base64,{0}", PROFILE_PIC);