简体   繁体   English

如何使用ASP.NET显示存储在SQL Server数据库中的图片?

[英]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,我需要显示以下图片:

  1. my gridview 我的GridView

  2. 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.

相关问题 使用ASP.NET MVC中的Entity Framework将图片上传到SQL服务器数据库 - Upload picture to SQL Server database using Entity Framework in ASP.NET MVC 使用asp.net和SQL Server在存储在数据库中的浏览器中显示页面 - Display a page in browser stored in Database using asp.net and sql server 使用SQL Server数据库和ASP.NET应用程序中的存储过程在多个表中插入记录 - Inserting records in multiple tables using stored procedure in SQL Server database and ASP.NET application 如何在SQL Server中将存储的pdf btye数组显示到图像框中的asp.net WebForm? - How to show stored pdf btye array in SQL Server to asp.net WebForm in a image box? SQL Server:存储过程和ASP.NET - SQL Server : stored procedure and ASP.NET 如何使用存储在SQL数据库中的现有加密密码在asp.net网站中验证用户? - How to validate a user in asp.net web site using the existing encrypted password stored in the SQL Database? 在asp.net中使用c#更新sql server数据库 - updating sql server database using c# in asp.net 使用asp.net和SQL Server浏览文件并将其上传到数据库 - Browsing and Uploading a file to database using asp.net and sql server 从SQL数据库asp.net C#加载图片 - Load picture from SQL database asp.net C# 如何从SQL Server数据库中检索图像字段civilimage1,civilimage2,以在带有vb.net或c#的ASP.NET中使用图像控件进行显示? - How to retrieve image fields civilimage1,civilimage2 from SQL Server database to show using image control in ASP.NET with vb.net or c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM