[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Perform binary contrast enhancement interactively in C#

[Perform binary contrast enhancement interactively in C#]

The example Perform binary contrast enhancement on an image in C# performs binary contrast enhancement by setting each pixel to black or white depending on whether the sum of its red, green, and blue component values is greater than 3 * 128 = 384. For images that are particularly dark or light, however, the result may be too dark or too light.

This example lets you use a scroll bar to determine what cutoff value to use in performing the adjustment. Whenever the program loads a new image or you adjust the scroll bar, the code calls the following PerformEnhancement method.

// Perform binary contrast enhancement. private void PerformContrastEnhancement() { if (picOriginal.Image == null) return; Cursor = Cursors.WaitCursor; // Perform contrast enhancement. Bitmap bm = new Bitmap(picOriginal.Image); BinaryContrast(bm, 3 * hscrCutoff.Value); // Display the result. picOriginal.Visible = true; if (picResult.Image != null) picResult.Image.Dispose(); picResult.Image = bm; picResult.Left = picOriginal.Right + 4; picResult.Visible = true; Cursor = Cursors.Default; }

The code creates a new Bitmap that is a copy of the original image. It then calls the BinaryContrast method (see the earlier post for a description of that method) to perform the binary contrast enhancement, passing it the value selected by the scroll bar. It then displays the result.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.