ARTICLE

Image Processing in C#

Posted by shru27 Articles | GDI+ & Graphics April 13, 2004
This application explains basic concepts of image processing including brightening, converting to gray and adding notes to an image using C# and GDI+.
Reader Level:
Download Files:
 

This application explains basic concepts of image processing including brightening, converting to gray and adding notes to an image using System.Drawing.Imaging namespace classes.

Following code shows how to display an image in a picture box.

private System.Windows.Forms.PictureBox p1;
string
path = abc.jpg;
p1.Image =
new
Bitmap(path);

This code show how to take the bitmap data:

Bitmap b = new Bitmap(path);
BitmapData bmData = b.LockBits(
new
Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

The screen shot above shows an image before and after increasing brightness.

This is the main function, where we do the main processing. If you look into the code you will notice, I am using unsafe code, where I am doing the main processing. This is really important because unsafe mode allow it to run that piece of code without CLR, which make the code to run much faster. Here its important because we care about the speed of the routine.

public static bool Brighten(Bitmap b, int nBrightness)
{
// GDI+ return format is BGR, NOT RGB.
BitmapData bmData
= b.LockBits(
new
Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int
stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe

{
int
nVal;
byte * p = (byte *)(void
*)Scan0;
int
nOffset = stride - b.Width*3;
int
nWidth = b.Width * 3;
for(int
y=0;y<b.Height;++y)
{
for (int
x = 0; x < nWidth; ++x)
{
nVal = (
int
) (p[0] + nBrightness);
if
(nVal < 0) nVal = 0;
if
(nVal > 255) nVal = 255;
p[0] = (
byte
)nVal;
++p;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true
;

When you click on the Bright button this piece of code execute.

private void btnBright_Click(object sender, System.EventArgs e)
{
try
{
string
path = Path.Combine(tv1.SelectedNode.FullPath, lstFiles.SelectedItem.ToString());
Bitmap b =
new
Bitmap(path);
Brighten(b, m_nTrack);
p2.Image = b;
}
catch
{}
}

This next screen-shot shows the gray effect.

Login to add your contents and source code to this article
post comment
     

I need code for image mirroring and Applying Blur effect. Could You please help

Posted by Muhammad Amer Feb 19, 2013

Hi, This is nice article.i am using this code.image display properly. But getting error 'parameter is not valid' if i do any operation on any image. I got the solution........ in function i change the pixelformat. Now i use the bitmap object pixelformat BitmapData bmData = b.LockBits(r,ImageLockMode.ReadWrite, b.PixelFormat); and its work but......but..... now i get another problem.its show 'Attempted to read or write protected memory.this is often an indication that other memory is correpted.' What is it mean?? can any one help me to short out this.??

Posted by Sumit Sharma Apr 04, 2012

at this website http://haishibai.blogspot.com/2009/12/image-processing-c-tutorial-6-face.html have image detection, but don't show codes for project.

Posted by thant zin Oct 28, 2011

i try to use ur software but i cannot open file in it.. i select folder but it doesnt show jpg or other file but only show ini file. can u help me.

Posted by Raman Shrestha Jun 13, 2011

THX. IT'S REALLY HELPFUL

Posted by JIE CHEN Dec 02, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.