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

问题描述

我有一个应用程序,其中我以二进制形式将图像存储在数据库中,并且还可以在gridview控件中显示图像.我想知道如何在Gridview控件的多列中显示不同的图像.

表中的字段
ImgId int
ImageName varchar
图片图片
Image2图片

Default.aspx

I have an application in which I store the image in binary form in a database and also display the image in a gridview control. I want to know how to display different images in multiple columns in a Gridview control.

Field in table
ImgId int
ImageName varchar
Image image
Image2 image

Default.aspx

<asp:GridView ID="GridView1" runat="server"
              AutoGenerateColumns="False" DataKeyNames="ID" style="z-index: 103; left: 237px; position: absolute; top: 245px" Width="405px">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id"
                InsertVisible="False" ReadOnly="True"
                               SortExpression="Id" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName"
                               SortExpression="ImageName" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
           ImageUrl=''<%# Eval("ImgId", "Handler.ashx?ImgId={0}")+"&img=1"%>''/>
<asp:Image ID="Image2" runat="server"
ImageUrl=''<%# Eval("ImgId", "Handler.ashx?ImgID={0}")+"&img=2"%>''/>

</ItemTemplate>



Default3.cs
下面的代码用于将图像以二进制形式插入数据库



Default3.cs
Below code is for image insertion into the database in binary form

byte[] image = new byte[stream.Length];
               //set the binary data
               stream.Read(image,0,image.Length);
                   SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2005\WebSites\Project2\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
                   string que = "insert into Pictures(Id,ImageName,Image)values(''" + Session["Id"].ToString() + "'',@imagename,@image )";
                   SqlCommand com = new SqlCommand(que, con);
                   com.Parameters.Add("@imagename", SqlDbType.VarChar, 500);
                   com.Parameters.Add("@image", SqlDbType.Image, image.Length);

               com.Parameters["@imagename"].Value = strImageName.ToString();
                   com.Parameters["@image"].Value = image;

                   con.Open();
                   com.ExecuteNonQuery();
                   con.Close();
                   Response.Redirect("Default3.aspx");



Handle.ascx



Handle.ascx

using System;
using System.Web;
using System.Data.SqlClient;
public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        string imageid = context.Request.QueryString["ImgId"];
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2005\WebSites\Project2\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        SqlCommand com = new SqlCommand("Select ImageName,Image,Image2 From Pictures Where ImgId="+imageid,con);
      //  com.Parameters.Add("@imgid", System.Data.SqlDbType.Int).Value = context.Request.QueryString["Id"];
     con.Open();
    //com.Prepare();
    SqlDataReader dReader = com.ExecuteReader();
   dReader.Read();
  int intImg = Convert.ToInt32(context.Request.QueryString["img"]);

   if(intImg==1){
        context.Response.BinaryWrite((byte[])dReader["Image"]);
   }
        if(intImg==2)
        {
          context.Response.BinaryWrite((byte[])dReader["Image2"]);

        }

        dReader.Close();
      con.Close(); 

推荐答案


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

08-14 02:40