【问题标题】:How to generate square thumbnail of an image?如何生成图像的方形缩略图?
【发布时间】:2011-06-28 21:27:32
【问题描述】:

我想从原件创建大小为 75x75 正方形的缩略图。 缩略图不会在一维上被拉伸,因为它不会遵循纵横比。

如果使用过 Flickr,您会看到它们生成方形缩略图。我需要同样的东西。

感谢任何线索或帮助。

编辑:

我使用的是 .NET 4.0 C#

我正在寻找生成拇指的程序化方式。如果没有可用的 dll,则需要批处理功能。

【问题讨论】:

  • 为您使用的语言或技术添加标签。看来您可能要求使用库。

标签: c# .net thumbnails


【解决方案1】:

这是来自Codeproject

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;

}

【讨论】:

    【解决方案2】:

    如果您在 Windows 上,您实际上可以使用 MSPaint 执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2010-10-27
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多