locked
Thumbnail creation won't work.....Why??? RRS feed

  • Question

  • User-1618958993 posted

    Hi

    I am using a Datalist to Bind some ImageButton at runtime. I made the size smaller to show the image small but it's not a proper way to make thumbnail. Now I tried for that but no result found..... I am submitting the HTML and Codes to debug it. Please help me. It's an very urgent task for me..

     

    HTML Code : <asp:ImageButton runat="server" ImageUrl="http://mysite.com/demo/MakeThumbnail.aspx?file=fall800.jpg"/>

    Code Behind MakeThumbnail.aspx.cs

    public class MakeThumbnail : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            // getting the file name -- 

            string file = Request.QueryString["file"];

            // creating an image object, using the filename we just retrieved
            System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(file));

            // creating the actual thumbnail image
            System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

            // making a memory stream to work with the image bytes
            MemoryStream imageStream = new MemoryStream();

            // putting the image into the memory stream
            thumbnailImage.Save(imageStream, System.Drawing.Imaging.Imageformat.Jpeg);

            // making byte array the same size as the image
            byte[] imageContent = new Byte[imageStream.Length];

            // rewinding the memory stream
            imageStream.Position = 0;

            // loading the byte array with the image
            imageStream.Read(imageContent, 0, (int)imageStream.Length);

            // returning byte array to caller with image type
            Response.ContentType = "image/jpeg";
            Response.BinaryWrite(imageContent);
        }

        
    public bool ThumbnailCallback()
        {
            return true;
        }

       
    }

     

    Please correct it if there is any mistakes in codes...

    Wednesday, February 6, 2008 2:28 AM

All replies

  • User494352855 posted
    • GetThumbnailImage doesn't actually resize the images if there is already an embedded version... it will appear to have random problems... You need to use DrawImage() instead... but set the interpolation, smoothing, pixel offset, and copositing quality all to high.
    • Set the jpeg quality to 90 (codec params) for good results...
    • Youc an just use ms.WriteTo.... no need to have the byte[] buffer.
    • You also *must* have disk caching on this type of system if you use it on a live site... You can't resize these on the fly for every user's request... and ASP.NET's caching system won't cut it...


    I wrote a module to do image resizing already... The source code is included, so you can make any changes you want to.... But I think it already does everything you need... and it's well tested.

    http://nathanaeljones.com/products/asp-net-image-resizer/

    Monday, October 6, 2008 4:19 PM