这是我显示图片的代码:
<asp:image ID="Image3" runat="server" ImageUrl="AyoBelajar_Angka.aspx?id_bahan=7">
这是我的代码隐藏:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("id_bahan") IsNot Nothing Then
Dim strQuery As String = "select nama,kategori, gambar from ayobelajar where id_bahan=@id_bahan"
Dim cmd As SqlCommand = New SqlCommand(strQuery)
cmd.Parameters.Add("@id_bahan", SqlDbType.VarChar).Value() = Convert.ToInt32(Context.Request.QueryString("id_bahan"))
Dim dt As DataTable = GetData(cmd)
Dim bytes() As Byte = CType(dt.Rows(0)("gambar"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = dt.Rows(0)("kategori").ToString()
Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows(0)("nama").ToString())
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
答案 0 :(得分:0)
将您的字节数组转换为base64String字符串,然后将其分配给您的图像控件。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("id_bahan") IsNot Nothing Then
Dim strQuery As String = "select nama,kategori, gambar from ayobelajar where id_bahan=@id_bahan"
Dim cmd As SqlCommand = New SqlCommand(strQuery)
cmd.Parameters.Add("@id_bahan", SqlDbType.VarChar).Value() = Convert.ToInt32(Context.Request.QueryString("id_bahan"))
Dim dt As DataTable = GetData(cmd)
Dim bytes() As Byte = CType(dt.Rows(0)("gambar"), Byte())
'Converting Byte array to ToBase64String
Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)
Image3.ImageUrl = "data:image/png;base64," & base64String
End Sub
答案 1 :(得分:0)
从你的代码隐藏中看来,你的图像被保存为byte
数组而不是字符串。您发布的代码隐藏不返回字符串,它返回二进制数据,并且不能放在需要字符串的ImageUrl
属性中。
您可以将图像二进制数据转换为Base64String
,然后将其放入ImageUrl
属性中。转换非常简单:
Dim ImageString As String = Convert.ToBase64String(bytes, 0, bytes.Length)
但是,您必须在字符串前面添加图像类型。如果您的所有图像都是相同的类型,那么让我们说jpg文件,然后这很容易:
ImageString = "data:image/jpg;base64," & ImageString
但是如果数据库中有不同类型的图像,则必须从二进制数据中检测图像的类型。 Here is how you can do so。但似乎你有dt.Rows(0)("kategori").ToString()
中的类型?
最后,你需要返回字符串,所以这里是完整的代码(将jpg改为你的图像类型):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("id_bahan") IsNot Nothing Then
Dim strQuery As String = "select nama,kategori, gambar from ayobelajar where id_bahan=@id_bahan"
Dim cmd As SqlCommand = New SqlCommand(strQuery)
cmd.Parameters.Add("@id_bahan", SqlDbType.VarChar).Value() = Convert.ToInt32(Context.Request.QueryString("id_bahan"))
Dim dt As DataTable = GetData(cmd)
Dim bytes() As Byte = CType(dt.Rows(0)("gambar"), Byte())
Dim ImageString As String = Convert.ToBase64String(bytes, 0, bytes.Length)
ImageString = "data:image/jpg;base64," & ImageString
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "text/plain"
Response.Write(ImageString)
Response.End()
Else
'I don't know what code you have here
End If
End Sub
答案 2 :(得分:0)
嗨,你要在数据库中有一个想要显示该图像的图像路径。您可以将其转换为ToBase64String格式并显示,也可以直接完成。如果你的路径是正确的。
/(\d{4}\/[^ ]+) \K([\d:]+)/
查看
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'From database get path and assign to imagePath variable
Dim imagePath As String = "~/Files/happy.jpg"
Dim filePath As String = Server.MapPath(imagePath)
' Will Convert filePath to bytes
Dim imageBytes As Byte() = File.ReadAllBytes(filePath)
'bytes array to ToBase64String
Dim base64String As String = Convert.ToBase64String(imageBytes, 0, imageBytes.Length)
'Attaching image to ToBase64String
Image3.ImageUrl = "data:image/jpg;base64," & base64String
End Sub
输出
文件夹视图