Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Resize image and keeping aspect ratio

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Danny Ni - 13 Oct 2007 20:55 GMT
Hi,

I am looking for a way to display images with different aspect ratio into
frames with fixed width and height, the problem is some images will look
distorted if they are forced into fixed frame due to differnt aspect ratio.
Some graphic designer suggests me to keep the aspect ratio of the original
graphic and pad the graphic with empty space to fit into the frame. One
example, the fixed frame is 100x60 and the image is 120x120, I would like to
resize the picture to 60x60 and pad the picture with 20 pixels on both left
and right.

Does anyone know where I can find code samples to do this?

TIA
rossum - 13 Oct 2007 22:37 GMT
>Hi,
>
[quoted text clipped - 10 lines]
>
>TIA

One of my programs does a similar task, it creates thumbnails to a
standard size, where the longest dimension of the thumbnail is fixed.
There is a certain amount of other code to check that it does not
create thumbnails of thumbnails (it is from a batch process that deals
with groups of files).  Careful with line wrap, this is cut and pasted
from my code and was not formatted for usenet.

rossum

// ==== Code Begin ====

const double thumbSize = 175.0;  // Longer dimension of thumbnails

/// <summary>
/// Creates a thumbnail of an image file in the same directory.
/// </summary>
/// <param name="jpeg">The image file from which to create the
thumbnail.</param>
/// <returns>True if a thumbnail was created, false
otherwise.</returns>
private bool makeThumbnail(FileInfo jpeg) {
   StringBuilder thumbName = new StringBuilder(jpeg.FullName);
   thumbName.Replace(m_targetExtn, m_thumbExtn);
   if (File.Exists(thumbName.ToString())) {
       MessageBox.Show("Warning: " + jpeg.Name + " thumbnail already
exists.",
                       "Thumbnail Batch",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Warning);
       return false;
   } // end if

   // Check not already a thumbnail
   if (jpeg.Name.Contains(m_thumbExtn)) {
       return false;
   } // end if
   Image img = Image.FromFile(jpeg.FullName);
   if (img.Height <= thumbSize || img.Width <= thumbSize) {
       img.Dispose();
       return false;
   } // end if
   
   // Scale the thumbnail
   int newWidth, newHeight;
   if (img.Height > img.Width) {
       newHeight = (int)thumbSize;
       newWidth = (int)(img.Width * thumbSize / img.Height);
   }
   else {
       newWidth = (int)thumbSize;
       newHeight = (int)(img.Height * thumbSize / img.Width);
   } // end if

   Image thumb = img.GetThumbnailImage(newWidth, newHeight, null,
(IntPtr)null);
   thumb.Save(thumbName.ToString());
   img.Dispose();
   thumb.Dispose();
   return true;

} // end makeThumbnail()

// ==== Code End ====
Andrew Thompson - 15 Oct 2007 11:51 GMT
...
> ...Careful with line wrap, this is cut and pasted
> from my code and was not formatted for usenet.

A tool designed to help with that.
<http://www.physci.org/twc.jnlp>

Andrew T.
Andrew P. - 14 Oct 2007 00:40 GMT
Here's a ready to use method I wrote for resizing pictures and maintaing
their aspect ratio:
http://www.geekpedia.com/code7_Method-for-resizing-pictures-with-aspect-ratio.html

It works with various formats.

Good luck!
Andrew

> Hi,
>
[quoted text clipped - 10 lines]
>
> TIA

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.