Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Image Processing in C#
By Rick Leinecker

Rate This Article: Add This Article To:

Image Processing in C# - ' The sample program'
( Page 2 of 3 )

The ImageProcessingDemo Program

I wrote a simple program named ImageProcessingDemo to demonstrate these concepts. You can download the code here. When the program first runs, it displays a default image that can be found in the program directory as shown in the figure below. You can click the browse button if you want to load a different image.

There are only four operations that can be performed on an image: darken, lighten, restore, and invert. The button with the '<' character darkens the image a bit. The button with the '>' character lightens the image a bit. The button with the 'R' character restores the image to its original state. And finally the button with the 'I' character inverts the image.

Before jumping into the processing code, we need to talk about BMP headers and the data format. BMP files have two headers: the file header and the information header. The file header is the first 14 bytes of the BMP file and contains the string 'BM', a long specifying the size of the file in bytes, two reserved words that must have a value of 0, and finally the offset from the beginning of the file to where the actual image data is. The following chart shows the BMP file header.

Start Size Name Example Value Purpose
0 2 bfType 19778 or 'BM' Identifies the file type.
2 4 bfSize ?? Specifies the size of the file in bytes.
6 2 bfReserved1 0 Must always be set to zero.
8 2 bfReserved2 0 Must always be set to zero.
10 4 bfOffBits 54 Specifies the offset from the beginning of the file to the bitmap data.

The information header contains information about the bitmap including its size, width, height, and format. The following chart shows the BMP information header.

Start Size Name Example Value Purpose
14 4 biSize 40 Specifies the size of the BITMAPINFOHEADER structure, in bytes.
18 4 biWidth 100 Specifies the width of the image, in pixels.
22 4 biHeight 100 Specifies the height of the image, in pixels.
26 2 biPlanes 1 Specifies the number of planes of the target device, must be set to zero.
28 2 biBitCount 24 Specifies the number of bits per pixel.
30 4 biCompression 0 Specifies the type of compression, usually set to zero (no compression).
34 4 biSizeImage ?? Specifies the size of the image data, in bytes. If there is no compression, it is valid to set this member to zero.
38 4 biXPelsPerMeter 0 Specifies the horizontal pixels per meter on the designated targer device, usually set to zero.
42 4 biYPelsPerMeter 0 Specifies the vertical pixels per meter on the designated targer device, usually set to zero.
46 4 biClrUsed 0 Specifies the number of colors used in the bitmap, if set to zero the number of colors is calculated using the biBitCount member.
50 4 biClrImportant 0 Specifies the number of color that are 'important' for the bitmap, if set to zero, all colors are important.



 
 
>>> More Microsoft Languages Articles          >>> More By Rick Leinecker