By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,271 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,271 IT Pros & Developers. It's quick & easy.

Resize image and keeping aspect ratio

P: n/a
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
Oct 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sat, 13 Oct 2007 12:55:48 -0700, "Danny Ni" <dn**@yahoo.comwrote:
>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
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 ====
Oct 13 '07 #2

P: n/a
Here's a ready to use method I wrote for resizing pictures and maintaing
their aspect ratio:
http://www.geekpedia.com/code7_Metho...ect-ratio.html

It works with various formats.

Good luck!
Andrew

"Danny Ni" <dn**@yahoo.comwrote in message
news:Ok**************@TK2MSFTNGP04.phx.gbl...
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

Oct 13 '07 #3

P: n/a
On Oct 14, 7:37 am, rossum <rossu...@coldmail.comwrote:
....
...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.

Oct 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.