Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow
i am trying to get the color of pixels of a string using c sharp program
the string is created in paint brush program and equal +-1234567890
in paint brush i selected text (the big capital A icon)and choosed black as a foreground color on white background the size of text was 16
then i wrote +-1234567890
and saved it in file named MyNumbers.jpg
in the program below i could not find the black color which was made in paint brush program
the purpose of my program is to store (in file)the black pixels(the foreground) as 1 and the white pixel(the background) as 0
i could not do that
the following is the complete program ... please help

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ScalImage
{
    public partial class Form1 : Form
    {
        public  FileStream fsw;
        public  StreamWriter sw;
        //----------------------------
        Pen p;
        int x, y;
        int start_x, start_y;
        Bitmap _bmp;
        Color co;
        PictureBox MyPictureBox1;
        Graphics gr;
        public Form1()
        {
            InitializeComponent();
        }
        private void FormLoad(object sender, EventArgs e)
        {
            fsw = new FileStream("MyDebug.txt", FileMode.Truncate, FileAccess.Write);
            sw = new StreamWriter(fsw);
            //-------------------------------------------
            MyPictureBox1 = new PictureBox();
            p = new Pen(Color.Red, 1);
            //-------------------------------
            MyPictureBox1.Parent = this;
            MyPictureBox1.Enabled = true;
            MyPictureBox1.Left = 1;
            MyPictureBox1.Top = 1;
            MyPictureBox1.Width = 160;
            MyPictureBox1.Height = 64;
            MyPictureBox1.Image = Image.FromFile("MyNumbers.jpg");//"MyNumbers.bmp");//"MyNumbers.gif");
            MyPictureBox1.Show();
            //-----------------------------------------------------
            _bmp = new Bitmap(MyPictureBox1.Image, MyPictureBox1.Size);
            //Graphics objGraphics = Graphics.FromImage(_bmp);
            gr = this.MyPictureBox1.CreateGraphics();
            textBox1.Text = MyPictureBox1.Width.ToString();//ReadOnly
            textBox2.Text = MyPictureBox1.Height.ToString();//ReadOnly
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int f1,f2,f3;
            for (y = 1; y < MyPictureBox1.Size.Height; y++)
            {
                for (x = 1; x < MyPictureBox1.Size.Width; x++)
                {
                    co = _bmp.GetPixel(x, y);
                    //sw.Write(pixelColor[x, y].R.ToString()+","+ pixelColor[x, y].G.ToString()+","+  pixelColor[x, y].B.ToString()+" ");
                    f1=0;  f2=0;  f3=0;
                    if (co.R != 255 ) f1=1;
                    if (co.G != 255 ) f2=1;
                    if (co.B != 255 ) f3=1;
                    ;
                    if(f1==1 && f2==1 && f3==1)  sw.Write("1");
                    else sw.Write("0");
                }
                sw.Write("\r\n");
            }
            start_x = int.Parse(textBox3.Text);//you may need this
            start_y = int.Parse(textBox4.Text);//you may need this
            gr.DrawLine(p, start_x, start_y, start_x + MyPictureBox1.Width, start_y);//you may need this
        }
        //-----------------------------------------
        protected override void OnClosing(CancelEventArgs e)
        {
            if (sw != null) sw.Close();
            if (fsw != null) fsw.Close();
        }
        //-----------------------------------------
     }
}
Posted
Updated 31-May-16 16:50pm
v3
Comments
F-ES Sitecore 31-May-16 12:39pm    
Test it on a bmp image to see if that is any better, if it is the issue is that jpeg is a "lossy" conversion, meaning that what is saved is an approximation of what the image looks like rather than an exact copy. You might need to alter your definitions of what you consider black and white, so rather than 000000 and ffffff, consider anything below a certain threshold as black, and anything above it as white.

You want to rendre a black pixel as 1 but the logic in your program does exactly the contrary.
In fact you can simplify the whole thing by replacing
C#
f1=0; f2=0; f3=0;
if (co.R != 255 ) f1=1;
if (co.G != 255 ) f2=1;
if (co.B != 255 ) f3=1;

if (f1 == 1 && f2 == 1 && f3 == 1)
   sw.Write("1");
else
   sw.Write("0");

with
C#
sw.Write((co == Color.Black) ? "1" : "0");

I seriously question the real interest of the operation, though :)

You should also adopt as early as possible some consistency in the way your are identing your code blocks. And having a look at basic debugging techniques could be quite useful, too.

[Edit]
You never load the image to your _bmp variable.
Maybe try
C#
_bmp = Image.FromFile("MyNumbers.jpg");

to initialize your variable.
 
Share this answer
 
v2
Comments
Engineer khalid 31-May-16 13:06pm    
hi philo.o
i replced it but the result were worse... i got zero for all pixels
phil.o 31-May-16 13:55pm    
I updated my answer.
Have you tried to debug?
You should never use GetPixel for massive pixel operations; it's prohibitively slow operation. It can only be acceptable if you need to read one of just few pixels.

This is the correct approach: Bitmap.LockBits Method (System.Drawing)[^].

Also, I'm not sure you really need to use PictireBox. If you need to render some graphics on screen, you don't need it at all, it may be not helpful. We can discuss it if you explain your ultimate goals, what you want to achieve.

—SA
 
Share this answer
 
i have change this and it works
C#
//MyPictureBox1.Image = Image.FromFile("aa.bmp");//"MyNumbers.gif");
//MyPictureBox1.Show();
//-----------------------------------------------------
//_bmp = new Bitmap(MyPictureBox1.Image, MyPictureBox1.Size);
_bmp = (Bitmap)Image.FromFile("aa.bmp");

in the loop i wrote this
C#
for (y = 1; y < _bmp.Height; y++)
{
for (x = 1; x < _bmp.Width; x++)
{
co = _bmp.GetPixel(x, y);
//sw.Write(pixelColor[x, y].R.ToString()+","+ pixelColor[x, y].G.ToString()+","+ pixelColor[x, y].B.ToString()+" ");
f1=0; f2=0; f3=0;
if (co.R != 255 ) f1=1;
if (co.G != 255 ) f2=1;
if (co.B != 255 ) f3=1;
;
if(f1==1 && f2==1 && f3==1) sw.Write("1");
else sw.Write("0");
}
sw.Write("\r\n");
}
 
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