4. Why is reading a graph so slow?
Generally, you can use the following methods to read images:
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 );
The three or four methods are mainly used to obtain the original DIB bitmap from the Windows handle. They are often used to read resource images or GDI images. The most commonly used is 7, where 1 and 5 are similar, 2 and 6 are similar, and 7 is called using different parameters in Method 5 and 6. We can perform a simple performance test. Take a 8000*7000 TIF image, which is generally larger than MB. Call Method 7 with different parameters. The following result is displayed.
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 {
12 Stopwatch watch = New Stopwatch ();
13 Watch. Start ();
14 Filestream FS = New Filestream (image, filemode. Open, fileaccess. Read );
15 Image img = Image. fromstream (FS, False , True );
16 Console. writeline ( " Use ICM: {0}. Validate: {1}, elapsedticks: {2 }. " , False , True , Watch. elapsedticks );
17 Watch. Stop ();
18 FS. Close ();
19 }
20
21 {
22 Stopwatch watch = New Stopwatch ();
23 Watch. Start ();
24 Filestream FS = New Filestream (image, filemode. Open, fileaccess. Read );
25 Image img = Image. fromstream (FS, True , False );
26 Console. writeline ( " Use ICM: {0}. Validate: {1}, elapsedticks: {2 }. " , True , False , Watch. elapsedticks );
27 Watch. Stop ();
28 FS. Close ();
29 }
30
31 {
32 Stopwatch watch = New Stopwatch ();
33 Watch. Start ();
34 Filestream FS = New Filestream (image, filemode. Open, fileaccess. Read );
35 Image img = Image. fromstream (FS, False , False );
36 Console. writeline ( " Use ICM: {0}. Validate: {1}, elapsedticks: {2 }. " , False , False , Watch. elapsedticks );
37 Watch. Stop ();
38 FS. Close ();
39 }
Let's take a look at the execution 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 can see that the culprit is validate. Of course, validate is actually useful. There are various formats of images. There are many different la s for a single BMP, not to mention JPEG, PNG, and GIF. JPEG has the oldest generation by using discrete cosine transformation, and JPEG 2000 by using wavelet. PNG has no research, and I don't know what it is. GIF has a static and active GIF 98. Therefore, it is normal that a problem occurs in the subsequent data streams. It is required in many cases, but the resulting performance loss is huge. If you are sure that these photos are normal, and reading images requires good performance, such as obtaining thumbnails of all images in a directory, use the last method.
Here, we will mention that ICM. ICM is a concept in color management. Different devices of the same image may have different colors. For example, if an LCD display and a CRT display have the same image, the color may be 108,000 different. This is mainly because different devices can display different color spaces. Color Space is another topic worth studying. I will not talk about it here. For color research, you can write a thick book. This 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 on. Some image formats allow you to write the information of the ICM into a file, such as JPEG. Therefore, this parameter indicates whether to use the file-embedded ICM information or the device's default information.
In fact, 7th functions are only available after. NET 2.0. The slow reading of images after the 1.1 s was scolded by others, so a function was added in the new version to solve the problem. At that time, someone called Justin Rogers wrote a class named imagefast (http://weblogs.asp.net/justin_rogers/articles/131704.aspx), and he used InterOP to call the GDI + method, skipping the ICM and validation. After. NET 2.0, this problem was solved. This is an example of. NET Framework's imperfect encapsulation of GDI +.