4. Why read a picture so slow?
In general, there are several ways to read a diagram:
1 public static Image FromFile(string filename);
2 public static Image FromFile(string filename, bool useEmbeddedColorManagement);
3 public static Bitmap FromHbitmap(IntPtr hbitmap);
4 public static Bitmap FromHbitmap (IntPtr hbitmap, IntPtr hpalette);
5 public static Image FromStream(Stream stream);
6 public static Image FromStream(Stream stream, bool useEmbeddedColorManagement);
7 public static Image FromStream(Stream stream, bool useEmbeddedColorManagement, bool validateImageData);
Of these, 3, 42 methods are mainly used to get the original DIB from the Windows handle bitmap, often used when you need to read a resource image, or a GDI image. The most common uses are 1,2 and 5,6,7, where 1 and 5 are similar, 2 and 6 are similar, and methods 5,6 use different parameters to invoke 7. We can do a simple performance test. Take a 8000*7000 large TIF image, such a general size of the image is more than 100M, with different parameters called Method 7, see the following results.
1 {
2 Stopwatch watch = new stopwatch ();
3 Watch. Start ();
4 FileStream fs = new FileStream (image, FileMode.Open, FileAccess.Read);
5 Image img = Image.fromstream (FS, True, true);
6 Console.WriteLine ("Use ICM: {0}. Validate: {1}, elapsedticks:{2}.", True, true, watch. Elapsedticks);
7 Watch. Stop ();
8 fs. Close ();
9}
10
11 {
Stopwatch watch = new stopwatch ();
Watch. Start ();
FileStream fs = new FileStream (image, FileMode.Open, FileAccess.Read);
Image img = Image.fromstream (FS, False, True);
Console.WriteLine ("Use ICM: {0}. Validate: {1}, elapsedticks:{2}.", False, true, watch. Elapsedticks);
Watch. Stop ();
FS. Close ();
19}
20
21 {
Stopwatch watch = new stopwatch ();
Watch. Start ();
FileStream fs = new FileStream (image, FileMode.Open, FileAccess.Read);
Image img = Image.fromstream (FS, True, false);
Console.WriteLine ("Use ICM: {0}. Validate: {1}, elapsedticks:{2}.", True, false, watch. Elapsedticks);
Watch. Stop ();
FS. Close ();
29}
30
31 {
Stopwatch watch = new stopwatch ();
Watch. Start ();
FileStream fs = new FileStream (image, FileMode.Open, FileAccess.Read);
Image img = Image.fromstream (FS, False, False);
Console.WriteLine ("Use ICM: {0}. Validate: {1}, Elapsedticks: {2}.", False, false, watch. Elapsedticks);
Notoginseng Watch. Stop ();
FS. Close ();
39}
Let's take a look at the implementation results:
Use ICM: True. Validate: True, ElapsedTicks:51853544.
Use ICM: False. Validate: True, ElapsedTicks:52507953.
Use ICM: True. Validate: False, ElapsedTicks:6880.
Use ICM: False. Validate: False, ElapsedTicks:5187.
So you see, the culprit is validate. Of course, validate is useful, the format of various images, on the single BMP there are many different ways to put, not to mention JPEG, PNG, GIF. JPEG has a common use of discrete cosine transform generated by the earliest, but also useful wavelet generated JPEG 2000. PNG has nothing to do with research, and I don't know what's inside. GIF has static and active GIF 98. So the data outflow problem is a normal thing, verification in many cases is required, but the resulting loss of performance is very large. If you are sure that these photos are generally not wrong and that you need good performance for reading graphs, such as getting thumbnails of all the images in a directory, use the last method.
Here's another mention of ICM. ICM is a concept in color management, the color may be different for different devices of the same image. For example, a liquid crystal display and a CRT display, first a picture of the same color may be 108,000 of the difference. This is mainly because different devices can display different color space. Color space is a topic worthy of study, there is no more to say. The study of color can write a very thick book. The world's most authoritative website http://www.color.org/can tell you a lot of useful information and mathematical formulas, including Gamma, the concept of color density, and so forth. Some image formats can be used to write ICM information in a file, such as JPEG, so this argument is whether to use the ICM information embedded in the file or the device default information.
In fact, the 7th function appears only after. NET 2.0. 1.1 After coming out of the slow reading of the problem by others scolded, so the new version added a function to solve the problem. There was a guy named Justin Rogers who wrote a class called Imagefast (Http://weblogs.asp.net/justin_rogers/articles/131704.aspx) and called GDI + with Interop. Method, skipped ICM and validation. After. NET 2.0, the problem was solved. This is an example of the. NET Framework's imperfect encapsulation of GDI +.