Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with Images in C# (Part 1)

Author: DevASP
Download Source Code : 453_GrayScale.zip

In this article I will try to explain you, how we can concert the image to gray sacle.

Create a new application using C# and drag two picture box and open file dialog on the from. Create a "File" menu with "New" as a sub menu, "Image" Menu with "Stretch" and "Center" as a sub menu and "Effects" with the "Gray Scale" as sub menu. As we have to us the pointers in it so we have to set the configuration properties of the project. Right Click on the project name in the solution explorer select the configuration properties and set Build allow unsafe code block to true and click ok. In this way you can use the pointers in C# else it will generate error now include namespace:

using System.Drawing.Imaging;

and create a following funtion which takes the image as a parameter converts it into the gray scale and return new image

private Bitmap GrayScale(Bitmap bmpimg)

{

BitmapData bmpData = bmpimg.LockBits( new Rectangle( 0 , 0 , bmpimg.Width , bmpimg.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );

int remain = bmpData.Stride - bmpData.Width * 3;

unsafe

{

byte* ptr = ( byte* )bmpData.Scan0;

for( int i = 0 ; i < bmpData.Height ; i ++ )

{

for( int j = 0 ; j < bmpData.Width ; j ++ )

{

ptr[ 0 ] = ptr[ 1 ] = ptr[ 2 ] = ( byte )( ( ptr[ 0 ] + ptr[ 1 ] + ptr[ 2 ] ) / 3 );

ptr += 3;

}

ptr += remain;

}

}

bmpimg.UnlockBits( bmpData );

return bmpimg;

}

Now write following code on new menu click event

private void menuItem2_Click(object sender, System.EventArgs e)

{

try

{

OpenFileDialog open = new OpenFileDialog();

open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

if (open.ShowDialog()==DialogResult.OK)

{

pictureBox1.Image = new Bitmap(open.FileName);

menuItem7.Enabled = true;

}

}

catch (Exception)

{

throw new ApplicationException("Failed loading image");

}

}

Write following code on stretch menu click event

private void menuItem4_Click(object sender, System.EventArgs e)

{

pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

}

Write following code on center menu click event

private void menuItem5_Click(object sender, System.EventArgs e)

{

pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;

}

Write following code on gray scale menu click event

private void menuItem7_Click(object sender, System.EventArgs e)

{

pictureBox2.Image = GrayScale(new Bitmap( pictureBox1.Image ) );

}

Article Comments
I'm a Delphi programmer, so I have to unlearn alot of things and relearn a new way of doing things.

I have a program that reads a list of images from JPEGs and generates 60 x 60 thumbnails to display in an owner draw listbox. To create the thumbnails I do something like:

with ThumbNailImage do
Width := 60;
Height := 60;
PixelFormat := pf24bit;
Canvas.StretchDraw(...);
end;

How in the Good Lord's green earth do you do the same thing in C#.NET? I've looked and can't find anything similar.

Posted on 3/9/2007 7:21:21 PM by Bob Thompson

ya thats okay ....
But i m searching for the 16bpp Grayscale Conversion using C#.net....
At least can anyone explain the detail information about the Grayscale Images (like structure, as well as how to store in a file with different formats eg PNG... ) ...
Please help me

Posted on 3/4/2008 1:36:25 AM by Chandra Kant

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to interact with CD-Rom Programmatically

Disclaimer - Privacy
© 2002-2017 DevASP.net