Thread: display average pixel values

  1. #1
    Registered User
    Join Date
    Nov 2010
    Reading, England, United Kingdom

    display average pixel values

    I have a project which opens an image in PictureBox1, no problem. But the Public Static Color method at the bottom of the code is not working. I was hoping it would display the average value of the pixels in the image. Can anybody help me to understand why it is not working please? Thank you.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing.Imaging;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace imageAlign
        public partial class Form1 : Form
            public Form1()
            private void button1_Click(object sender, EventArgs e)
                Bitmap myImage1 = (Bitmap)pictureBox1.Image;
                OpenFileDialog ofd1 = new OpenFileDialog();
                if (ofd1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    pictureBox1.Image = Image.FromFile(ofd1.FileName);
                    Image k = Image.FromFile(ofd1.FileName);
                    Graphics g = Graphics.FromImage(k);
                    g.FillRectangle(Brushes.White, 155, 235, 120, 70);
                    g.DrawRectangle(Pens.YellowGreen, 155, 235, 120, 70);
                    StringFormat sf = (StringFormat)StringFormat.GenericTypographic.Clone();
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    g.DrawString(DateTime.Now.ToShortDateString(), new
                    Font("Arial", 14, GraphicsUnit.Point), Brushes.Black, new RectangleF(157, 237, 114, 65), sf);
                    k.Save("C:\\testimage.jpeg", ImageFormat.Jpeg);
                    Image image_rect = Image.FromFile("C:\\testimage.jpeg");
                    pictureBox3.Image = image_rect;
                    //pictureBox3.Height = image_rect.Height;
                    //pictureBox3.Width = image_rect.Width;
                 public static Color getDominantColor(Bitmap myImage1)
                      //Used for tally
                int i = 0;
                int j = 0;
                int r = 0;
                int g = 0;
                int b = 0;
                int total = 0;
                for (i = 0; i < myImage1.Width; i++)
                    for (j = 0; j < myImage1.Height; j++)
                        Color clr = myImage1.GetPixel(i, j);
                        r += clr.R;
                        g += clr.G;
                        b += clr.B;
                //Calculate average
                r /= total;
                g /= total;
                b /= total;
                Console.WriteLine(j.ToString() + " " + i.ToString());
                return Color.FromArgb(r, g, b);
            private void button2_Click(object sender, EventArgs e)
                Bitmap myImage2 = (Bitmap)pictureBox2.Image;
                OpenFileDialog ofd2 = new OpenFileDialog();
                if (ofd2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    pictureBox2.Image = Image.FromFile(ofd2.FileName);

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    What's not working exactly? I can't seem to be able to tell from looking at it.

    Meanwhile, you don't need the total variable. "total" is equal to myImage1.Width * myImage1.Height. No need to be incrementing this variable in the loop. Your code will also be clear if you replace j.ToString() and i.ToString() with myImage1.Height and myImage1.Width, respectively.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    May 2003
    Only immediate problem I could see would be if you had integer overflow - but that would require a fairly large image (2900x2900 or larger, depending on the color).

    Agree that something more than "doesn't work" is needed to troubleshoot further.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-17-2010, 03:07 PM
  2. Reducing size of image pixel data to display in window
    By synthetix in forum C Programming
    Replies: 2
    Last Post: 11-16-2009, 05:10 AM
  3. how to display pixel data
    By vkoo in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2009, 07:07 AM
  4. Get pixel values from pic
    By Leite33 in forum Windows Programming
    Replies: 4
    Last Post: 09-12-2006, 01:13 PM
  5. Get pixel values from pic
    By Leite33 in forum C++ Programming
    Replies: 0
    Last Post: 09-12-2006, 06:26 AM

Tags for this Thread