如何生成的图像的平方缩略图?

问题描述:

我想从原稿创建的大小75x75平方缩略图。 缩略图不会看拉伸在一维,因为它不会跟随宽高比。

I want to create thumbnails of size 75x75 square from originals. The thumbnail will not look stretched in one dimension as it will not follow the aspect ratio.

如果已经使用Flickr后,你会看到他们产生方缩略图。我需要同样的事情。

If have used Flickr, you will see they generate square thumbnails. I need the same thing.

任何线索或帮助是AP preciated。

Any clue or help is appreciated.

编辑:

我在.NET 4.0 C#

I am on .NET 4.0 C#

我要寻找编程的方式来产生竖起大拇指。批处理功能需要,如果没有可用的dll

I am looking for programmatic way to generate thumbs. Batch capability needed if no dll available.

这是从 $ C $的CProject

static System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
    nPercent = nPercentH;
    destX = System.Convert.ToInt16((Width -
                  (sourceWidth * nPercent)) / 2);
}
else
{
    nPercent = nPercentW;
    destY = System.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(imgPhoto.HorizontalResolution,
                 imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
    new Rectangle(destX, destY, destWidth, destHeight),
    new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
    GraphicsUnit.Pixel);

grPhoto.Dispose();
return bmPhoto;

}