View Single Post
  #23 (permalink)  
Old 03-10-2008, 01:46 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Thumbs up Re: Converting image from one format to another in C#

Hi,
You can use the following code to convert the image to gray-scale image

Code:
public bool GrayScale(Bitmap b)
		{
			// GDI+ still lies to us - the 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; //the length of the line
			System.IntPtr Scan0 = bmData.Scan0; 

			unsafe
			{
				byte * p = (byte*)(void*)Scan0;

				int nOffset =  stride - b.Width*3;

				byte red, green, blue;

				for(int y=0;y < b.Height;++y)
				{
					for(int x=0; x < b.Width; ++x )
					{
						blue = p[0];
						green = p[1];
						red = p[2];

						p[0] = p[1] = p[2] = (byte)(.299 * red 
							+ .587 * green 
							+ .114 * blue);

						p += 3;
					}
					p += nOffset;
				}
			}		
			
			b.UnlockBits(bmData);

			return true;
		}
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Reply With Quote