locked
Very very difficult question! - help needed RRS feed

  • Question

  • User-663248805 posted

    well i have this fileupload control and i upload an image and it should resize but it wont.

     it upload the image to my "upload" folder but the dimension of image the same like 1600 -1200 pixels :/

     

     

     protected void Button1_Click(object sender, EventArgs e)
            {
                string SavePath =Server.MapPath("~/upload/");
                string filename = FileUpload1.PostedFile.FileName;
                System.Drawing.Image img = System.Drawing.Image.FromFile(filename);
        
                System.Drawing.Image newimage = img.GetThumbnailImage(300,200,null,IntPtr.Zero);
    
                img.Dispose();
                newimage.Save(SavePath+"new"+FileUpload1.FileName);
    
    
            }
     
    Wednesday, August 6, 2008 1:51 PM

Answers

  • User-1845656902 posted

     You need to create a delegate for your Callback parameter. Try adding this:

     Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);

     System.Drawing.Image newimage = img.GetThumbnailImage(300 ,200 ,myCallback, IntPtr.Zero);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2008 2:43 PM
  • User653228039 posted

    I use pretty much the same code to create thumbs myself, though mine is a little different.  Perhaps it is in one of the differences?

    1).  Is a file saving in your upload as "new"+filename?  Can I assume you have already uploaded and saved the PostedFile to your upload folder first?

    2).  I have a dummyCallback to an abort method that I pass to the GetThumnailImage method (instead of null as in your example).  Though I'm not 100 percent familiar with this method yet.  Will it work with null for this parameter? 

    Just A guess).  Perhaps saving your newimage after disposing img is doing it?  Again, not 100% familiar with how all this works at the lower level, but when establishing an image from the GetThumbnailImage method does it save that data in the Image object before you save it, or merely a reference to the img object until you save it?  Try moving the dispose method after you save it maybe?

     Here's what mine looks like:

     

    protected bool ResizeAbort()
    {
    	errMsg.Text += "Error creating thumb.<br/>";
    	return false;
    }
    	
    protected void CreateThumb(String uploadPath, String filename)
    {
    	if(!Directory.Exists(uploadPath+"/thumbs/"))
    		Directory.CreateDirectory(Server.MapPath(uploadPath+"/thumbs/"));
    	if(!File.Exists(Server.MapPath(uploadPath+"/thumbs/"+filename)))
    	{
    		System.Drawing.Image.GetThumbnailImageAbort dummyCallback = new System.Drawing.Image.GetThumbnailImageAbort(ResizeAbort);
    		System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(uploadPath+"/"+filename));
    		System.Drawing.Image thumbImg = fullSizeImg.GetThumbnailImage(80, 60, dummyCallback, IntPtr.Zero);
    		thumbImg.Save(Request.MapPath(uploadPath+"/thumbs/" + filename));
    	}
    }
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2008 2:44 PM

All replies

  • User-1845656902 posted

     You need to create a delegate for your Callback parameter. Try adding this:

     Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);

     System.Drawing.Image newimage = img.GetThumbnailImage(300 ,200 ,myCallback, IntPtr.Zero);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2008 2:43 PM
  • User653228039 posted

    I use pretty much the same code to create thumbs myself, though mine is a little different.  Perhaps it is in one of the differences?

    1).  Is a file saving in your upload as "new"+filename?  Can I assume you have already uploaded and saved the PostedFile to your upload folder first?

    2).  I have a dummyCallback to an abort method that I pass to the GetThumnailImage method (instead of null as in your example).  Though I'm not 100 percent familiar with this method yet.  Will it work with null for this parameter? 

    Just A guess).  Perhaps saving your newimage after disposing img is doing it?  Again, not 100% familiar with how all this works at the lower level, but when establishing an image from the GetThumbnailImage method does it save that data in the Image object before you save it, or merely a reference to the img object until you save it?  Try moving the dispose method after you save it maybe?

     Here's what mine looks like:

     

    protected bool ResizeAbort()
    {
    	errMsg.Text += "Error creating thumb.<br/>";
    	return false;
    }
    	
    protected void CreateThumb(String uploadPath, String filename)
    {
    	if(!Directory.Exists(uploadPath+"/thumbs/"))
    		Directory.CreateDirectory(Server.MapPath(uploadPath+"/thumbs/"));
    	if(!File.Exists(Server.MapPath(uploadPath+"/thumbs/"+filename)))
    	{
    		System.Drawing.Image.GetThumbnailImageAbort dummyCallback = new System.Drawing.Image.GetThumbnailImageAbort(ResizeAbort);
    		System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(uploadPath+"/"+filename));
    		System.Drawing.Image thumbImg = fullSizeImg.GetThumbnailImage(80, 60, dummyCallback, IntPtr.Zero);
    		thumbImg.Save(Request.MapPath(uploadPath+"/thumbs/" + filename));
    	}
    }
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 6, 2008 2:44 PM
  • User-663248805 posted

    hi holf i tried your method but it does not work

     

            public bool ThumbnailCallback()
            {
                return false;
            }
    
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                string SavePath =Server.MapPath("~/upload/");
                string filename = FileUpload1.PostedFile.FileName;
                System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 
                System.Drawing.Image img = System.Drawing.Image.FromFile(filename);
               
        
                System.Drawing.Image newimage = img.GetThumbnailImage(300,200,null,IntPtr.Zero);
    
                img.Dispose();
                newimage.Save(SavePath+"new"+FileUpload1.FileName);
    
    
            }
     
    Wednesday, August 6, 2008 3:21 PM
  • User-663248805 posted

     

    thanks guys the problem solved, was my mistake :D:D

     

    i was creating it inside a web user control file thats why it was not working.

    Wednesday, August 6, 2008 3:28 PM