【问题标题】:ASP.NET - How to display image in browser without saving image in temp?ASP.NET - 如何在浏览器中显示图像而不将图像保存在临时文件中?
【发布时间】:2014-11-12 15:12:33
【问题描述】:

我有一个问题-如何在浏览器中显示图像而不将图像保存在 Temp 文件夹中?甚至可能吗?我的代码必须从数据库中读取图像并在网站中显示图像。我实际上尝试从数据库转换数据,但我不知道我想做什么。 我也尝试使用“imageHandlers”、“FileStream”、“Base64StringToBitmap”,但仍然没有任何效果...... 请编写示例代码或修改我的代码。

private void LoadImages()
{
ImageButton imageButton = new ImageButton();
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandText = "select Id, Name, Data from tblFiles WHERE email = @CurrentUser";
        cmd.Parameters.Add("@CurrentUser", SqlDbType.NVarChar);
        cmd.Parameters["@CurrentUser"].Value = User.Identity.Name;
        cmd.Connection = conn;
        conn.Open();

        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            if (sdr.HasRows)
            {
                sdr.Read();

                string fileName = sdr["Name"].ToString();
                FileInfo fi = new FileInfo(fileName);
                byte[] byte_image_string = ((byte[])sdr["Data"]);
                string image_string = Convert.ToBase64String((byte[])sdr["Data"]) + fi.Name;

                imageButton.Height = Unit.Pixel(100);
                imageButton.Style.Add("padding", "5px");
                imageButton.Width = Unit.Pixel(100);
                imageButton.Click += new ImageClickEventHandler(imageButton_Click);
                Panel1.Controls.Add(imageButton);

                System.Drawing.Image newImage;

                if (byte_image_string != null)
                {
                    using (MemoryStream stream = new MemoryStream(byte_image_string))
                    {
                        newImage = System.Drawing.Image.FromStream(stream);

                        //I want here display image to browser without saving

                        //string newPhoto = "";
                        //newImage.Save(newPhoto);

                        imageButton.ImageUrl = "data:image/jpg;base64," + newPhoto;
                    }   
                }   
                conn.Close();
            }
        }    
    }
}
}

我来自数据库的示例图像代码:

0xFFD8FFE000104A46494600010100000100010000FFE1018C45786966000049492A0008000000020031010200070000002600000069870400010000002E00000000000000476F6F676C6500000500009007000400000030323230099007000B0000007000000086920700080100007B00000002A00400010000006F02000003A0 P>

【问题讨论】:

  • 你可以尝试将img文件显示为ImageUrl后删除。
  • 只有在网站上显示图片的方法吗?
  • 检查我的答案并告诉我你是否同意。

标签: c# sql asp.net database type-conversion


【解决方案1】:

您似乎以错误的方式创建 Base64String。您将文件名附加到它不应该工作。只需尝试以下。

string image_string = Convert.ToBase64String((byte[])sdr["Data"]);

直接将其分配给ImageUrl。此处无需使用MemoryStream

imageButton.ImageUrl = "data:image/jpg;base64," + image_string;

【讨论】:

    【解决方案2】:

    这就是我在我的一个项目中的做法:查看 src 部分

    <p><a href='<%#"TheObject.aspx?O=" + Eval("ID") %>'><img runat="server" visible='<%#Eval("AttachmentID") != DBNull.Value %>' class="objPicture1" alt='<%Eval("Title") %>' width="340" height="260" src='<%#"~/Attachment.aspx?ID=" + Eval("AttachmentID")%>' /></a></p>
    

    在 Attachment.aspx 中有以下代码:

        protected void Page_Load(object sender, EventArgs e)
        {
            Guid objID = //take the ID of the object ?ID=""
    
            DataRow attachmentRow = //fetch DataRow of the Attachment from Database
    
            if (attachmentRow == null)
                return;
    
            Response.ContentType = attachmentRow["ContentType"].ToString();// value of this is image/gif or image/jpeg and etc.
            Response.BinaryWrite((byte[])attachmentRow["Data"]); // Data is type image in my case                
            Response.End();
        }
    

    【讨论】:

    • 我的代码:pastebin.com/Gn2CFzpw 错误 1 ​​无法将类型 'string' 隐式转换为 'int'
    • 我无法更好地解释它,在 Attachement.aspx 中进行获取图像的查询并像 byte[] 响应一样返回它。在图片 url 中写上附件.aspx 的正确链接!你想让你成为一个完全符合你情况的例子吗?
    猜你喜欢
    • 1970-01-01
    • 2011-03-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多