Handler Problem

Last post 02-08-2007 2:01 PM by SheoNarayan. 5 replies.

Sort Posts:

  • Handler Problem

    02-08-2007, 9:01 AM
    • Member
      183 point Member
    • HoLLoW
    • Member since 05-15-2005, 1:27 AM
    • Un Mexicano En Brasil
    • Posts 86

    Hi all!... I have a problem with my handler... it resize an image on the fly... that works grate!... but the problem is that when i try to delete the image, in my control panel or ftp it gives me an error saying:

     

    Cannot delete the image because is being use by another process...

     

    first I thought it was a problem in the ftp or something like that, but after doing some stuff, I encounter with the problem, it was the Handler that didn't let the  image to be deleted, I put a <asp:Image /> without the handler and I tested and I didn't got any problems... any ideas how to resolve that problem...

     

    thanks...
     

    life is lost in dreaming, dreaming is lost in becoming
    -----------------------------------------------------------
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
  • Re: Handler Problem

    02-08-2007, 12:14 PM
    Answer
    • Member
      201 point Member
    • SheoNarayan
    • Member since 12-23-2004, 6:30 PM
    • Hyderabad
    • Posts 76

    I think, you have not closed or disposed the control form which you are creating the image.

     like FileStream fs = new FileStream("blac", "blac", blca.gkg)

    try
    { }
    Catch (Exception ee)
    { }
    Finally
    {
        fs.Close();
    }

    When you close it, it should work. 

    --
    Thanks with regards,
    Sheo Narayan
    http://www.dotnetfunda.com/profile/SheoNarayan.aspx

    Please "Mark as Answer", if this helped.
  • Re: Handler Problem

    02-08-2007, 12:40 PM
    Answer
    • Member
      183 point Member
    • HoLLoW
    • Member since 05-15-2005, 1:27 AM
    • Un Mexicano En Brasil
    • Posts 86
     
        private string _imagePath;
        private HttpRequest Request;
        private HttpResponse Response;
        private HttpServerUtility Server;
    
        public string ImagePath
        {
            get { return this._imagePath; }
            set { this._imagePath = value; }
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    
        public void ProcessRequest(HttpContext context)
        {
            this.Server = context.Server;
            this.Response = context.Response;
            this.Request = context.Request;
            this.RequestImage();
        }
    
        private void RequestImage()
        {
            string evento = this.Request.QueryString["evento"];
            int imageHeight = Convert.ToInt32(this.Request.QueryString["h"]);
            int imageWidth = Convert.ToInt32(this.Request.QueryString["w"]);
    
            if ((imageHeight <= 0) && (imageWidth <= 0)) 
            {
                imageWidth = 140;
                imageHeight = 110;
            }
    
            string Album = "";
            Album = PhotoManager.GetRandomPhoto(Convert.ToInt32(this.Request.QueryString["AlbumID"]));
            if (Album == String.Empty)
            {
                this.ImagePath = this.Server.HtmlDecode("~/eventos/nofotos.gif");
            }
            else
            {
                this.ImagePath = this.Server.HtmlDecode("~/eventos/" + Album);
            }
    
            this.Response.ContentType = this.ResponseType(this.ImagePath);
            string imagePath = GetImagePath(this.Request.RawUrl);
            Image thumbImage = null;
            Bitmap drawImage = null;
    
            thumbImage = Image.FromFile(HttpContext.Current.Server.MapPath(this.ImagePath));
    
            try
            {
                if (imageHeight > 0 && imageWidth > 0)
                {
                    drawImage = new Bitmap(thumbImage);
                    thumbImage = this.Resize(new Bitmap(drawImage), imageHeight, imageWidth);
                    drawImage.Dispose();
                }
                else
                {
                    thumbImage = new Bitmap(thumbImage);
                }
                
                thumbImage.Save(this.Response.OutputStream, this.ImageType(this.ImagePath));
                
            }
            catch { }
            finally
            {          
                thumbImage.Dispose();
            }
        }
    
        public static string GetImagePath(string rawUrl)
        {
            return rawUrl.Replace("&", "_").Replace("?", "_").Replace("/", "_").Replace("=", "_").Replace(".", "_");
        }
    
        private Bitmap Resize(Bitmap image, int height, int width)
        {
            Bitmap bmImage = new Bitmap(width, height);
            Graphics gpImage = Graphics.FromImage(bmImage);
            gpImage.CompositingQuality = CompositingQuality.GammaCorrected;
            gpImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gpImage.SmoothingMode = SmoothingMode.AntiAlias;
            gpImage.DrawImage(image, 0, 0, width, height);
            gpImage.Dispose();
            return bmImage;
        }
    
        private ImageFormat ImageType(string image_path)
        {
            switch (Path.GetExtension(image_path).ToLower())
            {
                case ".bmp":
                    return ImageFormat.Bmp;
                case ".gif":
                    return ImageFormat.Gif;
                case ".jpg":
                    return ImageFormat.Jpeg;
                case ".png":
                    return ImageFormat.Png;
                case ".tiff":
                    return ImageFormat.Tiff;
            }
            return null;
        }
    
        private string ResponseType(string image_path)
        {
            switch (Path.GetExtension(image_path).ToLower())
            {
                case ".bmp":
                    return "Image/bmp";
                case ".gif":
                    return "Image/gif";
                case ".jpg":
                    return "Image/jpeg";
                case ".png":
                    return "Image/png";
                case ".tiff":
                    return "Image/tiff";
            }
            return null;
        }
     

    well these is my code so if you see anything or anyone sees anything wrong please tell me... :D

     

     

    life is lost in dreaming, dreaming is lost in becoming
    -----------------------------------------------------------
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
  • Re: Handler Problem

    02-08-2007, 1:01 PM
    Answer
    • Member
      201 point Member
    • SheoNarayan
    • Member since 12-23-2004, 6:30 PM
    • Hyderabad
    • Posts 76

    Hi,

     I saw your code, it looks fine, Just have some questions and suggestions too.

    1. why you have returned false in IsReusable methods, generally we reuturn true, so the same handler can be used.

    2. In Resize method, you have graphics, bitmats etc object created, you must have come accross some errors while trying resizing the image or whole things sometimes, and that objects has not released that file till now, thats why you are getting this error. One solution might be just to restart the IIS. Also, I will suggest when you are dealing with file generation on the fly use Try catch finally block for each object you are creating. In this case you have not used try catch finally blocks in Resize methods. The possibilities are that at the time of testing you might have got any error and that objects didn't closed properly that is causing this error.

    Thanks 

     

     

    --
    Thanks with regards,
    Sheo Narayan
    http://www.dotnetfunda.com/profile/SheoNarayan.aspx

    Please "Mark as Answer", if this helped.
  • Re: Handler Problem

    02-08-2007, 1:08 PM
    Answer
    • Member
      183 point Member
    • HoLLoW
    • Member since 05-15-2005, 1:27 AM
    • Un Mexicano En Brasil
    • Posts 86

    Hi, well the IsReusable i put it to false when i read that the handler is put in memory, and for test to see if that was the error i forgot to put it back to true...

     I will try to restart IIS to see if the problem is that, and thanks for the suggestion to put some cod in error handling...


    Just one other thing... this error that I mention is only happening in my online server, i tested in my machine and that problem didn't show... In my machine I tested with Visual studio 2005 and with localhost IIS, and didn't gave me that error... If it helps those diagnostics please tell me what you think...  

    life is lost in dreaming, dreaming is lost in becoming
    -----------------------------------------------------------
    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
  • Re: Handler Problem

    02-08-2007, 2:01 PM
    Answer
    • Member
      201 point Member
    • SheoNarayan
    • Member since 12-23-2004, 6:30 PM
    • Hyderabad
    • Posts 76

    Hi,

    I am almost sure that when you will try sometimes later (after restarting IIS), you will be able to delete those files. The reason I already told you, its because that objects didn't closed and some error occured.

    Generally on server File creations error occurs when there is no writer permission.

     Hope you will be able to delete those files sometimes later.

    Thanks,  Bye.
     

    --
    Thanks with regards,
    Sheo Narayan
    http://www.dotnetfunda.com/profile/SheoNarayan.aspx

    Please "Mark as Answer", if this helped.
Page 1 of 1 (6 items)