Image resizer control

Last post 07-01-2009 3:04 AM by ervipingupta. 1 replies.

Sort Posts:

  • Image resizer control

    04-11-2008, 6:38 AM
    • Member
      49 point Member
    • Ajes
    • Member since 10-18-2007, 6:58 AM
    • Posts 34

    Hey people,

     I have this Custom Server Control.

     

    "C#" Class="ShowImage" %>
    
    using System;
    using System.Web;
    using System.Data.Odbc;
    using System.Data;
    using System.IO;
    using System.Drawing;
    
    public class ShowImage : IHttpHandler
    {
        const int BUFFERSIZE = 1024;
    
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    
        public void ProcessRequest(HttpContext context)
        {
            Byte[] imgBytes = null;
            context.Response.ContentType = "image/jpeg";
            String url = Utils.Decrypt(HttpUtility.UrlDecode(context.Request.QueryString.ToString()));
            String[] arguments = url.Split(new char[] { '&' });
    
            String imagePath = arguments[0];
            String width = arguments[1];
            String height = arguments[2];
            String cacheKey = "image=" + imagePath+";width="+width+";height="+height;
            Object cachedImageBytes = context.Cache.Get(cacheKey);
    
            if (cachedImageBytes != null)
            {
                imgBytes = (Byte[])cachedImageBytes;
            }
            else
            {
                context.Response.BufferOutput = false;
                imgBytes = writeSingleImage(imagePath, Convert.ToInt32(width), Convert.ToInt32(height));
                
                try
                {
                    context.Cache.Insert(cacheKey, imgBytes, null, DateTime.Now.AddDays(14), System.Web.Caching.Cache.NoSlidingExpiration);
                }
                catch
                {
                    //just ignore, dont know what happens
                }
            }
    
            if (imgBytes != null)
            {
                context.Response.OutputStream.Write(imgBytes, 0, imgBytes.Length);
            }
            context.Response.End();
        }
    
        public Byte[] writeSingleImage(String ImagePath, int width, int height)
        {
            Byte[] returnImage = null;
            Bitmap origImage;
            MemoryStream m = new MemoryStream();
            String filePath;
    
            filePath = HttpContext.Current.Server.MapPath(ImagePath);
            if(!File.Exists(filePath))
            {
                filePath = HttpContext.Current.Server.MapPath("images/no_pix.jpg");
            }
         
            try
            {
                origImage = new Bitmap(filePath);
                origImage.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
                origImage.Dispose();
    
                returnImage = ImageUtils.resizeImage(m.GetBuffer(), width, height);
            }
            catch
            {   
            }
            return returnImage;
        }
    }

     Now this all work great, but i am using this a lot of places and are working on creating a Class Library with all the controls i often use. This way i can deploy a dll with changes. Does anybody know how i should create a Web Server Control in a class library from the above code. I have created a class that inherits System.Web.UI.WebControl.WebControl, but when i try to write a resized image to the outputstream, all the pages displays is unicode chars. I used this code to write the image to the stream.

    Page.Response.ContentType = "image/jpeg";
    Page.Response.OutputStream.Write(imgBytes, 0, imgBytes.Length);
     
    Any thoughts??
  • Re: Image resizer control

    07-01-2009, 3:04 AM
    • Member
      13 point Member
    • ervipingupta
    • Member since 11-10-2008, 2:28 AM
    • Posts 62

    Hi Friend,

    Can you told me how to resize  the Animated image ???

Page 1 of 1 (2 items)