locked
Create thumbnail and keep aspect ratio RRS feed

  • Question

  • User-935705407 posted

    Could someone tell me how to create a thumbnail 200 x 200 px out of an original image and at the same time keep the aspect ratio by cropping the image?

    Thanks a lot!

    Wednesday, September 9, 2009 11:35 AM

Answers

  • User653228039 posted

     These are my resize functions which works pretty well:

    protected bool ResizeAbort()
    {
    	errMsg.Text += "Error creating thumb.<br/>";
    	return false;
    }
    	
    protected void CreateResizedCopy(String sourcePath, String uploadPath, String filePath, int width, int height)
    {
    	if(!Directory.Exists(Server.MapPath(uploadPath)))
    	{
    		Directory.CreateDirectory(Server.MapPath(uploadPath));
    	}
    	if(!File.Exists(Server.MapPath(uploadPath+filePath)))
    	{
    		System.Drawing.Image.GetThumbnailImageAbort dummyCallback = new System.Drawing.Image.GetThumbnailImageAbort(ResizeAbort);
    		System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(sourcePath+filePath));
    		System.Drawing.Image thumbImg = fullSizeImg.GetThumbnailImage(width, height, dummyCallback, IntPtr.Zero);
    		thumbImg.Save(Server.MapPath(uploadPath+filePath));
    	}
    }
    

    We need to look at your question a little more though.  You cannot force a thumbnail of 200 X 200 and get the same aspect ratio.  Instead, the logic needs to know if width or height is more important (perhaps for your layout) then it can find a width/height that is as close to your suggested width/height as possible.

    With a little algebra, lets say your image is X wide and Y high.  Let's also say that width is most important to you for your layout, that means we need to solve for the new height (Z):

    X/Y = 200/Z

    ZX/Y = 200

    ZX = 200Y

    Z = 200Y/X

    We can use that formula with what we know about the source file.  so inside the "CreateResizedCopy" function, we apply that formula to find the new height:

    protected void CreateResizedCopy(String sourcePath, String destinationPath, String filename, int width)
    {
    	if(!Directory.Exists(Server.MapPath(destinationPath)))
    	{
    		Directory.CreateDirectory(Server.MapPath(destinationPath));
    	}
    	if(!File.Exists(Server.MapPath(destinationPath+filename)))
    	{
      		System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(Server.MapPath(sourcePath+filename));
      		int X= sourceImage .Width;
      		int Y = sourceImage .Height;
      		int height = (int)((width*Y)/X);
    		System.Drawing.Image.GetThumbnailImageAbort dummyCallback = new System.Drawing.Image.GetThumbnailImageAbort(ResizeAbort);
    		System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(sourcePath+filename));
    		System.Drawing.Image thumbImg = fullSizeImg.GetThumbnailImage(width, height, dummyCallback, IntPtr.Zero);
    		thumbImg.Save(Server.MapPath(destinationPath+filename));
    	}
    }
    


     

    Or something like that

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 9, 2009 12:55 PM

All replies

  • User653228039 posted

     These are my resize functions which works pretty well:

    protected bool ResizeAbort()
    {
    	errMsg.Text += "Error creating thumb.<br/>";
    	return false;
    }
    	
    protected void CreateResizedCopy(String sourcePath, String uploadPath, String filePath, int width, int height)
    {
    	if(!Directory.Exists(Server.MapPath(uploadPath)))
    	{
    		Directory.CreateDirectory(Server.MapPath(uploadPath));
    	}
    	if(!File.Exists(Server.MapPath(uploadPath+filePath)))
    	{
    		System.Drawing.Image.GetThumbnailImageAbort dummyCallback = new System.Drawing.Image.GetThumbnailImageAbort(ResizeAbort);
    		System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(sourcePath+filePath));
    		System.Drawing.Image thumbImg = fullSizeImg.GetThumbnailImage(width, height, dummyCallback, IntPtr.Zero);
    		thumbImg.Save(Server.MapPath(uploadPath+filePath));
    	}
    }
    

    We need to look at your question a little more though.  You cannot force a thumbnail of 200 X 200 and get the same aspect ratio.  Instead, the logic needs to know if width or height is more important (perhaps for your layout) then it can find a width/height that is as close to your suggested width/height as possible.

    With a little algebra, lets say your image is X wide and Y high.  Let's also say that width is most important to you for your layout, that means we need to solve for the new height (Z):

    X/Y = 200/Z

    ZX/Y = 200

    ZX = 200Y

    Z = 200Y/X

    We can use that formula with what we know about the source file.  so inside the "CreateResizedCopy" function, we apply that formula to find the new height:

    protected void CreateResizedCopy(String sourcePath, String destinationPath, String filename, int width)
    {
    	if(!Directory.Exists(Server.MapPath(destinationPath)))
    	{
    		Directory.CreateDirectory(Server.MapPath(destinationPath));
    	}
    	if(!File.Exists(Server.MapPath(destinationPath+filename)))
    	{
      		System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(Server.MapPath(sourcePath+filename));
      		int X= sourceImage .Width;
      		int Y = sourceImage .Height;
      		int height = (int)((width*Y)/X);
    		System.Drawing.Image.GetThumbnailImageAbort dummyCallback = new System.Drawing.Image.GetThumbnailImageAbort(ResizeAbort);
    		System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(sourcePath+filename));
    		System.Drawing.Image thumbImg = fullSizeImg.GetThumbnailImage(width, height, dummyCallback, IntPtr.Zero);
    		thumbImg.Save(Server.MapPath(destinationPath+filename));
    	}
    }
    


     

    Or something like that

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 9, 2009 12:55 PM
  • User2130758966 posted

    This article shows you how to make a thumbnail generator that supports maintaining the ratio:

    But it looks like AceCorban has provided some pretty straightforward code that does what you need.


    Depends if you want to learn about the subject or just get it working :)

    Wednesday, September 9, 2009 1:05 PM
  • User653228039 posted

     Well, I can't guarantee it'll compile.  Tongue out

    It might take a little troubleshooting and tweaking, but that's the basic idea.

    Wednesday, September 9, 2009 1:12 PM
  • User2130758966 posted

    [:)]

    Wednesday, September 9, 2009 2:31 PM
  • User-935705407 posted

    Thanks a lot AceCorban!

    BTW: You are creating the original image two times, only with different names.

    Thursday, September 10, 2009 2:47 AM
  • User653228039 posted

     Is the duplicate the resized image?  In the app I used this for, I needed both because I was also applying a watermark to uploaded pictures.  There may still be some remnants of that code in there.

    Thursday, September 10, 2009 4:56 PM