asp.net 读取显示二进制图片,缺少判断,求审阅
ashx文件:MemoryStreamstream=newMemoryStream();intid=int.Parse(context.Request.QueryStri...
ashx文件:
MemoryStream stream = new MemoryStream();
int id = int.Parse(context.Request.QueryString["id"]);
SqlConnection connection = new SqlConnection("省略");
try
{
connection.Open();
SqlCommand command = new SqlCommand("select [WishImage] from Wish where WishID='" + id + "' and IsApproved=True", connection);
command.Parameters.Add(new SqlParameter("@id", id));
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
byte[] image = (byte[])reader["WishImage"];
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
context.Response.ContentType = "image/jpeg";
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
finally
{
connection.Close();
stream.Close();
}
aspx显示文件:
<script language="javascript" type="text/javascript">
var i = 1;
function fun() {
i++;
document.getElementById("img1").src = "display.ashx?id=" + i;
if (i == 7)
{ i = 0; }
}
setInterval("fun()", 1000);
</script>
<asp:Image ID="img1" runat="server" src="display.ashx?id=1">
我现在可以逐个显示从数据库提取出来的二进制图片,但是缺少一个判断,
比如一共6个图片 ID为1,3,4,5,6的IsApproved=True ID2的为False
运行的时候2还是会显示出来,但是图片没有读出来(因为没从database取值),显示的一个叉,怎么能加一个判断,当IsApproved的时候不显示,直接跳过这个ID 展开
MemoryStream stream = new MemoryStream();
int id = int.Parse(context.Request.QueryString["id"]);
SqlConnection connection = new SqlConnection("省略");
try
{
connection.Open();
SqlCommand command = new SqlCommand("select [WishImage] from Wish where WishID='" + id + "' and IsApproved=True", connection);
command.Parameters.Add(new SqlParameter("@id", id));
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
byte[] image = (byte[])reader["WishImage"];
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
context.Response.ContentType = "image/jpeg";
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
finally
{
connection.Close();
stream.Close();
}
aspx显示文件:
<script language="javascript" type="text/javascript">
var i = 1;
function fun() {
i++;
document.getElementById("img1").src = "display.ashx?id=" + i;
if (i == 7)
{ i = 0; }
}
setInterval("fun()", 1000);
</script>
<asp:Image ID="img1" runat="server" src="display.ashx?id=1">
我现在可以逐个显示从数据库提取出来的二进制图片,但是缺少一个判断,
比如一共6个图片 ID为1,3,4,5,6的IsApproved=True ID2的为False
运行的时候2还是会显示出来,但是图片没有读出来(因为没从database取值),显示的一个叉,怎么能加一个判断,当IsApproved的时候不显示,直接跳过这个ID 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询