In this article, I will show you, how to compare two images in c#. You can say that how to match prints images in c#. Basically, this types things we can implement in biometric systems like fingerprint etc. If you want to work on this system, follow these mentioned steps to complete this task.
Step-1: Create a windows form project using Visual Studio, you can take any version of visual studio.
Step-2: Design output type form in default added file ie. form1
Step-3: Copy and Paste mentioned code, according to their controls
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Fingure_print
{
public partial class Form2 : Form
{
Bitmap bitmapPictureBox1;
Bitmap bitmapPictureBox2;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox1.ImageLocation = openFileDialog1.FileName;
bitmapPictureBox1 = new Bitmap(openFileDialog1.FileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox2.ImageLocation = openFileDialog1.FileName;
bitmapPictureBox2 = new Bitmap(openFileDialog1.FileName);
}
}
private void button3_Click(object sender, EventArgs e)
{
bool compare = ImageCompareString(bitmapPictureBox1,bitmapPictureBox2);
if(compare==true)
{
MessageBox.Show("match");
}
else
{
MessageBox.Show("not");
}
}
public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage)
{
MemoryStream ms = new MemoryStream();
firstImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
String firstBitmap = Convert.ToBase64String(ms.ToArray());
ms.Position = 0;
secondImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
String secondBitmap = Convert.ToBase64String(ms.ToArray());
if (firstBitmap.Equals(secondBitmap))
{
return true;
}
else
{
return false;
}
}
}
}
Code Generates the following output
Step-1: Create a windows form project using Visual Studio, you can take any version of visual studio.
Step-2: Design output type form in default added file ie. form1
Step-3: Copy and Paste mentioned code, according to their controls
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Fingure_print
{
public partial class Form2 : Form
{
Bitmap bitmapPictureBox1;
Bitmap bitmapPictureBox2;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox1.ImageLocation = openFileDialog1.FileName;
bitmapPictureBox1 = new Bitmap(openFileDialog1.FileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox2.ImageLocation = openFileDialog1.FileName;
bitmapPictureBox2 = new Bitmap(openFileDialog1.FileName);
}
}
private void button3_Click(object sender, EventArgs e)
{
bool compare = ImageCompareString(bitmapPictureBox1,bitmapPictureBox2);
if(compare==true)
{
MessageBox.Show("match");
}
else
{
MessageBox.Show("not");
}
}
public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage)
{
MemoryStream ms = new MemoryStream();
firstImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
String firstBitmap = Convert.ToBase64String(ms.ToArray());
ms.Position = 0;
secondImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
String secondBitmap = Convert.ToBase64String(ms.ToArray());
if (firstBitmap.Equals(secondBitmap))
{
return true;
}
else
{
return false;
}
}
}
}
Code Generates the following output