Graphics in C#

This is a discussion on Graphics in C# within the C# Programming forums, part of the General Programming Boards category; Hey Is it possible to make a program which will recognize colors in a picture (.jpg, .gif or any other ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Graphics in C#

    Hey

    Is it possible to make a program which will recognize colors in a picture (.jpg, .gif or any other type). Eg if I click on a certain part of a picture, I want the to see the message telling me which color I'v clicked on and to store that info in a string for further use.

    TNX

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have no knowledge of the API's in C#, but this is what you need:
    1. Load and draw image on screen.
    2. A way to identify the location clicked.
    3. Use something like "GetPixel" for the location.
    4. Display/store the R, G, B values.

    None of this looks to me like it's rocket science.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    System.Drawing.Bitmap has a GetPixel method.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    5
    ok thx I'll see if this does the trick

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Here's something I've just thrown together which should do what you're asking for:

    Code:
    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 ShowColorTest
    {
        public class Form1
        {
            private IContainer components = null;
            private PictureBox pictureBox1;
            private Button button1;
    
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                    components.Dispose();
    
                base.Dispose(disposing);
            }
    
            private void InitializeComponent()
            {
                this.pictureBox1 = new PictureBox();
                this.button1 = new Button();
                ((ISupportInitialize)this.pictureBox1).BeginInit();
                this.SuspendLayout();
    
                this.pictureBox1.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
                this.pictureBox1.Location = new Point(12, 12);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new Size(260, 215);
                this.pictureBox1.TabIndex = 0;
                this.pictureBox1.TabStop = false;
                this.pictureBox1.MouseClick += new MouseEventHandler(this.pictureBox1_MouseClick);
    
                this.button1.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
                this.button1.Location = new Point(197, 233);
                this.button1.Name = "button1";
                this.button1.Size = new Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "load picture";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new EventHandler(this.button1_Click);
    
                this.AutoScaleDimensions = new SizeF(6F, 13F);
                this.AutoScaleMode = AutoScaleMode.Font;
                this.ClientSize = new Size(284, 264);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.pictureBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                ((ISupportInitialize)(this.pictureBox1)).EndInit();
                this.ResumeLayout(false);
    
            }
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
            {
                if (this.pictureBox1.Image == null)
                {
                    MessageBox.Show("load a picture first!");
                    return;
                }
    
                Color c = ((Bitmap)this.pictureBox1.Image).GetPixel(e.X, e.Y);
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Results for co-ordinate: " + e.X + ", " + e.Y);
                sb.AppendLine("R = " + c.R + ", G = " + c.G + ", B = " + c.B);
                MessageBox.Show(sb.ToString());
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog o = new OpenFileDialog();
                o.Multiselect = false;
                o.Filter = "Supported file types (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif|Bitmap (*.bmp)|*.bmp|JPeg (*.jpg)|*.jpg|Gif (*.gif)|*.gif";
    
                if (o.ShowDialog() == DialogResult.OK)
                {
                    byte[] buffer = File.ReadAllBytes(o.FileName);
                    this.pictureBox1.Image = new Bitmap(new MemoryStream(buffer));
                }
            }
        }
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    5
    Wow thanks, this is realy something, I'll definitely try this. Is it possible to apply this to a wider area, or just to one pixel.
    I'm supposed to show basic AI movements (chasing, evading and patroling) in a simple example - this is my task for the end of 3. year at ETF university in Croatia (sort of pre-diploma paper). The problem is I'v never had any lessons in programing graphics. So I'll try to make a program that will recognize obstacles by their color (eg you can pass trough white, but not trough red etc.). I know this is nowhere near the real way this gets done, but for obvious reason I have to be creative. Of course I appreciate your help, and if you have any suggestions how this could be done easier please reply

    TNX again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turtle Graphics, how does it work?
    By freddyvorhees in forum C++ Programming
    Replies: 15
    Last Post: 08-28-2009, 10:57 AM
  2. Graphics Programming :: Approach and Books
    By kuphryn in forum Windows Programming
    Replies: 4
    Last Post: 05-11-2004, 09:33 PM
  3. egavga.bgi problem
    By sunil21 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-22-2003, 06:06 PM
  4. Graphics Devices and Cprintf clash
    By etnies in forum C Programming
    Replies: 6
    Last Post: 05-09-2002, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21