NO

Author Topic: display average pixel values  (Read 6156 times)

defunktlemon

  • Guest
display average pixel values
« on: February 05, 2013, 12:41:48 AM »
Hi.
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.


Code: [Select]
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()
        {
            InitializeComponent();
        }
        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);

                g.Dispose();
                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;

                k.Dispose();
            }
        }

             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;

                    total++;
                }
            }

            //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);
            }
        }
 
    }

}

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: display average pixel values
« Reply #1 on: February 05, 2013, 04:37:52 PM »
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.
What the problem is ?
May the source be with you

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: display average pixel values
« Reply #2 on: February 05, 2013, 10:02:03 PM »
Are you aware that the posted code is C# and this forum is dedicated to programs written in ANSI C using Pelles C?

C# is not ANSI C and will never be.
---
Stefan

Proud member of the UltraDefrag Development Team

Tino

  • Guest
Re: display average pixel values
« Reply #3 on: February 06, 2013, 03:53:30 AM »
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.
What the problem is ?

 :o :o :o
i removed the .exe and it still compiled .. @.@ this is magic timo !!



Oh yes and also be aware that with very large images you will risk an "Integer overflow"
using those loops you do.

Have fun  :)

« Last Edit: February 06, 2013, 03:57:34 AM by Tino »