如何生成的图像的平方缩略图? [英] How to generate square thumbnail of an image?

查看:153
本文介绍了如何生成的图像的平方缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从原稿创建的大小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;

}

这篇关于如何生成的图像的平方缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆