0
は、私がここにGridViewのこのコードを与えるより良く理解するためにGridViewコントロールに関連付けられているObjectDataSourceのためのコードでデータベースとのショーから画像を取得する必要が...おかげASP.NETの画像(BLOB)データベースから取得する(MySQLの)
public static List<Users> GetAllUser()
{
List<Users> UsersList = new List<Users>();
string connection_string = "datasource=localhost;port=3306;username=root;password=root;";
using (MySqlConnection connection = new MySqlConnection(connection_string))
{
MySqlCommand command = new MySqlCommand("select * from smart_shop.users order by id desc", connection);
connection.Open();
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Users userObject = new Users();
userObject.userName = reader["name"].ToString();
userObject.image = reader["image"].ToString(); //Here variable 'image' is string type but in database BLOB
UsersList.Add(userObject);
}
}
return UsersList;
}
とGridViewのコードが
<div class="col-md-6 col-sm-12 col-xs-12">
<div class="panel panel-default">
<h3> User Information</h3>
<div class="panel-body">
<asp:GridView ID="GridView2" CssClass="dataGridTable" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource2" AllowPaging="True" Width="100%" CellPadding="4" HeaderStyle-Height="30" ForeColor="#333333" GridLines="None" OnRowCreated="GridView2_RowCreated" PageSize="20">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="userName" HeaderText="User Name" SortExpression="userName" />
<asp:TemplateField HeaderText="Image" SortExpression="image">
<EditItemTemplate>
<asp:Image ID="Image1" runat="server" Height="100px" Width="100px" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" Height="30px" VerticalAlign="Middle" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" VerticalAlign="Middle" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllUser" TypeName="OnlineSmartShop.SettingsDataAccessLayer"></asp:ObjectDataSource>
</div>
</div>
</div>
し、最終的に
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
byte[] bytes = (byte[])(e.Row.DataItem as DataRowView)["image"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
(e.Row.FindControl("Image1") as Image).ImageUrl = "data:image/png;base64," + base64String;
}
}
を引き起こす可能性がありますがIMAGEURLに設定されているコンテンツタイプについてよろしいですか? –
ここでImageUrlとは何を言っているのか分かりませんか? – Simon
文字列「data:image/png;」 ImageUrlには、イメージのタイプを表すイメージのコンテンツタイプが追加されます。だから私はその値が画像データに関して正しいかどうかを確認することを提案していました。画像がjpegまたは他の形式である可能性があります。そのような場合、pngの設定は機能しません。 –