Introduction:
Microsoft's new. NET platform has brought many new programming mechanisms, such as GDI + and Globalization, to developers. It also invented a completely new Java-like programming language-C #. For this new knowledge, we should understand, master, and try to apply it to practical projects as soon as possible, and the method of learning through instances is undoubtedly a very effective way. This article uses a simple example to show you how to use technologies such as GDI + and Unsafe code in Visual C # To implement simple digital image processing.
I. Overview:
The example in this article is a digital image processing application. Its functions include image color flip, grayscale processing, and brightening. The code of this program for image processing is included in a specialized Filters class. by calling the static member function in this class, we can implement the corresponding image processing function. To achieve image processing, we need to process images one by one in pixels. We know that an image is composed of pixel points. Each pixel of an image is processed accordingly, and the entire image is processed. In this process, we only need to process each pixel point, but we do not need to consider the influence of the surrounding pixel points during the processing process, therefore, the implementation of the program is much simpler.
Because the BitmapData class in GDI + does not provide direct access to the internal data of the image, our only method is to use pointers to obtain the internal data of the image, in this case, we need to use the keyword "unsafe" to specify the code block for the function to access the internal data of the image. In the program, I also used options such as opening files and saving files, so that our hard work will not go far.
Ii. Program Implementation:
1. Open Visual Studio.net, create a Visual C # project, and select "Windows application" in the template. The project name can be customized (ImageProcessor here ).
2. to enable the form to display images, we need to reload the OnPaint () event function of the form. In this function, we draw an image on the main form of the program, to enable the form to display images of different sizes, we also set the AutoScroll attribute of the form to true. In this way, the corresponding scroll bar appears on both sides of the form based on the image size. The function is implemented as follows: