高分请教如何从数据库中读取二进制图片放datalist显示

xiaojunyaoac 2006-11-03 10:36:12
我要在从数据库里把二进制图片取出来,放到DATALIST里面显示
怎么显示,
求源码。。。
...全文
199 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hertcloud 2007-03-11
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/article/ecd9ae16-8ff0-4a1c-9b9f-5e8b641cb1b1/read.aspx
ilexyang 2007-03-08
  • 打赏
  • 举报
回复
根据formview,可以写出gridview
gridview代码:
<asp:formview id="FormView1" runat="server" datasourceid="ObjectDataSource1" cssclass="view"
borderstyle="None" borderwidth="0px" CellPadding="0" EnableViewState="False" AllowPaging="True">
<itemtemplate>
<div class="buttonbar buttonbar-top">
<asp:ImageButton ID="ImageButton9" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
<asp:ImageButton ID="ImageButton10" Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
<asp:ImageButton ID="ImageButton11" Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
<asp:ImageButton ID="ImageButton12" Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
</div>
<p><%# Eval("Name") %></p>
<table border="0" cellpadding="0" cellspacing="0" class="photo-frame">
<tr>
<td><a href="handler.ashx?PaintingId=<%# Eval("PaintingId")%>" target="_blank">
<img src="Handler.ashx?PaintingId=<%# Eval("PaintingId") %>&Size=L" class="photo_198" style="border:4px solid white" alt='作品编号 <%# Eval("PaintingId") %>' /></a></td>
</tr>
<tr>
<td><a href=""></a></td>
<td><a href="Word.aspx">我要留言</a></td>
</tr>
</table>
<a href="handler.ashx?PaintingId=<%# Eval("PaintingId")%>" target="_blank">
<p> <asp:image runat="Server" id="DownloadButton" AlternateText="浏览此作品原图" skinid="download" /></a></p>
<div class="buttonbar">
<asp:ImageButton ID="ImageButton1" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
<asp:ImageButton ID="ImageButton2" Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
<asp:ImageButton ID="ImageButton3" Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
<asp:ImageButton ID="ImageButton4" Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
</div>
</itemtemplate>
</asp:formview>
handler.ashx代码:
<%@ WebHandler Language="C#" Class="Handler" %>

//整个Handler.ashx文件是一个返回二进制(在本系统里是图片)的数据。写得真凶!!!

using System;
using System.IO;
using System.Web;

public class Handler : IHttpHandler {

public bool IsReusable {
get {
return true;
}
}

//实例处理图片的类
PaintingBLL painting = new PaintingBLL();

public void ProcessRequest (HttpContext context) {
// 设置响应设置
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
// 设置 Size 参数
PaintingSize size;
switch (context.Request.QueryString["Size"]) {
case "S":
size = PaintingSize.Small;
break;
case "M":
size = PaintingSize.Medium;
break;
case "L":
size = PaintingSize.Large;
break;
default:
size = PaintingSize.Original;
break;
}
// 设置 PhotoID 参数
Int32 id = -1;
Stream stream = null;
if (context.Request.QueryString["PaintingId"] != null && context.Request.QueryString["PaintingId"] != "")
{
id = Convert.ToInt32(context.Request.QueryString["PaintingId"]);
stream=painting.GetTypePainting(id,size);
}
else {
id = Convert.ToInt32(context.Request.QueryString["VolumeId"]);
stream = painting.GetFirstTypePainting(id, size);
}

// 从数据库获取照片,如果未返回照片,将获取默认的“placeholder”照片
if (stream == null) stream = painting.GetTypePainting(size);

// 将图像流写入响应流中
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0) {
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}

}
C#后台代码:
//根据size的大小获取相应大小的单个图片
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select,false)]
public Stream GetTypePainting(int paintingId, PaintingSize size)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["PainterConnectionStringExpress"].ConnectionString))
{
using (SqlCommand command = new SqlCommand())
{
string ThumbText = "Select BytesThumb from Painting where PaintingId=" + paintingId;
string PosterText = "Select BytesPoster from Painting where PaintingId="+ paintingId;
string FullText = "Select BytesFull from Painting where PaintingId=" + paintingId;
string OriginalText = "Select BytesOriginal from Painting where PaintingId="+paintingId;
switch (size)
{
case PaintingSize.Small:
command.CommandText =ThumbText;
break;
case PaintingSize.Medium:
command.CommandText=PosterText;
break;
case PaintingSize.Original:
command.CommandText = OriginalText;
break;
default:
command.CommandText = FullText;
break;
}
command.Connection = connection;
connection.Open();
object result = command.ExecuteScalar();
try
{
return new MemoryStream((byte[])result);
}
catch
{
return null;
}
}
}
fgmis 2006-11-03
  • 打赏
  • 举报
回复
ding

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧