I am trying to compare two images by using picture box, but I got a problem: How can I pass the selected picture name as a parameter to a function as a string?
I save picture path and name as a string name1 and string name2, but I got a problem when I pass them as parameters.
Below is my code. Please tell me where I am wrong.
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.Title = "Select User Profile Image";
ofd1.Filter = "Image File(*.png;*.jpg;*.bmp;*.gif)|*.png;*.jpg;*.bmp;*.gif";
if (ofd1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(ofd1.FileName);
string name1 = ofd1.FileName;
}
}
private void button1_Click(object sender, EventArgs e)
{
Compare(name1,name2);
}
public void Compare(string bmp1, string bmp2, byte threshold = 3)
{
Bitmap firstBmp = (Bitmap)Image.FromFile(bmp1);
Bitmap secondBmp = (Bitmap)Image.FromFile(bmp2);
firstBmp.GetDifferenceImage(secondBmp, true);
string result = string.Format("Difference: {0:0.0} %", firstBmp.PercentageDifference(secondBmp, threshold) * 100);
}
You create variable name1 inside if statement inside pictureBox1_Click(). You should create class level variable to use it inside button1_Click(), because name1 is visible only inside if block:
public YourClass
{
string name1 = String.Empty:
//..... your code
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
ofd1.Title = "Select User Profile Image";
ofd1.Filter = "Image File(*.png;*.jpg;*.bmp;*.gif)|*.png;*.jpg;*.bmp;*.gif";
if (ofd1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(ofd1.FileName);
name1 = ofd1.FileName;
}
}
private void button1_Click(object sender, EventArgs e)
{
Compare(name1,name2);
}
public void Compare(string bmp1, string bmp2, byte threshold = 3)
{
Bitmap firstBmp = (Bitmap)Image.FromFile(bmp1);
Bitmap secondBmp = (Bitmap)Image.FromFile(bmp2);
firstBmp.GetDifferenceImage(secondBmp, true);
string result = string.Format("Difference: {0:0.0} %", firstBmp.PercentageDifference(secondBmp, threshold) * 100);
}
}
If you create name2 the same way, you should make it class level variable too.
You can declare a member variable in your Form to save the file path:
public partial class YourForm : Form
{
private string _imagePath1;
private string _imagePath2;
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd1 = new OpenFileDialog();
// ... your code
if (ofd1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(ofd1.FileName);
// SAVE PATH TO CLASS MEMBER
_imagePath1 = ofd1.FileName;
}
}
private void button1_Click(object sender, EventArgs e)
{
// USE CLASS MEMBERS
Compare(_imagePath1, _imagePath2);
}
}
I think below URL is helpful to you
http://www.c-sharpcorner.com/uploadfile/prathore/image-comparison-using-C-Sharp
Related
I have the following subroutine to create a label, located in form 1:
private void MakeLabelForGameStyle()
{
Label SelectedMode = new Label();
SelectedMode.Text = form2obj.GameStyle;
SelectedMode.Location = new Point(507, 92);
SelectedMode.AutoSize = true;
SelectedMode.Font = new Font("Calibri", 12);
Controls.Add(SelectedMode);
}
In form 2, the relevant code to determine 'GameStyle' is as follows:
private string gameStyle;
private void button3_Click(object sender, EventArgs e)
{
gameStyle = "Best Of 3";
Close();
}
private void button4_Click(object sender, EventArgs e)
{
gameStyle = "Best of 5";
Close();
}
public string GameStyle
{
get { return gameStyle; }
}
But when I run the program, the label is blank after clicking either of the buttons.
In my form. I have two picturebox and a button that capture the image into picturebox.
Two Picturebox
WebcamImage - represent the live camera image
PreviewImage - represent the Captured image from webcamimage
When i saved this captured image it will go to my UserImage picturebox (In my Usercontrol)
The problems is i don't know how i'm gonna get the picturebox image path.
What i want is when i click my saved button the image path will be saved to my label text.
Here's my code
PS: I'm using Aforge.dll
public partial class CaptureImage : Form
{
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
RegisterCustomer _view;
public CaptureImage(RegisterCustomer view)
{
InitializeComponent();
this._view = view;
}
private void CaptureImage_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
if (FinalFrame.VideoCapabilities.Length > 0)
{
string highestSolution = "0;0";
//Search for the highest resolution
for (int i = 0; i < FinalFrame.VideoCapabilities.Length; i++)
{
if (FinalFrame.VideoCapabilities[i].FrameSize.Width > Convert.ToInt32(highestSolution.Split(';')[0]))
highestSolution = FinalFrame.VideoCapabilities[i].FrameSize.Width.ToString() + ";" + i.ToString();
}
}
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
btn_save.Hide();
btn_cancel.Hide();
}
void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
WebcamImage.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void CaptureImage_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
}
private void btn_save_Click(object sender, EventArgs e)
{
_view.UserImage.Image = PreviewImage.Image;
this.Close();
}
private void btn_cancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btn_capture_Click(object sender, EventArgs e)
{
PreviewImage.Image = (Bitmap)WebcamImage.Image.Clone();
PreviewImage.BringToFront();
btn_capture.Hide();
btn_save.Show();
btn_cancel.Show();
}
}
This is only i know in getting the picturebox image path by using openfile dialog
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "Image Files (*.jpg;*.jpeg;.*.png; | *.jpg;*.jpeg;.*.png;)";
ofd.FilterIndex = 1;
ofd.Multiselect = false;
ofd.Title = "Select Image File";
if (ofd.ShowDialog() == DialogResult.OK)
{
location = ofd.FileName;
path.Text = location;
UserImage.Image = Image.FromFile(location);
UserImage.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
I am learning to program in C # so my question is how to call the method from the button3
Look for information on the web but it is not very clear to me why I turn to this site
private void button3_Click_1(object sender, EventArgs e)
{
}
private void export2File(ListView lv, string splitter)
{
string filename = "";
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "SaveFileDialog Export2File";
sfd.Filter = "Text File (.txt) | *.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
filename = sfd.FileName.ToString();
if (filename != "")
{
using (StreamWriter sw = new StreamWriter(filename))
{
foreach (ListViewItem item in lv.Items)
{
sw.WriteLine("{0}{1}{2}", item.SubItems[0].Text, splitter, item.SubItems[1].Text);
}
}
}
}
}
private void button3_Click_1(object sender, EventArgs e)
{
ListView listView1 = new ListView();
string splitter = ",";
export2File(listview1, splitter);
}
You need to pass a reference to the ListView on your Form, and the desired "splitter" into the method. Assuming listView1 and a comma:
private void button3_Click_1(object sender, EventArgs e)
{
export2File(listView1, ",");
}
I'm trying to implement an application in C# which generates me a QR Code. I've managed to do this, but I don't know how to call CreateQRImage(string inputData) inside the genButton_Click(object sender, EventArgs e).
I inside the windows form I have a textbox and a button, and I think that the function CreateQRImage must be called with the name of the textbox
Here is the code:
private void genButton_Click(object sender, EventArgs e)
{
}
public void CreateQRImage(string inputData)
{
if (inputData.Trim() == String.Empty)
{
System.Windows.Forms.MessageBox.Show("Data must not be empty.");
}
BarcodeWriter qrcoder = new ZXing.BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new ZXing.QrCode.QrCodeEncodingOptions
{
ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H,
Height = 250,
Width = 250
}
};
string tempFileName = System.IO.Path.GetTempPath() + inputData + ".png";
Image image;
String data = inputData;
var result = qrcoder.Write(inputData);
image = new Bitmap(result);
image.Save(tempFileName);
System.Diagnostics.Process.Start(tempFileName);
}
This should do the trick:
private void genButton_Click(object sender, EventArgs e)
{
// Assuming your text box name. Also assuming UI disables button until text is entered.
this.CreateQRImage(myTextBox.Text);
}
I have this code here which i use in order to upload some stuff in a windows form:
public Form1()
{
InitializeComponent();
}
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
userSelectedFilePath = ofd.FileName;
}
}
public string userSelectedFilePath
{
get
{ return tbFilePath.Text;
}
set
{tbFilePath.Text = value;
}
}
private void btn_compare_Click(object sender, EventArgs e)
{
string Xml1 = tbFilePath.Text;
string Xml2 = System.IO.File.ReadAllText(#"C:");
compare.comparison(Xml1, Xml2);
}
Apparently i'm doing something wrong because i'm not passing the tbFilePath.Text which i need when i have: string Xml1 = tbFilePath.Text;
What is it?
What you probably want is to compare the contents of 2 files.
As siride said your code does not make sense(see his comment)
Add this method to your class
private string FindFile()
{
OpenFileDialog ofd = new OpenFileDialog();
string _xmlPath1 = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
return ofd.FileName;
else
return null;
}
And then you can do this:
private void btn_compare_Click(object sender, EventArgs e)
{
string x1 = System.IO.File.ReadAllText(FindFile(), Encoding.UTF8);
string x2 = System.IO.File.ReadAllText(FindFile(), Encoding.UTF8);
//Or if you already have the second file
//string x2 = System.IO.File.ReadAllText(#"C:\YourPath\someFileName.xml", Encoding.UTF8);
compare.comparison(x1, x2);
}