Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » GDI+ & Graphics » Image Processing in C#

Image Processing in C#

This application explains basic concepts of image processing including brightening, converting to gray and adding notes to an image using C# and GDI+.

Page Views : 126961
Downloads : 4118
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ImageProcess.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
shru27
I have done Master's in Computer Science. Also I am Microsoft Certified Application Developer (MCAD .NET). I am working in Visual C# .NET from last two years.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Image by sou On February 15, 2007
I's a good web site.Please I need your help to find some code source in c# detecting contour of image..thank you.
Reply | Email | Modify 
image detection by thant On October 28, 2011
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.
Reply | Email | Modify 
Image Warpin by samir On February 18, 2007
hey guys..... i wana do a proj of Image Warping in C#...... can u guys help me out by tellin me some API or related info abt it ???
Reply | Email | Modify 
get image size. by hamid On November 26, 2007
how can i get size of images?
Reply | Email | Modify 
Choice of language by Sobia On January 2, 2008
Assalam O Alaikum, I am a BCS student and have to choose one language to develop E-Photographer. Either I should choose c#.net or vb.net? Kindly guide me. I have learnt c++ and Java during my BCS courses. Regards,
Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

       

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a VB

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a VB Lover

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a VB Lover,

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a VB Lover, but i will

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a VB Lover, but i will suggest

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a VB Lover, but i will suggest you

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a VB Lover, but i will suggest you C#

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a VB Lover, but i will suggest you C# .NET

Reply | Email | Modify 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

        Althogh i am a VB Lover, but i will suggest you C# .NET for

Reply | Email | Modify 
Re: Choice of language by Yogesh On January 24, 2008
If you know Java very well then choose C#.net
Reply | Email | Modify 
Image processing... by Bojan On October 31, 2008
Hi! How could I determine the speed of moving object of a video, taken in real time. I have one programe which makes from this video pictures in sequence. I would like to determine the speed of an object(somekind of mixer), based on this picture sequence with image processing in C#. Can enybodey please help me to resolve this problem? Thank you...!
Reply | Email | Modify 
Tiff reading by Kunal On November 20, 2008
Hi, can u tell how to open Tiff file (500 MB) in Picture box
Reply | Email | Modify 
gggg by adam On December 5, 2008

ssssss

Reply | Email | Modify 
its good by vin On March 5, 2009
hey its very useful
Reply | Email | Modify 
Help me sir by Guendolyn On August 9, 2009

Good day to you sir... I would like to ask for your help.. I am having my thesis subject now and I have no enough knowledge about c#... My groupmates surrender... Our project is about our periofical index of our library... Exactly with image processing of the manually used periodical index... Pls help me the codes... Thamks... Looking forward for your response... Thanks...

Reply | Email | Modify 
How to feather or smooth the edges of the subject in image. by Jaypee On December 7, 2009
How to feather or smooth the edges of the subject. For example, a chicken in a picture. I want to feather or smooth the edges of the chicken with transparent background or using ARGB elements. Please help me this is an urgent. Jaypee
Reply | Email | Modify 
I like it by bach On September 6, 2010
I like Image Processing. I am happy when I have this article. Thank you.
Reply | Email | Modify 
nice by Gourav Kumar On September 8, 2010
nice 
Reply | Email | Modify 
C# by zhang On November 1, 2010
just look it!!
Reply | Email | Modify 
laplacian by Ahlam On November 3, 2010
please I need code for laplacin filter in C#
Reply | Email | Modify 
GREAT by JIE On December 2, 2010
THX. IT'S REALLY HELPFUL
Reply | Email | Modify 
cant open jpg file by Raman On June 13, 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.
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.