我正在尝试以最快的速度拇指图像,而不考虑在我的ImageList和listview中使用的资源,目前我是这样做的,但它似乎很慢:
public Image toThumbs(string file, int width, int height)
{
image = null;
aspectRatio = 1;
fullSizeImg = null;
try
{
fullSizeImg = Image.FromFile(file);
float w = fullSizeImg.Width;
float h = fullSizeImg.Height;
aspectRatio = w / h;
int xp = width;
int yp = height;
if (fullSizeImg.Width > width && fullSizeImg.Height > height)
{
if ((float)xp / yp > aspectRatio)
{
xp = (int)(yp * aspectRatio);
}
else
{
yp = (int)(xp / aspectRatio);
}
}
else if (fullSizeImg.Width != 0 && fullSizeImg.Height != 0)
{
xp = fullSizeImg.Width;
yp = fullSizeImg.Height;
}
image = new Bitmap(width, height);
graphics = Graphics.FromImage(image);
graphics.FillRectangle(Brushes.White, ((width - xp) / 2), (height - yp), xp, yp);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(fullSizeImg, new Rectangle(((width - xp) / 2), (height - yp), xp, yp));
graphics.Dispose();
fullSizeImg.Dispose();
}
catch (Exception)
{
image = null;
}
return image;
}
我不确定是计算导致缩略图变慢,还是使用的类本身很慢。如果是后者,那么有哪些其他替代方案可以使用?也许可以使用不同的计算方法,或者导入其他类,或者使用第三方库,或者进行dll导入等等。请帮忙解决。
编辑:在这里找到了一个解决方案http://www.vbforums.com/showthread.php?t=342386,它可以从文件中提取缩略图而无需读取整个文件。当我使用它时,能够将时间缩短约40%。