It is common to obtain or set up the brightness by exposing the button at LockBits~UnlockBits in C#, as using GetPixel or SetPixel method make the brightness value obtaining process slow. Today, I will compare the processing time by using different modes.

Testing Environment
OS Windows10 64bit ver.1809
CPU Intel Core i7-7700K 4.2GHz
Memory 32GB
.NET Framework 4.5.2
Platform Any CPU (32Bit prioritized)
Evaluation Image JPEG 6,000 x 4,000px 24bit Color

In this test, I will obtain the color image’s brightness value in R,G,B respectively and use the program, in which I reverse the intensity value and re-apply the brightness value of the color image.

  • I set up the process to repeat in the order of “bright→dark→bright→dark” as seen below.
  • Processing time : I measured the average time in processing 5 repetitions.

1. Using GetPixel, SetPixel

Copy to Clipboard

2. Obtaining pointer (Scan0) at LockBits~UnlockBits, copying the data, processing and re-copying the data based on the result

Copy to Clipboard

3. Directly read and write on the pointer(Scan0) with ReadByte, WriteByte in Marshal class

Copy to Clipboard

4. Directly read and write on the pointer(Scan0) using Unsafe

Copy to Clipboard

5. Read and write with pointer and parallelize the processing using Parallel.For

Copy to Clipboard

6. Program that brings back the processing

Copy to Clipboard

Summary

Processing time by different processing methods

It turns out that the processing time by different methods was different, according to the versions and Bit.
When operating with 32Bit, there was not much difference between .NET Framework 4.5.2 and 4.7.2, however, upon comparing 32bit and 64bit in the same .NET Frame work version, I could find that 64Bit had gained a relatively faster result.

  1. .NET Framework 4.5.2 – 32Bit

    Method Processing Time (ms)
    GetPixel, SetPixel 27,439
    Processing in consideration of arranging LockBits, UnlockBits 101
    ReadByte, WriteByte in Marshal class 273.6
    Processing by Unsafe pointer 25.4
    Parallel processing by Unsafe pointer 11.4
  2. .NET Framework 4.7.2 – 32Bit

    Method Processing Time (ms)
    GetPixel, SetPixel 27,293.4
    Processing in consideration of arranging LockBits, UnlockBits 101.8
    ReadByte, WriteByte in Marshal class 290.8
    Processing by Unsafe pointer 24.4
    Parallel processing by Unsafe pointer 9.8
  3. .NET Framework 4.7.2 – 64bit 
    Method Processing Time (ms)
    GetPixel, SetPixel 21,281.2
    Processing in consideration of arranging LockBits, UnlockBits 85.4
    ReadByte, WriteByte in Marshal class 213.6
    Processing by Unsafe pointer 27.2
    Parallel processing by Unsafe pointer 9.2

[Reference & Caution]

  • The contents of this blog are excerpted from the blog (Imaging Solution.net) posted by Mr. Akira Yaskawa, who is an engineer working for AVAL DATA. If you need any additional information, please refer to Mr. Akira Yaskawa’s blog directly.
  • We do not restrict your sharing or referring to this contents, however, using this contents commercially by copying, revising and reproducing without the author’s permission is prohibited.
  • We do not restrict your use of the contents, however, we are not liable for the occurrence of the bugs from your using the methods shown in this contents.