Files
2014-12-05 22:12:51 +01:00

62 lines
2.1 KiB
C#

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Swarmops.Logic.Support
{
public class Imagery
{
public static Image Resize (Image sourceImage, int width, int height)
{
// Code copied from CodeProject, http://www.codeproject.com/KB/GDI-plus/imageresize.aspx
int sourceWidth = sourceImage.Width;
int sourceHeight = sourceImage.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = (width/(float) sourceWidth);
nPercentH = (height/(float) sourceHeight);
if (nPercentH > nPercentW) // modified: crop, don't pad
{
nPercent = nPercentH;
destX = Convert.ToInt16 ((width -
(sourceWidth*nPercent))/2);
}
else
{
nPercent = nPercentW;
destY = Convert.ToInt16 ((height -
(sourceHeight*nPercent))/2);
}
int destWidth = (int) (sourceWidth*nPercent);
int destHeight = (int) (sourceHeight*nPercent);
Bitmap bmPhoto = new Bitmap (width, height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution (sourceImage.HorizontalResolution,
sourceImage.VerticalResolution);
Graphics grPhoto = Graphics.FromImage (bmPhoto);
grPhoto.Clear (Color.Red);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage (sourceImage,
new Rectangle (destX, destY, destWidth, destHeight),
new Rectangle (sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
}
}