Resize image and keeping aspect ratio

D

Danny Ni

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
 
R

rossum

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 ====
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top