如何在gridview中显示图像? [英] How to show an image in the gridview?

查看:66
本文介绍了如何在gridview中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经以base64字符串的形式将我的图像存储在数据库中。现在我想在我的gridview中显示这个图像,而不将字符串转换回图像。我正在使用以下代码,但它没有显示我的图像。

 <   asp:GridView     ID   =  gvList    runat   =  server    AutoGenerateColumns   =  false    ShowHeaderWhenEmpty   =  true  

< span class =code-attribute> HeaderStyle-CssClass = GridView_Header RowStyle-CssClass = GridView_Row

AlternatingRowStyle-CssClass = GridView_AlterRow 样式 = 宽度:95%;身高:100%; table-layout:fixed;网格行大小:60px;溢出:滚动; vertical-align:top OnRowDataBound = gvList_RowDataBound >
< >
< asp:BoundField DataField = BioDataID HeaderText = Sr.否 ItemStyle-Horizo​​ntalAlign = 中心 HeaderStyle-Horizo​​ntalAlign = 中心 / >
< asp:BoundField DataField = d1 HeaderText = / >
< asp:BoundField DataField = d2 HeaderText = Tehsil / >
< asp: BoundField DataField = d3 HeaderText = 警察局 / >
< asp:BoundField < span class =code-attribute>
DataField = CNIC HeaderText = CNIC / >
< asp:BoundField DataField = 南e HeaderText = 名称 / >
< asp:BoundField < span class =code-attribute> DataField = TemporaryAddress HeaderText = 地址 / >
< asp:BoundField DataField = 年龄 HeaderText = 年龄 / >
<% - < asp:BoundField DataField = YearOfBirth HeaderText = 出生年份 /> - %>
< asp:TemplateField >
& lt; HeaderTemplate > 图片< / HeaderTemplate >
< ItemTemplate >
< img src =' <% #Eval( ImagePath%> ' alt = image height = 100 width < span class =code-keyword> = 200 / >
< / ItemTemplate >
< / asp:TemplateField >


<% - < asp:ImageField DataImageUrlField = ImagePath
DataImageUrlFormatString = ' <%#Eval(ImagePath)%>'
AlternateText = 员工照片
NullDisplayText = 文件中没有图像。
HeaderText = < span class =code-string> Photo
ReadOnly = true /> - %>
< / Columns >
< EmptyDataTemplate >
< width = 100% border = 0 >
< tr class = GridView_Row >
< td colspan = 4 style = font-size:16; font-weight:bold;红色; text-align:center; > 找不到记录!
< / td >
< / tr >
< ; / table >

< / EmptyDataTemplate >
< / asp:GridView >

解决方案

点击链接



[ ^ ]


I have stored my image in the database in the form of a base64 string. Now I want to show this image in my gridview without converting the the string back to image. I'm using the following code but it doesn't show my image.

<asp:GridView ID="gvList" runat="server" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true"

                                HeaderStyle-CssClass="GridView_Header" RowStyle-CssClass="GridView_Row"

                                AlternatingRowStyle-CssClass="GridView_AlterRow" Style="width: 95%; height: 100%; table-layout: fixed; grid-row-sizing: 60px; overflow: scroll; vertical-align: top" OnRowDataBound="gvList_RowDataBound">
                                <Columns>
                                    <asp:BoundField DataField="BioDataID" HeaderText="Sr. No" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
                                    <asp:BoundField DataField="d1" HeaderText="District" />
                                    <asp:BoundField DataField="d2" HeaderText="Tehsil" />
                                    <asp:BoundField DataField="d3" HeaderText="Police Station" />
                                    <asp:BoundField DataField="CNIC" HeaderText="CNIC" />
                                    <asp:BoundField DataField="Name" HeaderText="Name" />
                                    <asp:BoundField DataField="TemporaryAddress" HeaderText="Address" />
                                    <asp:BoundField DataField="Age" HeaderText="Age" />
                                    <%--<asp:BoundField DataField="YearOfBirth" HeaderText="Year Of Birth" />--%>
                                    <asp:TemplateField>
                                        <HeaderTemplate>Image</HeaderTemplate>
                                        <ItemTemplate>
                                            <img src='<%# Eval("ImagePath") %>' alt="image" height="100" width="200" />
                                        </ItemTemplate>
                                    </asp:TemplateField>


                                    <%--<asp:ImageField DataImageUrlField="ImagePath"
                                        DataImageUrlFormatString='<%# Eval("ImagePath") %>'
                                        AlternateText="Employee Photo"
                                        NullDisplayText="No image on file."
                                        HeaderText="Photo"
                                        ReadOnly="true" />--%>
                                </Columns>
                                <EmptyDataTemplate>
                                    <table width="100%" border="0">
                                        <tr class="GridView_Row">
                                            <td colspan="4" style="font-size: 16; font-weight: bold; color: Red; text-align: center;">No Records Found!
                                            </td>
                                        </tr>
                                    </table>

                                </EmptyDataTemplate>
                            </asp:GridView>

解决方案

Follow the link

http://www.aspsnippets.com/Articles/Display-image-from-database-in-Image-control-without-using-Generic-Handler-in-ASPNet.aspx[^]


If manipulating in code below code , you can use

public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,imageBytes.Length);

// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}


or if you are using in webpage use direct


Please go through the below link
Displaying Images from a Database in a GridView[^]


这篇关于如何在gridview中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆