2011-02-17 44 views
1

我想从原件创建大小75x75平方米的缩略图。 缩略图在一个维度上看起来不会拉长,因为它不会遵循宽高比。如何生成图像的方形缩略图?

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

任何线索或帮助表示赞赏。

编辑:

我在.NET 4.0 C#

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

+0

添加标签的语言或技术所使用。看来你可能会要求一个图书馆使用。 – 2011-02-17 08:02:47

回答

4

这是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; 

}

0

ImageMagick是一个很好的库/图像转换/转换程序。

Resizing images

+0

ImageMagick体积庞大。我已经远离它了。它也很慢。 – kheya 2011-02-17 07:25:48

-1

如果你在windows上,你实际上可以用MSPaint做到这一点。

+0

需要编程方式。 – kheya 2011-02-17 07:27:50

0

如果您确定使用PHP,那么this将帮助您。