Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
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+.

Technologies: GDI+,Visual C# .NET
Total downloads : 1997
Total page views :  64885
Rating :
 3.67/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
ImageProcess.zip
 
Become a Sponsor


Related EbooksTop Videos

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
 [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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
ImageProcess.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
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 | Delete | 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 | Delete | Modify | 
get image size. by hamid On November 26, 2007
how can i get size of images?
Reply | Email | Delete | 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 | Delete | Modify | 
Re: Choice of language by Wahaj Rashid On January 3, 2008

Salam!

       

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

Salam!

        Althogh i am a

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

Salam!

        Althogh i am a VB

Reply | Email | Delete | Modify |