Asp – Not able to display an image in repeater control for [thumbnail images]

asp.net

i have imgae path in datatable[id, path] now this would a value like

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;

now these images are in size width*height (1018*768). now i need to convert these images into thumnail

caling the function

**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;
            }

        }

like this i am coverting into thumbnail image. but here i am saving the thumbnail image under the path F:\R&D\RD\RD\Images\thum.jpg .

so is there any way without saving the thumbnail in the disk and how to bind the new thumbnail image in the repeater control and i need to show image there. but if once user clicks on the thumnail image a orignal image should pop up.

if any one have did this functionlity any where let me know
working on this solution from past 2 days.
any help would be greatly appreciated

now after changing code
like this

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();
        }
    }


}

in repeater control i have added this line of code

','_blank','toolbar=no,menubar=no'))" > '/>

my thumbnail image is not displayed still but once i click my thumbnail image i am able to see the entire image in new pop up

Best Answer

You should use a Photo Image Handler to serve the pages from your disk to the clients. Then in the repeater control

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

The ideea is not to pass in the actual path/name of the file, but the ID of the item you wish to view. Then the handler will:

    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();
}
}
Related Topic