Loading pictures in MP alot faster (1 Viewer)

ingig

Portal Member
September 4, 2006
9
0
Home Country
Iceland Iceland
I just downloaded the MP source code and just looking around in it. Hopefully create a plugin sometime soon.

I noticed one thing in the source code about how you load the images.

This is usually done this way in MP
Code:
using (Image img = Image.FromFile(fileName))
{
    //do stuff
}

What is a great way to load images, maybe 100 times faster, is this way

Code:
using (FileStream fs = new FileStream(fullName, FileMode.Open, FileAccess.Read))
{
	using (Image g = Image.FromStream(fs, true, false))
       {
            //do stuff
       }
}

The reason this is alot faster is that Image.FromFile(fileName) does some type of check on the image.

You can read more about it on this blog http://blogs.msdn.com/omars/archive/2004/03/29/100941.aspx
 

Inker

Retired Team Member
  • Premium Supporter
  • December 6, 2004
    2,055
    318
    Hmm, that looks like a great find, unless there is a reason for that check to be called.

    However, this seems to be for .net 1.1, so perhaps the libraries have already been enhanced to speed up imageloading. But maybe not (backwards compatibility?).

    I recommend you jump on IRC and talk to our devs about it, and since you seem to be a dev, MP is always looking for your type :)

    Inker

    Edit: I just did a quick test in .net 2.0 and wow, the difference is remarkable. I didn't actually do anything with the images though, I just created a bunch of image objects, but wow, it is tons! faster.
     

    ingig

    Portal Member
    September 4, 2006
    9
    0
    Home Country
    Iceland Iceland
    I've used this method for probably 2 years now on my webserver without any problems. When I implemented it, my cpu average when from 70% to about 20% (the server does a lot of thumbnail work)

    The cause according to microsoft is
    "This performance difference occurs because the System.Drawing.Image.FromStream method always calls an image validation routine on the loaded image."

    So my guess is that the method throws an exception if the object isn't an image, but not on this "new" method. But I guess that when you try something image related with the object you'll get an exception if it's not an image. So I don't think the difference is much and well worth it.

    I'm not much of an IRC man, but maybe I'll check it out.
     

    Nightmare77

    Portal Pro
    July 5, 2005
    531
    1
    Canberra
    Home Country
    this sounds really cool is this for all thumb nail views eg, dvd / movie coverart as well? would this be put into a SVN release or just wait until 0.3 ?
    Sounds sweet well done ingig
     

    scoop

    Retired Team Member
  • Premium Supporter
  • November 14, 2004
    614
    7
    Hi,

    First of all thanks for posting this tip.
    Today I've changed the most important parts in MP where this code appeared, but unfortunately there was no noticeable performance gain for MP so I decided not to put this change into SVN. I also expect the performance related issues with thumbs are not really related with loading/validating the image and this would only become beneficial when processing lots of images sequentially.

    Kind regards,
    Michel
     

    ingig

    Portal Member
    September 4, 2006
    9
    0
    Home Country
    Iceland Iceland
    scoop, I agreee with you. I replaced most of the code with what I had suggested and the performance is litle better or the same, the performance was pretty much the same(this was with about 60 images in one folder).

    One thing I don't get in the code is there is a thread sleeper for 1000 milliseconds, maybe you know better then I, but why is that?

    Still I thing the function that I provided is better, at least for those people that have realy many pictures.

    The reason that I think it's better is that, from my experiense it costs a lot less cpu power then Image.GetImage()
     

    mbuzina

    Retired Team Member
  • Premium Supporter
  • April 11, 2005
    2,839
    726
    Germany
    Home Country
    Germany Germany
    Another small post for Image speed:

    Why not use the Thumbs.db file windows creates? There is a real nice C# library around which shows how to use it (LGPL, but author also states: Do whatever you like with it). That might speed up things also (for thumbnail showing...).

    Hang on I will check the URL and post it in a second.
     

    Users who are viewing this thread

    Top Bottom