C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Resize Image With Aspect Ratio ?


Posted Date: 03 Jun 2008    Resource Type: Articles    Category: .NET Framework

Posted By: R O B I N       Member Level: Gold
Rating:     Points: 5



The sample code shows a C# method that resizes pictures and maintains their aspect ratio, optionally only if a maximum width is exceeded. The method works with both images that have a higher height than width and viceversa.


public void ResizeImage(string OrigFile, string NewFile, int NewWidth, int MaxHeight, bool ResizeIfWider)
{
System.Drawing.Image FullSizeImage = System.Drawing.Image.FromFile(OrigFile);
// Ensure the generated thumbnail is not being used by rotating it 360 degrees
FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

if (ResizeIfWider)
{
if (FullSizeImage.Width <= NewWidth)
{
NewWidth = FullSizeImage.Width;
}
}

int NewHeight = FullSizeImage.Height * NewWidth / FullSizeImage.Width;
if (NewHeight > MaxHeight) // Height resize if necessary
{
NewWidth = FullSizeImage.Width * MaxHeight / FullSizeImage.Height;
NewHeight = MaxHeight;
}

// Create the new image with the sizes we've calculated
System.Drawing.Image NewImage = FullSizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
FullSizeImage.Dispose();
NewImage.Save(NewFile);
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Resizing images  .  Resize images in c#  .  Resize image  .  Resize an image  .  Image resizing  .  Image manipulation  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Garbage Collcetion
Previous Resource: Difference between ADO.Net Dataset and ADO Recordset
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

efax

Contact Us    Privacy Policy    Terms Of Use