c#按钮不起作用 [英] c# buttons don't work

查看:132
本文介绍了c#按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





https://dl.dropbox.com/u/15501961/designer_sized .jpg [ ^ ]

推荐答案

可能是点击事件的问题...

检查以下方式..



- > goto设计形式

- >选择不起作用的按钮

- >转到该按钮的属性并选择活动

- >转到点击事件检查正确的事件是否附加到该按钮..

- >如果没有事件或错误的事件然后选择正确的按钮事件..

ex:

按钮1 - >它必须具有click1_Click(对象发送者,EventArgs e)等点击事件





尝试这一切
may be the problem is on click-events...
check the following way..

->goto design of the form
->select the the button which is not working
->goto properties of that button and select the events
->goto click event check the proper event is attached to that button or not..
->if there is no event or wrong event then select the correct button event..
ex:
"button 1"->it must have the click event like button1_Click(object sender, EventArgs e)


try this all the best


检查你在设计师中连接的事件。

查看每个按钮转动,然后双击它。它是用于新方法还是现有方法之一?

如果所有按钮都转到现有方法,则在每个方法的第一行放置断点,然后运行程序。每个按钮都会遇到断点吗?如果没有,那么您需要查看断开事件处理程序的内容。如果确实如此,那就单步一步找出为什么它看起来不像你认为应该做的那样。



BTW:这是一个坏主意保持VS默认名称 - 当你创建一个控件时,给它一个反映用户将要用它做什么的名字:这样你的代码更自我记录,更容易理解。 button2_Click比butNormalizeChromatics_Click或你编码实际正在做的任何事情都要清晰得多。



如果再次发布代码片段,请将其设为相关位,而不是你的整个节目,不要发布多余的,评论旧的垃圾。帮助我们尽可能清楚地帮助您。
Check you have the events wired up in the designer.
Look at each button in turn, and double click it. Does it go to a new method, or one of the existing ones?
If all buttons go to the existing methods, then put a breakpoint on the first line of each, then run your program. Does each button hit a breakpoint? If not, then you need to look at what is disconnecting the event handlers. If it does, then single step though to find out why it doesn''t seem to be doing what you think it should.

BTW: It is a bad idea to keep the VS default names - when you create a control, give it a name which reflects what the user is going to do with it: that way your code is more self documenting, and easier to follow. "button2_Click" is a lot less clear than "butNormalizeChromatics_Click" or whatever you code is actually doing.

And if you post code fragments again, make it just the relevant bits, not your whole program, and don''t post redundant, commented out old rubbish. Help us to help you by making it as clear as you can.


没有任何代码错误清除所以您可能需要尝试这个....





如果您在成功调试代码时没有得到任何响应,那么只需关闭您的应用并重新启动计算机,如果再没有任何响应然后放入断点在您的按钮单击事件和运行您的应用程序它肯定会给出响应....这是同样的问题一次与我的项目,我做了相同...
there is no any clearance of code error so you may have to try this....


if you getting no response with successful debugging of your code then just close your app and restart your computer after it if again no any response then put the break points on your button click events and run your app it will definitely give response....this is the same problem once with my project and i done the same...


这篇关于c#按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆