5 Replies - 990 Views - Last Post: 20 February 2013 - 09:07 AM Rate Topic: -----

#1 defunktlemon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-November 10

c# buttons don't work

Posted 20 February 2013 - 01:26 AM

Hi.
I have a code which should allow for images to open in the form in c# but buttons 3 and 5 aren't working for some reason and I can't find the problem. The code executes ok, but when I press the buttons I get no response. The other 3 buttons, load image / load image / button 4, are all working correctly.
I have attached an image with the designer form on to show the layout. If anyone can advise me on what the problem is I would be very thankful.

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 IdentifySleeper : Form
    {
        Bitmap newImage1, newImage2, test;
        public static Bitmap Diff(Bitmap src1, Bitmap src2, int x1, int y1, int x2, int y2, int width, int height)
        {
            Bitmap diffBM = new Bitmap(width, height, PixelFormat.Format24bppRgb);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    //Get Both Colours at the pixel point
                    Color col1 = src1.GetPixel(x1 + x, y1 + y);
                    Color col2 = src2.GetPixel(x2 + x, y2 + y);

                    //Get the difference RGB
                    int r = 0, g = 0, b = 0;
                    r = Math.Abs(col1.R - col2.R);



                    g = Math.Abs(col1.G - col2.G);
                    b = Math.Abs(col1.B - col2.B)/>/>;

                    //Invert the difference average
                    int dif = ((r + g + B)/>/> / 3);

                    //Create new grayscale rgb colour
                    Color newcol = Color.FromArgb(dif, dif, dif);

                    diffBM.SetPixel(x, y, newcol);

                }
            }

            return diffBM;
        }


        public IdentifySleeper()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd1 = new OpenFileDialog();

            if (ofd1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                pictureBox1.Image = Image.FromFile(ofd1.FileName);
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

                // Grab the user's image and crop it to produce a sample image
                Bitmap k = (Bitmap)Image.FromFile(ofd1.FileName);
                Rectangle imageSection = new Rectangle(k.Width / 3, 0, k.Width / 3, k.Height);
                Bitmap kCropped = (Bitmap)k.Clone(imageSection, k.PixelFormat);

                // Array for keeping the sums of each row of pixels
                float[] resultArray = new float[kCropped.Height];

                // Populate the array with data from each row of pixels, using the brightness value
                for (int i = 0; i < kCropped.Height; i++)
                {
                    float value = 0;
                    for (int j = 0; j < kCropped.Width; j++)
                    {
                        value += kCropped.GetPixel(j, i).GetBrightness();
                    }
                    resultArray[i] = value;
                }

                // Obtain the maximum and minimum value of the sampled rows.
                float maxValue = 0;
                float minValue = float.MaxValue;
                for (int i = 1; i < resultArray.Length; i++)
                {
                    if (resultArray[i - 1] > maxValue) maxValue = resultArray[i - 1];
                    if (resultArray[i - 1] < minValue) minValue = resultArray[i - 1];

                }

                // Find the median value of each row, and use this to find upper and lower bounds for the image
                float midPoint = maxValue - minValue;
                float upperBound = (midPoint + maxValue) / 2;
                float lowerBound = (midPoint + minValue) / 2;

                int alignmentStart = 0;
                int alignmentFinish = 0;

                // Scan the result array for the start of the first sleeper
                for (int i = 0; i < resultArray.Length; i++)
                {
                    if (resultArray[i] > upperBound)
                    {
                        alignmentStart = i;
                        break;
                    }
                }

                // Scan the array from the place the last loop left off for the end of the sleeper
                for (int i = alignmentStart; i < resultArray.Length; i++)
                {
                    if (resultArray[i] < lowerBound)
                    {
                        alignmentFinish = i;
                        break;
                    }
                }

                // Using the start and end locations, we can now isolate the sleeper itself
                Rectangle alignedImage = new Rectangle(0, alignmentStart, k.Width, alignmentFinish - alignmentStart);

                Bitmap kAligned = (Bitmap)k.Clone(alignedImage, k.PixelFormat);

                Image image_rect = kAligned;


                image_rect.Save("good.jpg");

                // Put the sleeper in a picturebox for the user to see
                pictureBox3.Image = image_rect;
                pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;



                // Get rid of unused images
                k.Dispose();
                kCropped.Dispose();
            }
        }




        private void IdentifySleeper_Load(object sender, EventArgs e)
        {



        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd2 = new OpenFileDialog();

            if (ofd2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                pictureBox4.Image = Image.FromFile(ofd2.FileName);
                pictureBox4.SizeMode = PictureBoxSizeMode.Zoom;

                // Grab the user's image and crop it to produce a sample image
                Bitmap k = (Bitmap)Image.FromFile(ofd2.FileName);
                Rectangle imageSection = new Rectangle(k.Width / 3, 0, k.Width / 3, k.Height);
                Bitmap kCropped = (Bitmap)k.Clone(imageSection, k.PixelFormat);

                // Array for keeping the sums of each row of pixels
                float[] resultArray = new float[kCropped.Height];

                // Populate the array with data from each row of pixels, using the brightness value
                for (int i = 0; i < kCropped.Height; i++)
                {
                    float value = 0;
                    for (int j = 0; j < kCropped.Width; j++)
                    {
                        value += kCropped.GetPixel(j, i).GetBrightness();
                    }
                    resultArray[i] = value;
                }

                // Obtain the maximum and minimum value of the sampled rows.
                float maxValue = 0;
                float minValue = float.MaxValue;
                for (int i = 1; i < resultArray.Length; i++)
                {
                    if (resultArray[i - 1] > maxValue) maxValue = resultArray[i - 1];
                    if (resultArray[i - 1] < minValue) minValue = resultArray[i - 1];

                }

                // Find the median value of each row, and use this to find upper and lower bounds for the image
                float midPoint = maxValue - minValue;
                float upperBound = (midPoint + maxValue) / 2;
                float lowerBound = (midPoint + minValue) / 2;

                int alignmentStart = 0;
                int alignmentFinish = 0;

                // Scan the result array for the start of the first sleeper
                for (int i = 0; i < resultArray.Length; i++)
                {
                    if (resultArray[i] > upperBound)
                    {
                        alignmentStart = i;
                        break;
                    }
                }

                // Scan the array from the place the last loop left off for the end of the sleeper
                for (int i = alignmentStart; i < resultArray.Length; i++)
                {
                    if (resultArray[i] < lowerBound)
                    {
                        alignmentFinish = i;
                        break;
                    }
                }

                // Using the start and end locations, we can now isolate the sleeper itself
                Rectangle alignedImage = new Rectangle(0, alignmentStart, k.Width, alignmentFinish - alignmentStart);

                Bitmap kAligned = (Bitmap)k.Clone(alignedImage, k.PixelFormat);

                Image image_rect2 = kAligned;

                image_rect2.Save("bad.jpg");

                // Put the sleeper in a picturebox for the user to see
                pictureBox2.Image = image_rect2;
                pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;



                // Get rid of unused images
                k.Dispose();
                kCropped.Dispose();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPEG IMAGES|*.jpg";
            openFileDialog1.InitialDirectory = "C:\\Users\\jason\\Documents\\IProject\\code\\imageAlign\\imageAlign\\bin\\Debug";
            openFileDialog1.Title = "Open Image";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // textBox1.Text = openFileDialog1.FileName;
                newImage1 = (Bitmap)(System.Drawing.Image.FromFile(openFileDialog1.FileName));
                pictureBox5.Width = newImage1.Width;
                pictureBox5.Height = newImage1.Height;
                pictureBox5.Image = newImage1;
                //pictureBox6.Location = new System.Drawing.Point(newImage1.Width + 40, pictureBox6.Location.Y);
            }
            //else
            //{
            //    textBox1.Text = "select file please";
            //}
            //}
        }

        //  private void textBox2_MouseHover(object sender, EventArgs e)
        //{

        //}


        private void button4_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPEG IMAGES|*.jpg";
            openFileDialog1.InitialDirectory = "C:\\Users\\jason\\Documents\\IProject\\code\\imageAlign\\imageAlign\\bin\\Debug";
            openFileDialog1.Title = "Open Image";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // textBox2.Text = openFileDialog1.FileName;
                newImage2 = (Bitmap)(System.Drawing.Image.FromFile(openFileDialog1.FileName));
                pictureBox6.Width = newImage2.Width;
                pictureBox6.Height = newImage2.Height;
                pictureBox6.Image = newImage2;
            }
            //else
            //{
            //    textBox2.Text = "select file please";
            //}
            //}
        }


        private void button5_Click(object sender, EventArgs e)
        {
            test = Diff(newImage1, newImage2, 0, 0, 0, 0, newImage1.Width, newImage1.Height);
            pictureBox7.Width = test.Width;
            pictureBox7.Height = test.Height;
            pictureBox7.Image = test;
            //pictureBox7.Location = new System.Drawing.Point(newImage1.Width + newImage2.Width + 80, pictureBox7.Location.Y);
        }
    }
}


Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: c# buttons don't work

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: c# buttons don't work

Posted 20 February 2013 - 01:32 AM

Well, I believe you seem to have posted in the wrong forum. This certainly belongs in the C# forum.

Note to mod : Can you please move this to the C# forum. Thanks. :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 defunktlemon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-November 10

Re: c# buttons don't work

Posted 20 February 2013 - 02:45 AM

can't see a move option here. have actually found a solution anyway. is there a delete thread button around on this site somewhere?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: c# buttons don't work

Posted 20 February 2013 - 04:14 AM

Moved to C#
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: c# buttons don't work

Posted 20 February 2013 - 07:20 AM

Make sure you have the click events for the buttons attached to the methods. Put a MessageBox.Show("I'm in 3"); at the top of the method.

This post has been edited by Momerath: 20 February 2013 - 07:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: c# buttons don't work

Posted 20 February 2013 - 09:07 AM

No, there isn't a delete thread option around here at all. The point of these forums, in addition to solving the OP's problem(s), is to document solutions to those problems for future users with the same or similar questions. So not only should you not delete it, we would appreciate it if you would post the solution you found so that others may benefit too. Also, the experts around here might have advice regarding the solution you found, so it's a win-win if you post it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1