473,670 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14101
On Sat, 13 Oct 2007 12:55:48 -0700, "Danny Ni" <dn**@yahoo.com wrote:
>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(F ileInfo jpeg) {
StringBuilder thumbName = new StringBuilder(j peg.FullName);
thumbName.Repla ce(m_targetExtn , m_thumbExtn);
if (File.Exists(th umbName.ToStrin g())) {
MessageBox.Show ("Warning: " + jpeg.Name + " thumbnail already
exists.",
"Thumbnail Batch",
MessageBoxButto ns.OK,
MessageBoxIcon. Warning);
return false;
} // end if

// Check not already a thumbnail
if (jpeg.Name.Cont ains(m_thumbExt n)) {
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.Heigh t * thumbSize / img.Width);
} // end if

Image thumb = img.GetThumbnai lImage(newWidth , newHeight, null,
(IntPtr)null);
thumb.Save(thum bName.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.com wrote in message
news:Ok******** ******@TK2MSFTN GP04.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...@coldm ail.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
3859
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
9442
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 ratio * Provide white (or other color) borders at the sides or the top/bottom The last point would be used to allow users to resize the panel to any ratio
2
19355
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 (height) x 780 (wide) So the "preferred size" would be 440 x 780
0
1997
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 single image (see below) but don't have a clue how to apply the same approach in a datalist, perhaps with a custom databinding expression (or what?). I would also prefer not displaying the image if it's blank so being able to
0
1678
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 the controls resize and text also resizes to keep the aspect ratio. In my current form (VB.Net 2005) when I stretch a form the controls which are docked will resize with the form, but the text stays the same size. Is there a way to make a...
5
3482
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 is convenient for resizing a form for different screen resolutions. I can't see how to do this /control this in .Net (VS2005). I have a user that uses 800x600 resolution, and my apps appear very large on this user's workstation. How can I...
0
2101
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
5435
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 = new PrintDocument(); doc.PrintPage += this.Doc_PrintPage; PrintDialog dlgSettings = new PrintDialog();
3
4289
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 the edges (left, right, top, bottom) works just fine, but I'm running into a problem when I get to the corners. I'm using Camtasia's screen recorder as my reference. In that program, when you lock the aspect ratio and resize by the corners it...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8660
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7415
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4209
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1792
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.