DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Magic of Aspects: How AOP Works in Spring
  • A Guide to Aspect-Based Sentiment Analysis With GPT and BERT
  • Adding Versatility to Java Logging Aspect
  • Role of C# in Building Dynamic and Secure Web Applications

Trending

  • Optimizing Databricks Spark Pipelines Using Declarative Patterns
  • Design Patterns for GenAI Creative Systems in Advertising
  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  • Building a Skill-Based Agentic Reviewer with Claude Code: A Practical Guide Using Skills.MD, MCP Servers, Tools, and Tasks

C#: Resize An Image While Maintaining Aspect Ratio And Maximum Height

By 
Arthur Chaparyan user avatar
Arthur Chaparyan
·
Jul. 19, 07 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
22.4K Views

Join the DZone community and get the full member experience.

Join For Free

public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

if (OnlyResizeIfWider)
{
if (FullsizeImage.Width  MaxHeight)
{
// Resize with height instead
NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
NewHeight = MaxHeight;
}

System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();

// Save resized picture
NewImage.Save(NewFile);
}


csharp Aspect (computer programming)

Opinions expressed by DZone contributors are their own.

Related

  • Magic of Aspects: How AOP Works in Spring
  • A Guide to Aspect-Based Sentiment Analysis With GPT and BERT
  • Adding Versatility to Java Logging Aspect
  • Role of C# in Building Dynamic and Secure Web Applications

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook