如何更快的缩略图在C# [英] How to thumbnail faster in c#

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

问题描述

我尽可能快地试图拇指的图像,无论资源的使用情况在我的ImageList和ListView使用,这是目前我怎么做的,但它似乎是缓慢的:

 公众形象toThumbs(字符串的文件,诠释的宽度,高度INT)
{
图像= NULL;
的aspectRatio = 1;
fullSizeImg = NULL;

{
fullSizeImg = Image.FromFile(文件);
浮动W = fullSizeImg.Width;
浮动H = fullSizeImg.Height;
的aspectRatio = W / H;

INT XP =宽度;
INT YP =高度;

如果(fullSizeImg.Width>宽度放大器;&安培; fullSizeImg.Height>高度)
{
如果((浮点)XP / YP>的aspectRatio)
{
XP =(INT)(YP *的aspectRatio);
}
,否则
{
YP =(INT)(XP /的aspectRatio);
}
}
,否则如果(fullSizeImg.Width = 0&安培;!&安培;!fullSizeImg.Height = 0)
{
XP = fullSizeImg.Width;
YP = fullSizeImg.Height;
}

图像=新位图(宽,高);
显卡= Graphics.FromImage(图像);
graphics.FillRectangle(Brushes.White,((宽 - XP)/ 2),(高度 - YP),XP,YP);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(fullSizeImg,新的Rectangle(((宽 - XP)/ 2),(高度 - YP),XP,YP));
graphics.Dispose();
fullSizeImg.Dispose();
}
赶上(例外)
{
图像= NULL;
}
返回图像;
}



我不知道如果计算是放慢一缩图或者正在使用的类本身是缓慢的,如果是这样的话,那么还有什么其他的选择可以使用可能是不同的计算或我需要导入其他类还是有可以使用的或者我需要一个第三方库做一个DLL导入什么?请帮我。




编辑:刚刚在这里找到一个解决方案
http://www.vbforums.com/showthread.php?t=342386
它提取从文件$ b缩略图$ b没有阅读整个文件。我能够减少40%左右,当我用这个时间。



解决方案

您的计算发生在一个几分之一秒。要将呼叫的DrawImage 最有可能是这个最慢的部分(如,一个是做缩放)。



如果你需要这个缩略图正是一次,然后我看不出有很大的提升空间在这里。如果你调用同一图像上的方法不止一次,你应该缓存缩略图。


I'm trying to thumb an image as fast as possible regardless of the usage of resources to be used in my ImageList and listview and this is currently how i'm doing it but it seems to be slow:

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;
        }

I'm not sure if the computation is the one that is slowing down the thumbnailing or maybe the classes itself that are being used are slow, if that is the case then what other alternatives can be use maybe a different computation or i need to import other classes or is there a third party libraries that can be used or i need to do a dll import or something? Please help me.

Edit: Just found a solution here http://www.vbforums.com/showthread.php?t=342386 it extracts a thumbnail from a file without reading the whole file. I was able to reduce the time about 40% when i used this.

解决方案

Your calculations happen in fractions of a second. The call to DrawImage is most likely the slowest part of this (as that one is doing the scaling).

If you're needing this thumbnail image exactly once then I don't see much room for improvement here. If you're calling that method on the same image more than once, you should cache the thumbnails.

这篇关于如何更快的缩略图在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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