c# - What is the best way to adjust Contrast level on an Image as found in Photoshop -


Do you think the following is best for adjusting the contrast level? In fact, I'd like to get the same / close paradigm for adjusting the opposite found in Photoshop.

  Public Static Bitmap Adjust Contrast (bitmap image, float value) {value = (100.0f + Value) / 100.0F; Value * = value; System.Drawing.Bitmap NewBitmap = Image; For (int x = 0; x & lt; NewBitmap.Width; ++ x) {for (int y = 0; y & lt; NewBitmap.Height; ++ y) {Color pixels = Newbitmap. Gate pixels (x, y); Float red = pixel R / 255.0F; Float Green = PixelG.G / 255.0F; Float blue = pixel.b / 255.0 f; Red = (((red - 0.5 F) * value) + 0.5 F) * 255.0 F; Green = ((Green - 0.5 F) * Value) + 0.5 F) * 255.0 F; Blue = (((blue - 0.5 F) * value) + 0.5 F) * 255.0 F; NewBitmap.SetPixel (x, y, Color.FromArgb (clamp ((int) red, 255, 0), clamp (int) green, 255, 0), clamp ((int) blue, 255, 0))); }} Return NewbieMap; } Public Static T Clamp & lt; T & gt; (T-value, T max, T-min) where T: System. {If (Value.CompareTo (Max)> 0) Max Return; If (value. Resolve (minimum) & lt; 0) return minimum; return value; }  

The above code is not mine and I forgot the source code.

Actually, no. Get a pixel and set it is really slow, try something with this line it locks the pixel of bitmap in memory and then changes the field of memory with the contrast algorithm directly. This requires unsafe code, but if you want any kind of display out of your solution, then you should go the same way.

Alternatively, you can basically use the matrix manipulator to do the same thing. I do not particularly like using them because sometimes it is difficult to get the right effect, but they can be the easiest thing in your case. .


Comments