Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
ı have a grayscale ımage. ı wrote thıs code and now ı want to convert to color ımage and ı want to determıned range of r g b ı wrote code about that but doesn t work

C#
private void button13_Click(object sender, EventArgs e)
       {



           Bitmap resim = new Bitmap(pictureBox2.Image);
           Color renk, renk2;
           int r, g, b, d;
           for (int i = 0; i < resim.Width; i++)
           {

               for (int j = 0; j < resim.Height; j++)
               {


                   renk = resim.GetPixel(i, j);
                   r = renk.R;
                   g = renk.G;
                   b = renk.B;
                   d = (r + g + b) / 3;





                   if (d < 55)
                   {
                       d = renk.R;
                   }
                   if (d > 55 && d < 155)
                   {
                       d = renk.G;
                   }

                   if (d > 155 && d < 255)
                   {
                       d = renk.B;
                   }

                   resim.SetPixel(i, j, renk);


                   renk2 = Color.FromArgb(d, d, d);


               }

               pictureBox3.Image = resim;
           }
Posted
Updated 18-Apr-13 8:18am
v2
Comments
Monster Maker 18-Apr-13 14:18pm    
Editors, please take care of the code..!!!
DinoRondelly 18-Apr-13 14:19pm    
You mean like put it in a code block?
Monster Maker 18-Apr-13 14:21pm    
same!
Sergey Alexandrovich Kryukov 18-Apr-13 15:50pm    
Please do not re-post.
—SA

Search the articles for "Image processing for dummies". Yes, you read that right.

There's an article for quickly converting an image to grayscale.


You cannot convert a grayscale image to color as there is no information in the image or its metadata to know which shade of gray goes to what color. The same shade may also be several different colors in the image, so it's impossible for an algorithm to know what the original color was for any one pixel.
 
Share this answer
 
Comments
Kenneth Haugland 18-Apr-13 14:37pm    
You are abolutly right, however I assumed there was an english mistake in his text and assumed his header was the real question. Though on a closer look it might seem that he thinks that is possible. 5'ed it in any case.
Espen Harlinn 18-Apr-13 15:58pm    
This sounds about right ;)
You didnt specify if this was WinForm or WPF. I have used this in WPF:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.formatconvertedbitmap.aspx[^]

VB
bmp = New BitmapImage(New Uri(dlg.FileName))

Dim GreyBmp As New FormatConvertedBitmap
GreyBmp.BeginInit()
GreyBmp.Source = bmp
GreyBmp.DestinationFormat = PixelFormats.Gray8
GreyBmp.EndInit()

Dim im As BitmapSource = GreyBmp

Image1.Source = im


However it is possible to trasform a Bitmap image to BitmapSource:
Bitmap to BitmapSource[^]
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900