C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 4th, 2013, 05:57 PM
defunktlemon defunktlemon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 16 defunktlemon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 23 m 8 sec
Reputation Power: 0
Display average pixel values

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

}

Reply With Quote
  #2  
Old February 4th, 2013, 06:23 PM
ProgramGuruCpp ProgramGuruCpp is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 7 ProgramGuruCpp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 33 m 28 sec
Reputation Power: 0
Would I be correct in understanding that this is C# code?

Please define "not working". How is it not working?
Is the code compiling? If not, then what are the compile errors (it might also be helpful to state what compiler you are using)? If it does compile then please describe what is happening.

Reply With Quote
  #3  
Old February 5th, 2013, 10:07 AM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 118 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 18 h 48 m 29 sec
Reputation Power: 44
Well, one obvious problem is that your getDominantColor method is not being executed by any event subroutine etc. Thus, it is "not working".

Edit. If you're using Visual Studio, you can view the output by opening the Output Window from the Debug->Window->Output menu option.

Last edited by BobS0327 : February 5th, 2013 at 03:23 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Display average pixel values

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap