Mega Code Archive

 
Categories / C# / 2D Graphics
 

Image Scale Isotropic

using System; using System.Drawing; using System.Windows.Forms;     class ImageScaleIsotropic: Form {      Image image = Image.FromFile("Color.jpg");          public static void Main()      {           Application.Run(new ImageScaleIsotropic());      }      public ImageScaleIsotropic()      {           ResizeRedraw = true;       }      protected override void OnPaint(PaintEventArgs pea)      {           DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);      }           protected void DoPage(Graphics grfx, Color clr, int cx, int cy)      {           Rectangle rect = new Rectangle(0, 0, cx, cy);                 SizeF sizef = new SizeF(image.Width / image.HorizontalResolution,                                   image.Height / image.VerticalResolution);               float fScale = Math.Min(rect.Width  / sizef.Width,                                   rect.Height / sizef.Height);               sizef.Width  *= fScale;           sizef.Height *= fScale;                      grfx.DrawImage(image, rect.X + (rect.Width  - sizef.Width ) / 2,                                 rect.Y + (rect.Height - sizef.Height) / 2,                                 sizef.Width, sizef.Height);      } }