简体   繁体   中英

Correct way to get an image thumbnail?

What is the correct way to get the thumbnails of images when using C#? There must be some built-in system method for that, but I seem to be unable find it anywhere. Right now I'm using a workaround, but it seems to be much heavier on the computing side, as generating the thumbnails of 50 images, when using parallel processing takes about 1-1,5 seconds, and during that time, my CPU is 100% loaded. Not to mention that it builds up quite some garbage, which it later needs to collect. This is what my class currently looks like:

public class ImageData
{
    public const int THUMBNAIL_SIZE = 160;

    public string path;
    private Image _thumbnail;

    public string imageName {  get {  return Path.GetFileNameWithoutExtension(path); } }

    public string folder { get { return Path.GetDirectoryName(path); } }

    public Image image { get 
        { 
            try
            {
                using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    var memoryStream = new MemoryStream(reader.ReadBytes((int)stream.Length));
                    return new Bitmap(memoryStream);
                }
            } 
            catch (Exception e) { }
            return null; 
        } 
    }

    public Image thumbnail
    {
        get
        {
            if (_thumbnail == null)
                LoadThumbnail();
            return _thumbnail;
        }
    }

    public void LoadThumbnail()
    {
        if (_thumbnail != null) return;
        Image img = image;
        if (img == null) return;
        float ratio = (float)image.Width / (float)image.Height;
        int h = THUMBNAIL_SIZE;
        int w = THUMBNAIL_SIZE;
        if (ratio > 1)
            h = (int)(THUMBNAIL_SIZE / ratio);
        else
            w = (int)(THUMBNAIL_SIZE * ratio);
        _thumbnail = new Bitmap(image, w, h);
    }

I am saving up the thumbnail once generated, to save up some computing time later on. Meanwhile, I have an array of 50 elements, containing picture boxes, where I inject the thumbnails into.

Anyways... when I open a folder, containing images, my PC certainly doesn't use up 100% CPU for the thumbnails, so I am wondering what is the correct method to generate them.

Windows pregenerates the thumbnails and stores them in the thumbs.db -File (hidden) for later use.

So unless you either access the thumbs.db file and are fine with relying on it being available or cache the thumbnails yourself somewehere you always will have to render them in some way or another.

That being said, you can probably rely on whatever framework you are using for your UI to display them scaled down seeing as you load them into memory anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM