using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompareImages
{
    public partial class frm37CompareImages : Form
    {
        public frm37CompareImages()
        {
            InitializeComponent();
        }

        // Let the user select an image file.
        private void label1_Click(object sender, EventArgs e)
        {
            SelectImage(original1PictureBox);
        }
        private void label2_Click(object sender, EventArgs e)
        {
            SelectImage(original2PictureBox);
        }
        private void SelectImage(PictureBox pic)
        {
            if (pictureOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                // Load the image.
                pic.Image = LoadBitmapUnlocked(pictureOpenFileDialog.FileName);

                // Clear the result image.
                resultPictureBox.Image = null;

                // If both images are loaded, enable the Compare button.
                compareButton.Enabled =
                    ((original1PictureBox.Image != null) &&
                     (original2PictureBox.Image != null));
            }
        }

        // Load a bitmap without locking it.
        private Bitmap LoadBitmapUnlocked(string filename)
        {
            using (Bitmap bm = new Bitmap(filename))
            {
                return new Bitmap(bm);
            }
        }

        // Compare the images.
        private void compareButton_Click(object sender, EventArgs e)
        {
            // Get the original images.
            Bitmap bmA = original1PictureBox.Image as Bitmap;
            Bitmap bmB = original2PictureBox.Image as Bitmap;

            // Get the result dimensions.
            int width = Math.Min(bmA.Width, bmB.Width);
            int height = Math.Min(bmA.Height, bmB.Height);

            // Make result image.
            Bitmap bmC = new Bitmap(width, height);

            // Make Bitmap32 objects and lock them.
            Bitmap32 bm32a = new Bitmap32(bmA);
            Bitmap32 bm32b = new Bitmap32(bmB);
            Bitmap32 bm32c = new Bitmap32(bmC);
            bm32a.LockBitmap();
            bm32b.LockBitmap();
            bm32c.LockBitmap();

            // Process the images.
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    byte ra, ga, ba, aa, rb, gb, bb, ab;
                    bm32a.GetPixel(x, y, out ra, out ga, out ba, out aa);
                    bm32b.GetPixel(x, y, out rb, out gb, out bb, out ab);

                    // Make the result pixel red if the input pixels are different.
                    if ((ra == rb) && (ga == gb) && (ba == bb))
                        bm32c.SetPixel(x, y, 255, 255, 255, 255);
                    else
                        bm32c.SetPixel(x, y, 255, 0, 0, 255);
                }
            }

            // Unlock and display the results.
            bm32a.UnlockBitmap();
            bm32b.UnlockBitmap();
            bm32c.UnlockBitmap();
            resultPictureBox.Image = bmC;
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.original1PictureBox = new System.Windows.Forms.PictureBox();
            this.original2PictureBox = new System.Windows.Forms.PictureBox();
            this.resultPictureBox = new System.Windows.Forms.PictureBox();
            this.pictureOpenFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.compareButton = new System.Windows.Forms.Button();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel3 = new System.Windows.Forms.Panel();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.original1PictureBox)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.original2PictureBox)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.resultPictureBox)).BeginInit();
            this.tableLayoutPanel1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.panel3.SuspendLayout();
            this.SuspendLayout();
            // 
            // original1PictureBox
            // 
            this.original1PictureBox.Location = new System.Drawing.Point(0, 0);
            this.original1PictureBox.Name = "original1PictureBox";
            this.original1PictureBox.Size = new System.Drawing.Size(100, 100);
            this.original1PictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.original1PictureBox.TabIndex = 0;
            this.original1PictureBox.TabStop = false;
            // 
            // original2PictureBox
            // 
            this.original2PictureBox.Location = new System.Drawing.Point(0, 0);
            this.original2PictureBox.Name = "original2PictureBox";
            this.original2PictureBox.Size = new System.Drawing.Size(100, 100);
            this.original2PictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.original2PictureBox.TabIndex = 1;
            this.original2PictureBox.TabStop = false;
            // 
            // resultPictureBox
            // 
            this.resultPictureBox.Location = new System.Drawing.Point(0, 0);
            this.resultPictureBox.Name = "resultPictureBox";
            this.resultPictureBox.Size = new System.Drawing.Size(100, 100);
            this.resultPictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.resultPictureBox.TabIndex = 1;
            this.resultPictureBox.TabStop = false;
            // 
            // pictureOpenFileDialog
            // 
            this.pictureOpenFileDialog.Filter = "Picture files|*.bmp;*.jpg;*.gif;*.png;*.tif|All Files|*.*";
            // 
            // compareButton
            // 
            this.compareButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.compareButton.Enabled = false;
            this.compareButton.Location = new System.Drawing.Point(139, 12);
            this.compareButton.Name = "compareButton";
            this.compareButton.Size = new System.Drawing.Size(75, 23);
            this.compareButton.TabIndex = 2;
            this.compareButton.Text = "Compare";
            this.compareButton.UseVisualStyleBackColor = true;
            this.compareButton.Click += new System.EventHandler(this.compareButton_Click);
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tableLayoutPanel1.ColumnCount = 3;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33334F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33334F));
            this.tableLayoutPanel1.Controls.Add(this.label3, 2, 0);
            this.tableLayoutPanel1.Controls.Add(this.label2, 1, 0);
            this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 1);
            this.tableLayoutPanel1.Controls.Add(this.panel2, 1, 1);
            this.tableLayoutPanel1.Controls.Add(this.panel3, 2, 1);
            this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
            this.tableLayoutPanel1.Location = new System.Drawing.Point(12, 41);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 2;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 25F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(328, 135);
            this.tableLayoutPanel1.TabIndex = 3;
            // 
            // label3
            // 
            this.label3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label3.Location = new System.Drawing.Point(221, 0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(104, 25);
            this.label3.TabIndex = 4;
            this.label3.Text = "Result";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label2
            // 
            this.label2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label2.Location = new System.Drawing.Point(112, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(103, 25);
            this.label2.TabIndex = 4;
            this.label2.Text = "Original 2...";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.label2.Click += new System.EventHandler(this.label2_Click);
            // 
            // panel1
            // 
            this.panel1.AutoScroll = true;
            this.panel1.Controls.Add(this.original1PictureBox);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(3, 28);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(103, 104);
            this.panel1.TabIndex = 0;
            // 
            // panel2
            // 
            this.panel2.AutoScroll = true;
            this.panel2.Controls.Add(this.original2PictureBox);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel2.Location = new System.Drawing.Point(112, 28);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(103, 104);
            this.panel2.TabIndex = 1;
            // 
            // panel3
            // 
            this.panel3.AutoScroll = true;
            this.panel3.Controls.Add(this.resultPictureBox);
            this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel3.Location = new System.Drawing.Point(221, 28);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(104, 104);
            this.panel3.TabIndex = 2;
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label1.Location = new System.Drawing.Point(3, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(103, 25);
            this.label1.TabIndex = 3;
            this.label1.Text = "Original 1...";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.label1.Click += new System.EventHandler(this.label1_Click);
            // 
            // frm37CompareImages
            // 
            this.AcceptButton = this.compareButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(352, 188);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Controls.Add(this.compareButton);
            this.Name = "frm37CompareImages";
            this.Text = "CompareImages";
            ((System.ComponentModel.ISupportInitialize)(this.original1PictureBox)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.original2PictureBox)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.resultPictureBox)).EndInit();
            this.tableLayoutPanel1.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.panel3.ResumeLayout(false);
            this.panel3.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion
        private System.Windows.Forms.PictureBox original1PictureBox;
        private System.Windows.Forms.PictureBox resultPictureBox;
        private System.Windows.Forms.OpenFileDialog pictureOpenFileDialog;
        private System.Windows.Forms.PictureBox original2PictureBox;
        private System.Windows.Forms.Button compareButton;
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Panel panel3;
        private System.Windows.Forms.Label label1;
    }
}