我有一个名为" Staff"的表,在该表中我有一个名为" ProfilePicture"的实体。包含编码的Base64图像字符串。
如何解码base64图像并在下面的gridview中显示?
<asp:GridView ID="staffgrid" runat="server" AutoGenerateColumns="False" DataKeyNames="Staff_ID" DataSourceID="staffdb">
<Columns>
<asp:BoundField DataField="Staff_ID" HeaderText="ID" ReadOnly="True" SortExpression="Staff_ID"></asp:BoundField>
<asp:BoundField DataField="ProfilePicture" HeaderText="ProfilePicture" ReadOnly="true" SortExpression="ProfilePicture">
<ItemStyle Width="100px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>
<asp:BoundField DataField="Gender" HeaderText="gender" SortExpression="Gender"></asp:BoundField>
<asp:BoundField DataField="Email" HeaderText="email" SortExpression="Email"></asp:BoundField>
<asp:BoundField DataField="Password" HeaderText="password" SortExpression="Password"></asp:BoundField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Button" ShowHeader="True" HeaderText="Edit"></asp:CommandField>
<asp:ImageField></asp:ImageField>
</Columns>
</asp:GridView>
<asp:SqlDataSource runat="server" ID="staffdb" ConnectionString='<%$ ConnectionStrings:conStr %>' SelectCommand="SELECT [Staff_ID], [Name], [Email], [Password], [Gender], [ProfilePicture] FROM [Staff]"></asp:SqlDataSource>
我在互联网上找到了一个函数,它接受一个字符串,并在一个名为img
的变量中返回图像,但我无法弄清楚如何实现它。
Function Base64ToImage(ByVal base64string As String) As System.Drawing.Image
'Setup image and get data stream together
Dim img As System.Drawing.Image
Dim MS As System.IO.MemoryStream = New System.IO.MemoryStream
Dim b64 As String = base64string.Replace(" ", "+")
Dim b() As Byte
'Converts the base64 encoded msg to image data
b = Convert.FromBase64String(b64)
MS = New System.IO.MemoryStream(b)
'creates image
img = System.Drawing.Image.FromStream(MS)
Return img
End Function
答案 0 :(得分:3)
您不需要将base64字符串转换为System.Drawing.Image
。您只需将它作为字符串显示在GridView中即可。
<asp:TemplateField>
<ItemTemplate>
<img src="data:image/jpeg;base64,<%# Eval("ProfilePicture") %>" />
</ItemTemplate>
</asp:TemplateField>