gpt4 book ai didi

c# - 将字节从 sql server 渲染到图像控件?

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:21 24 4
gpt4 key购买 nike

标记:

<asp:ListView ID="lvGallery" runat="server" DataSourceID="SqlDataSource1">
<LayoutTemplate>
<table runat="server" id="tableGallery">
<tr runat="server" id="itemPlaceHolder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<div class="box_img2">
<div class="g_size">
<a runat="server" id="linkImage">
<img id="Image1" runat="server" src="MyImage.jpg" />
</a>
</div>
</div>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..." ProviderName="System.Data.SqlClient" SelectCommand="SELECT [Image] FROM [GalleryImages]"></asp:SqlDataSource>

代码隐藏:

public void ProcessRequest(HttpContext context)
{
//write your handler implementation here.
string username = Convert.ToString(context.Request.QueryString["username"]);
if (username != null)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
byte[] arrContent;
DataRow dr;
string strSql;
strSql = "Select Image from GalleryImages where username = '" + username + "'";
da = new SqlDataAdapter(strSql, connection.ConnectionString);
da.Fill(ds);
dr = ds.Tables[0].Rows[0];
arrContent = (byte[])dr["ImageFile"];
context.Response.ContentType = "jpeg";
context.Response.OutputStream.Write(arrContent, 0, arrContent.Length);
context.Response.End();
}
}

我还在 web.config 中添加了 httphandlers 部分。但是我没有在 ListView 控件中获取图像。

最佳答案

您必须创建一个返回图像的 http 处理程序

public void ProcessRequest(HttpContext context)
{
byte[] yourImage = //get your image byte array
context.Response.BinaryWrite(yourImage);
context.Request.ContentType = "image/jpeg";
context.Response.AddHeader("Content-Type", "image/jpeg");
context.Response.AddHeader("Content-Length", (yourImage).LongLength.ToString());
con.Close();

context.Response.End();
context.Response.Close();
}

您可以通过从 visual studio 创建一个 GenericHandler 文件类型并添加前面的代码来做到这一点,然后您可以调用您可以将通用处理程序的 url 写为图像源

关于c# - 将字节从 sql server 渲染到图像控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6878033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com