2013-09-26 81 views
0

我的项目允许管理员添加奖章给军官简介,目前我只能插入最多5枚奖牌。但是我的老师让我让管理员为他们的官员插入尽可能多的奖章。我不知道如何检索管理员插入的所有图像,我知道如何使用varbinary将图像插入数据库。请给我这样做的方式。谢谢!显示多个图像从数据库检索

代码下面是我在最多5枚奖牌的插入操作:

代码上传:

System.Drawing.Image uploaded = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream); 

System.Drawing.Image newImage = new Bitmap(1024, 768); 
using (Graphics g = Graphics.FromImage(newImage)) 
{ 
    g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    g.DrawImage(uploaded, 0, 0, 1024, 768); 
} 

byte[] results; 
using (MemoryStream ms = new MemoryStream()) 
{ 
    ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid); 
    EncoderParameters jpegParms = new EncoderParameters(1); 
    jpegParms.Param[0] = new EncoderParameter(Encoder.Quality, 95L); 
    newImage.Save(ms, codec, jpegParms); 
    results = ms.ToArray(); 
} 

string sqlImage = "Insert into OfficerMedal(policeid, image) values ('" + Session["policeid"] + "', @Data)"; 
SqlCommand cmdImage = new SqlCommand(sqlImage); 
cmdImage.Parameters.AddWithValue("@Data", results); 
InsertUpdateData(cmdImage); 

我使用aspx页面

protected void Page_Load(object sender, EventArgs e) 
    { 
     string strQuery = "select image from OfficerMedal where policeid='" + Session["policeid"] + "'"; 
     SqlCommand cmd = new SqlCommand(strQuery); 
     DataTable dt = GetData(cmd); 
     if (dt != null) 
     { 
      download(dt); 
     } 
    } 

    private DataTable GetData(SqlCommand cmd) 
    { 
     DataTable dt = new DataTable(); 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 
     SqlDataAdapter sda = new SqlDataAdapter(); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 
     try 
     { 
      con.Open(); 
      sda.SelectCommand = cmd; 
      sda.Fill(dt); 
      return dt; 
     } 
     catch 
     { 
      return null; 
     } 
     finally 
     { 
      con.Close(); 
      sda.Dispose(); 
      con.Dispose(); 
     } 
    } 

    private void download(DataTable dt) 
    { 
     // check if you have any rows at all 
     // no rows -> no data to convert! 
     if (dt.Rows.Count <= 0) 
      return; 

     // check if your row #0 even contains data -> if not, you can't do anything! 
     if (dt.Rows[0].IsNull("image")) 
      return; 

     Byte[] bytes = (Byte[])dt.Rows[0]["image"]; 
     Response.Buffer = true; 
     Response.Charset = ""; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.ContentType = "image/jpg"; 
     Response.BinaryWrite(bytes); 
     Response.End(); 
    } 

要增加对检索图像目前这种方法我从数据库中检索图像1。而不是检索属于该官员的所有图像。

+0

发布一些代码,无论您尝试到目前为止。 – Pratik

+0

“我只知道如何将这些图像插入到数据库中” - 这意味着您将图像作为二进制插入到数据库中,还是将图像保存在文件系统中并将路径保存到数据库中? – kandroid

+0

以二进制形式将数据插入数据库 – XiAnG

回答

0

通常的做法是使用HttpHandler而不是ASPX页面本身。处理程序负责从数据库中检索图像数据并将其作为图形输出;它可以用作src的常规<img>标记或ImageUrl属性用于<asp:image>控件。

添加处理程序最简单的方法是在添加新项对话框中选择通用处理器: Add New Item

在处理程序的代码,的ProcessRequest是做的工作例如方法

public void ProcessRequest(HttpContext context) 
{ 
    byte[] imageBytes; 
    // Get the id of the image we want to show 
    string imageId = context.Request.QueryString["ImageId"]; 

    // Get the image bytes out of the database 
    using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
    { 
     // Pass in the image id we got from the querystring 
     SqlCommand cmd = new SqlCommand("SELECT image FROM PoliceMedal WHERE ImageId=" + imageId, conn); 
     conn.Open(); 
     SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
     reader.GetBytes(0, 0, imageBytes, 0, int.MaxValue); 
    } 

    context.Response.Buffer = true; 
    context.Response.Charset = ""; 
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    context.Response.ContentType = "image/jpg"; 
    context.Response.BinaryWrite(imageBytes); 
    context.Response.End(); 
} 

因此,我们有我们的处理程序,将返回数据库中的图像数据,我们称这种与像\ImageHandler.ashx?ImageId=1的URL。这确实需要对数据库进行更改,因为您需要向PoliceMedal表中添加密钥 - 我建议使用SQL Server IDENTITY列。

从你的问题中不清楚你是如何在ASPX页面上显示图像的,所以在这里我将告诉你如何将它们添加到PlaceHolder。因此,我们将检索一名警官的一组图像ID,为他们每个人构建一个<asp:image>控件,并将图像添加到PlaceHolder。

protected void Page_Load(object sender, EventArgs e) 
{ 
    using (
     SqlConnection conn = 
      new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
    { 
     // Query the table to get the list of image IDs for the current user 
     SqlCommand cmd = new SqlCommand("SELECT ImageId FROM PoliceMedal WHERE policeid = " + Session["policeid"], conn); 
     conn.Open(); 
     SqlDataReader imageReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

     // For each image in the database 
     while (imageReader.Read()) 
     { 
      // Create the new Image control 
      Image medalImage = new Image(); 
      // Call the handler, passing the id of the image in the querystring 
      medalImage.ImageUrl = string.Format("ImageHandler.ashx?ImageId={0}", 
               imageReader.GetInt32(0)); 
      // Add the image to the placeholder 
      MedalPlaceHolder.Controls.Add(medalImage); 
     } 
    } 
} 
相关问题