[英]How to show picture that is stored in SQL Server database using ASP.NET?
I have a SQL Server 2008 database that has image field which contains a picture. 我有一个SQL Server 2008数据库,其中包含包含图片的图像字段。
I have a gridview in my ASP.NET web program, and I need to show the picture in: 我的ASP.NET Web程序中有一个gridview,我需要显示以下图片:
my gridview 我的GridView
my image control that I have in the form 我在表单中拥有的图像控件
Can I get any sample code ? 我可以得到任何示例代码吗?
Thanks in advance. 提前致谢。
I believe that the Image will come over as binary. 我相信Image将以二进制形式出现。 You will need to convert the binary to image.
您将需要将二进制文件转换为图像。
These references will help you with the conversion 这些参考资料将帮助您进行转换
Silverlight 4.0: How to convert byte[] to image? Silverlight 4.0:如何将byte []转换为图像?
http://www.eggheadcafe.com/community/aspnet/2/10038022/convert-binary-data-to-an-image.aspx http://www.eggheadcafe.com/community/aspnet/2/10038022/convert-binary-data-to-an-image.aspx
use Generic handler to fetch image from database and to show that use ifram that will be better option to show files on web. 使用通用处理程序从数据库中获取图像并使用ifram进行显示,这将是在Web上显示文件的更好选择。
iframeFile.Attributes.Add("src", "/ShowImage.ashx?id=" + id);
And this is a Generic handler code to show image files and also for pdf files. 这是显示图像文件和pdf文件的通用处理程序代码。 if you just want to show image files just remove if else conditions and use image/jpeg for extension.
如果您只想显示图像文件,请删除其他条件,然后使用image / jpeg进行扩展。
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 imgID;
if (context.Request.QueryString["id"] != null)
imgID = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
byte[] buffer = ShowEmpImage(imgID);
bool temp = false;
string extension = "";
var enc = new ASCIIEncoding();
var header = enc.GetString(buffer);
dbClass db = new dbClass();
extension = db.GetID("select fileExtension from tableName WHERE LOGuid=" + imgID, "fileExtension");
if (extension == null || extension == "")
{
if (buffer[0] == 0x25 && buffer[1] == 0x50
&& buffer[2] == 0x44 && buffer[3] == 0x46)
{
temp = header.StartsWith("%PDF-");
extension = "application/pdf";
}
else if (buffer[0] == 0xFF && buffer[1] == 0xD8
&& buffer[2] == 0xFF && buffer[3] == 0xE0)
{
temp = header.StartsWith("%JPG-");
extension = "image/jpeg";
extension = "image/jpg";
}
else if (buffer[0] == 0x89 && buffer[1] == 0x50
&& buffer[2] == 0x4E && buffer[3] == 0x47)
{
temp = header.StartsWith("%PNG-");
extension = "image/jpeg";
extension = "image/png";
}
else if (buffer[0] == 0x49 && buffer[1] == 0x49
&& buffer[2] == 0x2A && buffer[3] == 0x00)
{
temp = header.StartsWith("%TIF-");
extension = "image/jpeg";
extension = "image/tiff";
extension = "image/tif";
}
else if (buffer[0] == 0x47 && buffer[1] == 0x49
&& buffer[2] == 0x46 && buffer[3] == 0x38)
{
temp = header.StartsWith("%GIF-");
extension = "image/jpeg";
extension = "image/gif";
}
else if (buffer[0] == 0x42 && buffer[1] == 0x4D
&& buffer[2] == 0x46 && buffer[3] == 0x38)
{
temp = header.StartsWith("%BMP-");
extension = "image/jpeg";
extension = "image/bmp";
}
else if (buffer[0] == 0x00 && buffer[1] == 0x00
&& buffer[2] == 0x01 && buffer[3] == 0x00)
{
temp = header.StartsWith("%ICO-");
extension = "image/jpeg";
extension = "image/ico";
}
else
extension = "image/jpeg";
//else
//extension = "application/pdf";
}
context.Response.ContentType = extension;
context.Response.AddHeader("content-length", buffer.Length.ToString());
context.Response.BinaryWrite(buffer);
}
public byte[] ShowEmpImage(int imgID)
{
dbClass db = new dbClass();
string sql = "SELECT LOGpdf FROM tableName WHERE LOGuid = @ID";
SqlCommand cmd = new SqlCommand(sql, db.con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", imgID);
object img = null;
try
{
db.Connect();
img = cmd.ExecuteScalar();
}
catch
{
return null;
}
finally
{
db.Disconnect();
}
return (byte[])img;
}
public bool IsReusable
{
get
{
return false;
}
}
}
You could use the data uri scheme with base64 (assuming it is stored in base64) string ie 您可以将data uri方案与base64一起使用(假设它存储在base64中),即
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
See http://en.wikipedia.org/wiki/Data_URI_scheme 参见http://en.wikipedia.org/wiki/Data_URI_scheme
So for example if you were using a databound control you would ie for a repeater do this: 因此,例如,如果您使用的是数据绑定控件,则对于中继器,可以这样做:
<ItemTemplate>
<img src='data:image/<%# Eval("FileType") %>;base64,<%# Eval("ImageData") %> width='<%# Eval("ImgWidth")%>' height='<%# Eval("ImgHeight")%>' />
</ItemTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.