Click here to Skip to main content
15,885,244 members
Articles / Multimedia / GDI+

Image Processing Using Matrices in C#

Rate me:
Please Sign up or sign in to vote.
4.44/5 (34 votes)
6 Aug 2011CPOL4 min read 78.3K   11.8K   100   18
Image related operations done using C#.

scr1.jpg

Introduction

This is my eighth article in C#. Similar to my previous article, I got impressed from a similar article and I tried this.

Overview

The purpose of the article is to be able to build a class that allows any C# programmer to perform image processing functionality. The reason we are doing it in C# is that it is very flexible. We can see that the code becomes somewhat more complex when we start moving pixels or changing values based on calculations that take into account all the pixel values.

Application

The application is a basic Windows Forms application. I have handled the images with a separate class called CurrentImageHandler in which all the image related operations are done including saving and graphics related operations. The functionality includes getting image information, zooming, color filtering, brightening, contrasting, grayscale filtering, invert filtering, resizing with full resolution, rotating and flipping, cropping, and inserting text, any other image, or geometric shapes. The image related functionalities are handled in a separate class library called ImageFunctions.dll. Scrolling is achieved in the standard manner. The Paint method uses the AutoScrollPosition property to find out our scroll position, which is set by using the AutoScrollMinSize property.

1. Thumbnail View

scr0.jpg

C#
ThumbnailViewFrm tFrm = new ThumbnailViewFrm(currImgHandler);
tFrm.Show();

2. Color Filter

Color filters are sometimes classified according to their type of spectral absorption: short-wavelength pass, long-wavelength pass, or band-pass; diffuse or sharp-cutting; monochromatic or conversion. The short-wavelength pass transmits all wavelengths up to the specified one and then absorbs. The long-wavelength pass is the opposite. Every filter is a band-pass filter when considered generally.

It is very simple - it just adds or subtracts a value to each color. The most useful thing to do with this filter is to set two colors to -255 in order to strip them and see one color component of an image. For example, for red filter, keep the red component as it is and just subtract 255 from the green component and blue component.

scr0.jpgscr0.jpg
scr0.jpgscr0.jpg
C#
currImgHandler.CurrentFilterHandler.SetColorFilter(ColorFilterTypes.Red);
currImgHandler.CurrentFilterHandler.SetColorFilter(ColorFilterTypes.Green);
currImgHandler.CurrentFilterHandler.SetColorFilter(ColorFilterTypes.Blue);

3. Alpha Value

Alphaing images is sometimes needed, it's a personal choice. Sometimes printing needs a lighter image than viewing. It is done just by adjusting the color components as per the user requirements. The input ranges between 0 and 100.

scr0.jpgscr7.jpg
C#
AlphaFrm aFrm = new AlphaFrm();
aFrm.AlphaValue = 255;
if (aFrm.ShowDialog() == DialogResult.OK)
{
    currImgHandler.CurrentFilterHandler.SetAlphaFilter(aFrm.AlphaValue);
    this.Invalidate();
}

4. Brightness

Brightening images are sometimes needed, and again it's a personal choice. Sometimes printing needs a lighter image than viewing. It is done just by adjusting the color components as per the user requirements. The input ranges between -255 and 255.

scr0.jpgscr7.jpg
C#
BrightnessForm bFrm = new BrightnessForm();
bFrm.BrightnessValue = 0;
if (bFrm.ShowDialog() == DialogResult.OK)
{
    currImgHandler.CurrentBrightnessHandler.SetBrightness(bFrm.BrightnessValue);
    this.Invalidate();
}

5. Contrast

Contrasting of images is certainly a complex process. The color matrix formed gives a somewhat different combination which makes the input image contrast.

scr0.jpgscr8.jpg
C#
ContrastForm cFrm = new ContrastForm();
cFrm.ContrastValue = 0;
if (cFrm.ShowDialog() == DialogResult.OK)
{
    currImgHandler.CurrentContrastHandler.SetContrast(cFrm.ContrastValue);
    this.Invalidate();
}

5. Grayscale

Gray scale filtering is in reference to the color mode of a particular image. A gray scale image would, in layman's terms, be a black and white image, any other color would not be included in it.

Basically, it's a black and white image; the colors in that image, if any, will be converted to the corresponding shade of gray (mid tones between black and white) thus making each bit of the image still differentiable.

scr0.jpgscr9.jpg
C#
currImgHandler.CurrentGrayscaleHandler.SetGrayscale();

6. Sepia Tone

Sepia tone filtering is somewhat similar to grayscale filtering.

scr0.jpgscr10.jpg
C#
currImgHandler.CurrentSepiaToneHandler.SetSepiaTone();

7. Invert

This is so simple that it doesn't even matter that the color components are out of order. It is just taking the opposite color of the current component. That is, for example, if the color component is 00, then the opposite we get is FF (255-0).

scr0.jpgscr11.jpg
C#
currImgHandler.CurrentInvHandler.SetInversion();

8. Rotating and Flipping

Rotating or flipping is also referred to as creating the mirror of an image. Rotating in angles like 90, 180, 270 degrees, then flipping the image horizontally or vertically. And also rotating in a custom angle.

scr0.jpgscr12.jpg
C#
//rotation
currImgHandler.CurrentRotationHandler.Flip(RotateFlipType.Rotate90FlipNone);
currImgHandler.CurrentRotationHandler.Flip(RotateFlipType.Rotate180FlipNone);
currImgHandler.CurrentRotationHandler.Flip(RotateFlipType.Rotate270FlipNone);

//flipping
currImgHandler.CurrentRotationHandler.Flip(RotateFlipType.RotateNoneFlipX);
currImgHandler.CurrentRotationHandler.Flip(RotateFlipType.RotateNoneFlipY);

//Custom Rotation
RotateForm rFrm = new RotateForm();
rFrm.RotateAngle = 0;
if (rFrm.ShowDialog() == DialogResult.OK)
{
     currImgHandler.CurrentRotationHandler.Rotate(rFrm.RotateAngle);
     this.AutoScrollMinSize = 
          new Size(Convert.ToInt32(currImgHandler.CurrentBitmap.Width), 
          Convert.ToInt32(currImgHandler.CurrentBitmap.Height));
     this.Invalidate();
}

9. Inserting Text, Other Images, or Shapes

This is just including any required thing in the image. This is achieved using the Graphics object of the image.

scr0.jpgscr0.jpg
scr0.jpgscr0.jpg
C#
//Inserting Text
InsertTextForm itFrm = new InsertTextForm();
itFrm.DisplayTextPositionX = itFrm.DisplayTextPositionY = 0;
if (itFrm.ShowDialog() == DialogResult.OK)
{
     currImgHandler.CurrentTextInsHandler.Insert(itFrm.DisplayText, 
       itFrm.DisplayTextPositionX, itFrm.DisplayTextPositionY, 
       itFrm.DisplayTextFont, itFrm.DisplayTextFontSize, 
       itFrm.DisplayTextFontStyle, 
       itFrm.DisplayTextAngle, itFrm.DisplayTextOpacity, 
       itFrm.DisplayTextColor1, itFrm.DisplayTextColor2, 
       itFrm.DisplayTextGradientStyle);
     this.Invalidate();
}
//Inserting Image
InsertImageForm iiFrm = new InsertImageForm();
iiFrm.DisplayImagePositionX = iiFrm.DisplayImagePositionY = 0;
if (iiFrm.ShowDialog() == DialogResult.OK)
{
    currImgHandler.CurrentImgInsHandler.Insert(iiFrm.DisplayImagePath, 
      iiFrm.DisplayImagePositionX, iiFrm.DisplayImagePositionY, 
      iiFrm.DisplayImageWidth, iiFrm.DisplayImageHeight, 
      iiFrm.DisplayImageAngle, iiFrm.DisplayImageOpacity);
    this.Invalidate();
}
//Inserting Shapes
InsertShapeForm isFrm = new InsertShapeForm();
isFrm.DisplayShapePositionX = isFrm.DisplayShapePositionY = 0;
if (isFrm.ShowDialog() == DialogResult.OK)
{
    currImgHandler.CurrentShapeInsHandler.Insert(isFrm.DisplayShape, 
      isFrm.DisplayShapePositionX, isFrm.DisplayShapePositionY, 
      isFrm.DisplayShapeWidth, isFrm.DisplayShapeHeight, isFrm.DisplayShapeAngle, 
      isFrm.DisplayShapeOpacity, isFrm.DisplayShapeColor1, 
      isFrm.DisplayShapeColor2, isFrm.DisplayShapeGradientStyle);
    this.Invalidate();
}

Points of Interest

I have also included Undo Options, Clear Image, and Image Information, which are quiet simple. Also some other functionalities are similar to those in my previous article on image processing so I have not included them here. You can get them from the downloads.

See Also

Conclusion

Thanks for viewing this article. I expect feedback from you. You expect more from me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
AbnoanMuniz9-Jun-14 7:28
AbnoanMuniz9-Jun-14 7:28 
QuestionNice, but a small bug Pin
V.13-May-14 23:10
professionalV.13-May-14 23:10 
GeneralMy vote of 3 Pin
ritesh.chakravarti20-Oct-13 18:12
ritesh.chakravarti20-Oct-13 18:12 
Bugimpossible to save! Pin
Member 1017923330-Jul-13 7:05
Member 1017923330-Jul-13 7:05 
QuestionCustom rotation is incorrect Pin
neticous29-May-13 10:20
neticous29-May-13 10:20 
Questionimage converting in c sharp Pin
Tennyson Pic25-Feb-13 15:59
Tennyson Pic25-Feb-13 15:59 
AnswerRe: image converting in c sharp Pin
BillW3320-Jun-13 9:10
professionalBillW3320-Jun-13 9:10 
GeneralMy vote of 4 Pin
Sudesh Ryan29-Dec-12 17:52
Sudesh Ryan29-Dec-12 17:52 
QuestionMy vote of 5 Pin
S.B.3-Dec-12 10:32
S.B.3-Dec-12 10:32 
Questionout of memory Pin
akul12310-Apr-12 18:14
akul12310-Apr-12 18:14 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey20-Feb-12 21:38
professionalManoj Kumar Choubey20-Feb-12 21:38 
QuestionProblem in Asp.Net Pin
pep30cula4-Dec-11 7:46
pep30cula4-Dec-11 7:46 
AnswerRe: Problem in Asp.Net Pin
Saleth Prakash4-Dec-11 17:22
Saleth Prakash4-Dec-11 17:22 
GeneralMy vote of 5 Pin
rrossenbg4-Nov-11 13:34
rrossenbg4-Nov-11 13:34 
GeneralMy vote of 5 Pin
Аslam Iqbal17-Jul-11 22:47
professionalАslam Iqbal17-Jul-11 22:47 
Questionlook nice Pin
Pranay Rana17-Jul-11 22:47
professionalPranay Rana17-Jul-11 22:47 
QuestionInvert ... Pin
Amarnath S17-Jul-11 21:57
professionalAmarnath S17-Jul-11 21:57 
GeneralMy vote of 5 Pin
salimdz200217-Jul-11 2:56
salimdz200217-Jul-11 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.