无法在[缩略图图像]的转发器控件中显示图像

时间:2009-11-14 04:31:07

标签: asp.net

我在datatable [id,path]中有imgae路径,现在这将是一个类似

的值
ex: id   path
    1    F:\R&D\RD\RD\Images\a1.JPG;
    2    F:\R&D\RD\RD\Images\a2.JPG;
    3    F:\R&D\RD\RD\Images\a3.JPG;

现在这些图像的大小宽*高(1018 * 768)。现在我需要将这些图像转换为thumnail

调用函数

**C_Thumbnails(100, "F:\R&D\RD\RD\Images\a1.JPG", "F:\R&D\RD\RD\Images]thum.jpg")**
public static void C_Thumbnails(int size, string FilePath, string ThumbPath)
        {

            System.Drawing.Image image = System.Drawing.Image.FromFile(FilePath);

            try
            {
                int thumbHeight, thumbWidth;

                decimal h = image.Height;

                decimal w = image.Width;

                if (image.Height > image.Width)
                {

                    thumbHeight = size;

                    decimal tWidth = (w / h) * thumbHeight;

                    thumbWidth = Convert.ToInt32(tWidth);

                }

                else
                {

                    thumbWidth = size;

                    decimal tHeight = (h / w) * thumbWidth;

                    thumbHeight = Convert.ToInt32(tHeight);

                }
                System.Drawing.Image thumbnailImage = image.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
                image.Dispose();
                thumbnailImage.Save(ThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);

            }

            catch (Exception ex)
            {
                image.Dispose();
                throw ex;
            }

        }
像这样,我正在转变为缩略图。但在这里我将缩略图保存在路径 F:\ R& D \ RD \ RD \ Images \ thum.jpg 下。

所以有没有办法保存磁盘中的缩略图以及如何在转发器控件中绑定新的缩略图图像,我需要在那里显示图像。但如果一旦用户点击了thumnail图像,就会弹出一个orignal图像。

如果有人在任何让我知道的地方做了这个功能   从过去2天开始研究这个解决方案。  任何帮助将不胜感激

更改代码后

像这样

public void ProcessRequest(HttpContext context)
    {
        string imageid = context.Request.Params["ImageID"];
        string thumbnail = context.Request.Params["thumbnail"];
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
        connection.Open();
        SqlCommand command = new SqlCommand("select Image from Image where ImageID=" + imageid, connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();

        string filePath = dr["image"].ToString();
        dr.Close();

        if (!System.IO.File.Exists(filePath))
        {
            //you have a problem 
            return;
        }
        if (context.Request.Params["thumbnail"] == "true")
        {
            //TODO: the thumbnail
            // Image thumbnailImage = originalImage.GetThumbnailImage to generate thumbnail then
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "image/" + format;
            thumbnailImage.Save(Response.OutputStream, imageFormat);
            thumbnailImage.Dispose();

        }
        else
        { //stream directly the image fromdisk
            System.IO.FileStream fs = System.IO.File.OpenRead(filepath);
            const int ChunkSize = 10000;
            Byte[] buffer = new Byte[ChunkSize];
            long dataLengthToRead = fs.Length;

            while (dataLengthToRead > 0)
            {
                int lengthRead = fs.Read(buffer, 0, ChunkSize);
                Response.OutputStream.Write(buffer, 0, lengthRead);
                System.Web.HttpContext.Current.Response.Flush();
                dataLengthToRead = dataLengthToRead - lengthRead;
            }
            fs.Close();
        }
    }


}

在转发器控制中我添加了这行代码                       ','_ blank','toolbar = no,menubar = no'))“>'/>                      我的缩略图图像仍然没有显示,但是一旦我点击我的缩略图,我就可以在新的弹出窗口中看到整个图像

1 个答案:

答案 0 :(得分:1)

您应该使用Photo Image Handler来提供从磁盘到客户端的页面。 然后在转发器控件中

  <a href="ImageHandler.ashx?thumbnail=false&id='<%# Eval("ID")%>'>
       <img src="ImageHandler.ashx?thumbnail=true&id='<%# Eval("ID")%>' border='0'/>
  </a>

ideea不会传入文件的实际路径/名称,而是传递要查看的项目的ID。然后处理程序将:

    public void ProcessRequest(System.Web.HttpContext context)
    {
         string filePath = //TODO: Get File Path from ItemID = context.Request.Params["id"]
         if (!System.IO.File.Exists(filePath))
         { 
             //you have a problem 
             return;
         }
         if(context.Request.Params["thumbnail"]=="true")
         {
             //TODO: the thumbnail
             // Image thumbnailImage = originalImage.GetThumbnailImage to generate thumbnail then
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "image/" + format;
            thumbnailImage.Save(Response.OutputStream, imageFormat);
            thumbnailImage.Dispose();

         } else 
         {  //stream directly the image fromdisk
           System.IO.FileStream fs = System.IO.File.OpenRead(filepath);
            const int ChunkSize = 10000;
            Byte[] buffer = new Byte[ChunkSize];
            long dataLengthToRead = fs.Length;

            while (dataLengthToRead > 0)
            {
                int lengthRead = fs.Read(buffer, 0, ChunkSize);
                Response.OutputStream.Write(buffer, 0, lengthRead);
                System.Web.HttpContext.Current.Response.Flush();
                dataLengthToRead = dataLengthToRead - lengthRead;
            }
            fs.Close();
}
}