473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Resize image and keeping aspect ratio

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
3 14046
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Arthur Hsu | last post by:
Hello, I have an ImageButton that refers to an external image. How can I keep that image's aspect ratio when I set the ImageButton's size to 120x120? TIA, Arthur
2
by: Carl Gilbert | last post by:
Hi I am looking for either a component or technique to allow me to do the following: * Provide a panel with a background image * Resize the image to best fit the panel to maintain aspect...
2
by: Farce Milverk | last post by:
Hi, I'm looking for an algorithm to resize an image of arbitrary size to a "fixed" / required width and height. For example, my application requires that images be no larger than 440 pixel...
0
by: mharness | last post by:
Hello All, Does anyone know how to dynamically change the width and height properties of an image in a datalist in order to maintain the original aspect ratio? I've managed to do this with a...
0
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, Is there a property or method or any code for controlling the aspect ratio of a form(s)? Example: In Java if you create a form with Panels... when you stretch/resize the form, all...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, If I create a form in Java with controls like Panels, textboxes... when I stretch/shrink the form, all the controls can grow/shrink - along with the text contained in the textboxes. This...
0
by: Martin Plotz | last post by:
Hi all, is it possible to say "I'd like a <divwith width:100% and an aspect ratio of height/width=2/3" in a way that browsers understand? Thanks for hints, Martin
2
by: alag20 | last post by:
Hi, How can I print an while preserving its aspect ratio. Currently I am using the code below. private void PrintClicked(object sender, EventArgs e) { PrintDocument doc =...
3
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I'm overriding the WM_SIZE message so that I can lock my form into a specific aspect ratio. I'm allowing the user to turn on and off aspect ratio locking by toggling a checkbox. So far resizing by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.