I need to find 2 closest edges to a point within a picturebox.I'm using the following method
void findclosestedges(Point p)
{
//Xedge=1 -- Left Edge is closer to Point 2--Right Edge
int Xedge,Yedge;
//Finding closest Left/Right Edge
if (p.X < (Width - p.X))
{
Xedge = 1;
LaunchOrigin2.closestedge.Text = " ";
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " left";
}
else
{
Xedge = 2;
LaunchOrigin2.closestedge.Text = " ";
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " right";
}
//Finding closest Top/Bottom Edge
if (p.Y < (Height - p.Y))
{
Yedge = 1;
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " top";
}
else
{
Yedge = 2;
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " bottom";
}
}
Is my approach correct? Are there any caveats for this approach?
Please advice.
As mentioned in the comments this might not be the correct place since your code is working, at least as far as i know?
But a few comments might be that i am not sure why you set the text to a value and then on the next row add to it?
LaunchOrigin2.closestedge.Text = " ";
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " left";
Why not just do it directly:
LaunchOrigin2.closestedge.Text = " " + " left";
Or of course the correct way:
LaunchOrigin2.closestedge.Text = " left";
I would also shorten down the code like the following:
void findclosestedges(Point p) {
int Xedge = p.X < (Width - p.X) ? 1 : 2;
int Yedge = p.Y < (Height - p.Y) ? 1 : 2;
LaunchOrigin2.closestedge.Text = (Xedge == 1 ? " left" : " right") + (Yedge == 1 ? " top" : " bottom");
}
But that is just my two cents...
Related
I am creating a multiplication table, when you input a number and click the calculate button it should display. I have tried watching a few tutorials on YouTube and have checked out some coding forums however I can only find people using the Console Application however I am using the Windows Form Application
1 * 1 = 1
2 * 1 = 2
3 * 1 = 3
4 * 1 = 4
5 * 1 = 5
6 * 1 = 6
7 * 1 = 7
8 * 1 = 8
9 * 1 = 9
10 * 1 = 10
However, when I run the program it only displays
1 * 10 = 10
Here is my code;
private void btnCalc_Click(object sender, EventArgs e)
{
int n, i;
n = Int32.Parse(txtNum.Text);
for (i = 1; i <= 10; ++i)
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
This loop keeps setting the control's text to a different value over and over, leaving you to see only the final value.
for (i = 1; i <= 10; ++i)
{
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
A straightforward solution is:
string text = "";
for (i = 1; i <= 10; ++i)
{
text += Convert.ToString(n + " * " + i + " = " + n * i);
}
txtCalc.Text = text;
You will still run into some formatting issues you'll need to solve, but you'll get the fundamental info in there.
You're overwriting the text over and over again. What you want to do is append new text every time through the loop. Try something like:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i) + Environment.NewLine;
}
your txtCalc.Text... overwrites the field in every iteration. You probably want something like this:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i);
}
I am trying to write to a CSV file, and have researched a bit on how to do this and it seems pretty straightforward but it is not populating my CSV file when I run it. The program will write to a txt and my console with no issue.
StreamWriter vWriteFile = new StreamWriter("Positions2.csv");
var path = #"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReader3\Position_2016_02_25.0415.csv";
if (vShowBoth == false)
{
//This determines whether to view by account or by underlying.
Console.WriteLine("Please enter 0 if you wish to see sums by account, or 12 if you wish to see by underlying");
int vViewControl = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Account" + " Settle Sum" + " Open Sum" + " Buy Sum" + " Sell Sum");
using (var parsedLines = File.ReadLines(path).Select(line => line.Split(',')).GetEnumerator())
{
bool vMore = parsedLines.MoveNext();
while (vMore)
{
// Initialize
bool vWasSuccessful;
var vAccount = parsedLines.Current[vViewControl];
double vSettleMMSum = 0;
double vOpenSum = 0;
int vBuySum = 0;
int vSellSum = 0;
do
{
double vParseSettleMM = 0;
double vParseOpen = 0;
int vParseBuy = 0;
int vParseSell = 0;
//Parsing data read in from strings, into temp variables
vWasSuccessful = double.TryParse(parsedLines.Current[7], out vParseSettleMM);
vWasSuccessful = double.TryParse(parsedLines.Current[8], out vParseOpen);
vWasSuccessful = int.TryParse(parsedLines.Current[6], out vParseBuy);
vWasSuccessful = int.TryParse(parsedLines.Current[10], out vParseSell);
//adding temp variabels to sum
vSettleMMSum += vParseSettleMM;
vOpenSum += vParseOpen;
vBuySum += vParseBuy;
vSellSum += vParseSell;
vMore = parsedLines.MoveNext();
}
//sets up when to break
while (vMore && vAccount == parsedLines.Current[vViewControl]);
//After each Break need to print out Account name and sums from above.
// Do printing here as part of the loop, at the very end of the loop code block.
Console.WriteLine("--------------------------------------------------------");
Console.WriteLine(vAccount + " " + vSettleMMSum + " " + vOpenSum + " " + vBuySum + " " +
vSellSum);
//vWriteFile.Write(vAccount + "," + vSettleMMSum + "," + vOpenSum + "," + vBuySum + "," +
// vSellSum);
vWriteFile.WriteLine("{0},{1},{2},{3},{4}", vAccount, vSettleMMSum, vOpenSum, vBuySum, vSellSum);
//reset sums for next loop
vSettleMMSum = 0;
vOpenSum = 0;
vBuySum = 0;
vSellSum = 0;
}
}
}
You should make it a habit to wrap your streams with the using keyword so you don't run into situations where you forget to close it out. It's a good practice to use the using keyword around anything that inherits IDisposable
So your code would look like this.
using(StreamWriter vWriteFile = new StreamWriter("Positions2.csv"))
{
var path = #"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CSVReader3\Position_2016_02_25.0415.csv";
if (vShowBoth == false)
{
//This determines whether to view by account or by underlying.
Console.WriteLine("Please enter 0 if you wish to see sums by account, or 12 if you wish to see by underlying");
int vViewControl = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Account" + " Settle Sum" + " Open Sum" + " Buy Sum" + " Sell Sum");
using (var parsedLines = File.ReadLines(path).Select(line => line.Split(',')).GetEnumerator())
{
bool vMore = parsedLines.MoveNext();
while (vMore)
{
// Initialize
bool vWasSuccessful;
var vAccount = parsedLines.Current[vViewControl];
double vSettleMMSum = 0;
double vOpenSum = 0;
int vBuySum = 0;
int vSellSum = 0;
do
{
double vParseSettleMM = 0;
double vParseOpen = 0;
int vParseBuy = 0;
int vParseSell = 0;
//Parsing data read in from strings, into temp variables
vWasSuccessful = double.TryParse(parsedLines.Current[7], out vParseSettleMM);
vWasSuccessful = double.TryParse(parsedLines.Current[8], out vParseOpen);
vWasSuccessful = int.TryParse(parsedLines.Current[6], out vParseBuy);
vWasSuccessful = int.TryParse(parsedLines.Current[10], out vParseSell);
//adding temp variabels to sum
vSettleMMSum += vParseSettleMM;
vOpenSum += vParseOpen;
vBuySum += vParseBuy;
vSellSum += vParseSell;
vMore = parsedLines.MoveNext();
}
//sets up when to break
while (vMore && vAccount == parsedLines.Current[vViewControl]);
//After each Break need to print out Account name and sums from above.
// Do printing here as part of the loop, at the very end of the loop code block.
Console.WriteLine("--------------------------------------------------------");
Console.WriteLine(vAccount + " " + vSettleMMSum + " " + vOpenSum + " " + vBuySum + " " +
vSellSum);
//vWriteFile.Write(vAccount + "," + vSettleMMSum + "," + vOpenSum + "," + vBuySum + "," +
// vSellSum);
vWriteFile.WriteLine("{0},{1},{2},{3},{4}", vAccount, vSettleMMSum, vOpenSum, vBuySum, vSellSum);
//reset sums for next loop
vSettleMMSum = 0;
vOpenSum = 0;
vBuySum = 0;
vSellSum = 0;
}
}
}
}
I am working on an asp.net project, it takes values from the front end, stores them as string in a matrix. When the matrix size is greater than 5 * 5, it keep returns an InvalidCastException (It works fine on 5 * 5 matrix and under).
The code is attached below, with a screen shot of Exception:
public partial class WebForm1 : System.Web.UI.Page
{
public DataSet relmatrix = new DataSet();
public DataTable rt = new DataTable();
public Double[] inconsistency;
public int nodersnum;
public string strrel;
protected void Button1_Click(object sender, EventArgs e)
{
nodersnum = Convert.ToInt16(count.Text);
switch (nodersnum)
{
case 1:
break;
case 2:
{ strrel = RelationAB2.Text; }
break;
case 3:
{ strrel = RelationAB3.Text + " " + RelationAC3.Text + " " + RelationBC3.Text; }
break;
case 4:
{ strrel = RelationAB4.Text + " " + RelationAC4.Text + " " + RelationAD4.Text + " " + RelationBC4.Text + " " + RelationBD4.Text + " " + RelationCD4.Text; }
break;
case 5:
{ strrel = RelationAB5.Text + " " + RelationAC5.Text + " " + RelationAD5.Text + " " + RelationAE5.Text + " " + RelationBC5.Text + " " + RelationBD5.Text + " " + RelationBE5.Text + " " + RelationCD5.Text + " " + RelationCE5.Text + " " + RelationDE5.Text; }
break;
case 6:
{ strrel = RelationAB6.Text + " " + RelationAC6.Text + " " + RelationAD6.Text + " " + RelationAE6.Text + " " + RelationAF6.Text + " " + RelationBC6.Text + " " + RelationBD6.Text + " " + RelationBE6.Text + " " + RelationBF6.Text + " " + RelationCD6.Text + " " + RelationCE6.Text + " " + RelationCF6.Text + " " + RelationDE6.Text + " " + RelationDF6.Text + " " + RelationEF6.Text; }
break;
case 7:
{ strrel = RelationAB7.Text + " " + RelationAC7.Text + " " + RelationAD7.Text + " " + RelationAE7.Text + " " + RelationAF7.Text + " " + RelationAG7.Text + " " + RelationBC7.Text + " " + RelationBD7.Text + " " + RelationBE7.Text + " " + RelationBF7.Text + " " + RelationBG7.Text + " " + RelationCD7.Text + " " + RelationCE7.Text + " " + RelationCF7.Text + " " + RelationCG7.Text + " " + RelationDE7.Text + " " + RelationDF7.Text + " " + RelationDG7.Text + " " + RelationEF7.Text + " " + RelationEG7.Text + " " + RelationFG7.Text; }
break;
default:
{ strrel = ""; }
break;
}
GenerateTable.generatetable(relmatrix, strrel, rt, nodersnum);
Class1.generatePC(nodersnum, relmatrix);
int num = 0;
double maxincon = 0.0;
int mi = 0, mj = 0, mk = 0;
inconsistency = new Double[43] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
//PROBLEM
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array
//Get the biggest inconsistency number
if (inconsistency[num] >= maxincon)
{
maxincon = inconsistency[num];
mi = i;
mj = j;
mk = k;
}
num++;
}
}
}
}
Class1.sort(inconsistency);//sort inconsistency array
while (inconsistency[0] > 0.3333333)
{
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
// PROBLEM
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
if (inconsistency[0] == (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a))))//calculate the inconsistency value and store in the inconsistency array
{
if ((b * c) < a)
{
double A = (b * c) / ((a + b + c) * (a + b + c));
double B = (a + 2 * b * c) / (a + b + c);
double C = b * c - a;
double m = B * B - 4 * A * C;
if (m < 0)
{
Console.Write("error");
break;
}
else
{
double x1 = (-1 * B + System.Math.Sqrt(m)) / (2 * A);
double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
if ((x1 > 0) && (x2 < 0))
{
b = (float)(b + (b * x1) / (a + b + c));
c = (float)(c + (c * x1) / (a + b + c));
a = (float)(a - (a * x1) / (a + b + c));
}
else if ((x1 < 0) && (x2 > 0))
{
b = (float)(b + (b * x2) / (a + b + c));
c = (float)(c + (c * x2) / (a + b + c));
a = (float)(a - (a * x2) / (a + b + c));
}
else if ((x1 > 0) && (x2 > 0))
{
double x = Class1.Min((float)x1, (float)x2);
b = (float)(b + (b * x) / (a + b + c));
c = (float)(c + (c * x) / (a + b + c));
a = (float)(a - (a * x) / (a + b + c));
}
else if ((x1 < 0) && (x2 < 0))
{
break;
}
}
}
else if ((b * c) > a)
{
double A = (b * c) / ((a + b + c) * (a + b + c));
double B = -1 * (a + 2 * b * c) / (a + b + c);
double C = b * c - a;
double m = B * B - 4 * A * C;
if (m < 0)
{
Console.Write("error");
break;
}
else
{
double x1 = (-1 * B + Math.Sqrt(m)) / (2 * A);
double x2 = (-1 * B - Math.Sqrt(m)) / (2 * A);
if ((x1 > 0) && (x2 < 0))
{
b = (float)(b - (b * x1) / (a + b + c));
c = (float)(c - (c * x1) / (a + b + c));
a = (float)(a + (a * x1) / (a + b + c));
}
else if ((x1 < 0) && (x2 > 0))
{
b = (float)(b - (b * x2) / (a + b + c));
c = (float)(c - (c * x2) / (a + b + c));
a = (float)(a + (a * x2) / (a + b + c));
}
else if ((x1 > 0) && (x2 > 0))
{
double x = Class1.Min((float)x1, (float)x2);
b = (float)(b - (b * x) / (a + b + c));
c = (float)(c - (c * x) / (a + b + c));
a = (float)(a + (a * x) / (a + b + c));
}
else if ((x1 < 0) && (x2 < 0))
{
break;
}
}
}
}
relmatrix.Tables["relTable"].Rows[i][j] = Convert.ToString(a);
relmatrix.Tables["relTable"].Rows[i][k] = Convert.ToString(b);
relmatrix.Tables["relTable"].Rows[k][j] = Convert.ToString(c);
}
}
}
}
num = 0;
maxincon = 0.0;
mi = 0;
mj = 0;
mk = 0;
for (int i = 0; i < nodersnum - 2; i++)
{
for (int k = i + 1; k < nodersnum - 1; k++)
{
for (int j = k + 1; j < nodersnum; j++)
{
if ((Convert.ToString(relmatrix.Tables["relTable"].Rows[i][j]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[i][k]) != " ") && (Convert.ToString(relmatrix.Tables["relTable"].Rows[k][j]) != " "))
{
Double a = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);//obtain value from the matrix
Double b = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][k]);//obtain value from the matrix
Double c = Convert.ToDouble(relmatrix.Tables["relTable"].Rows[k][j]);//obtain value from the matrix
inconsistency[num] = (Class1.Min(System.Math.Abs(1 - a / (b * c)), System.Math.Abs(1 - (b * c) / a)));//calculate the inconsistency value and store in the inconsistency array
// Get the biggest inconsistency number
if (inconsistency[num] >= maxincon)
{
maxincon = inconsistency[num];
mi = i;
mj = j;
mk = k;
}
num++;
}
}
}
}
//sort inconsistency array
Class1.sort(inconsistency);
}
//Fill up the whole pairwise comparsion matrix, when row=col, the value =1, when row<col the vaule[col][row] is "1/"[col][row]
//nodersnum is how many nodes, row is the matrix row, col is column of matrix
for (int row = 0; row < nodersnum; row++)
{
for (int col = row; col < nodersnum; col++)
{
if (row < col)
{
//set the the value of lower matrix
relmatrix.Tables["relTable"].Rows[col][row] = "1/" + relmatrix.Tables["relTable"].Rows[row][col];
}
//set the value of diagnoal
else if (row == col)
{
relmatrix.Tables["relTable"].Rows[row][col] = "1";
}
}
}
//compute the weight of each element
Double[] rowproduct;
rowproduct = new Double[7] { 1, 1, 1, 1, 1, 1, 1 };
for (int i = 0; i < nodersnum; i++)
{
for (int j = 0; j < nodersnum; j++)
{
if (i >= j)
{
rowproduct[i] = rowproduct[i] / Convert.ToDouble(relmatrix.Tables["relTable"].Rows[j][i]);
}
else
{
rowproduct[i] = rowproduct[i] * Convert.ToDouble(relmatrix.Tables["relTable"].Rows[i][j]);
}
}
}
Double[] num1;
num1 = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };
double numsum = 0;
//compute each row total number(power of node number)
for (int i = 0; i < nodersnum; i++)
{
num1[i] = Math.Pow(rowproduct[i], 1 / (double)nodersnum);
numsum = numsum + num1[i];
}
//transfer into the number of percentage
Double[] weight;
weight = new Double[7] { 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < nodersnum; i++)
{
weight[i] = (int)Math.Round(100 * num1[i] / numsum * 100) / 100f;
Console.WriteLine(weight[i]);
}
GridView2.DataSource = relmatrix;
GridView2.DataBind();
this.GridView2.Rows[mi].Cells[mj].BackColor = System.Drawing.Color.Red;
this.GridView2.Rows[mi].Cells[mk].BackColor = System.Drawing.Color.Red;
this.GridView2.Rows[mk].Cells[mj].BackColor = System.Drawing.Color.Red;
Label3.Text = "Maximum Inconsistency after Reduction: " + Convert.ToString(inconsistency[0]);
TextBox1.Text = Convert.ToString(weight[0]);
TextBox2.Text = Convert.ToString(weight[1]);
TextBox3.Text = Convert.ToString(weight[2]);
TextBox4.Text = Convert.ToString(weight[3]);
TextBox5.Text = Convert.ToString(weight[4]);
TextBox6.Text = Convert.ToString(weight[5]);
TextBox7.Text = Convert.ToString(weight[6]);
}
}
Looks like your matrix contains null values. You cannot cast them into Double.
Check like this:
if (relmatrix.Tables["relTable"].Rows[i][k] != DBNull.Value)
Or
if (relmatrix.Tables["relTable"].Rows[i][k] != null)
Use nullable double as shown below:
var mynull = DBNull.Value;
Double? c = Convert.IsDBNull(mynull) ? (double?)null : Convert.ToDouble(mynull);
In your case:
Double? c = Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) ? (double?)null : Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);;
Alternatively using if-else:
if(Convert.IsDBNull(relmatrix.tables["relTable"].Rows[k][j]) == true)
{
Double? c = (double?)null;
}
else
{
Double? c = Convert.ToDouble(relmatrix.tables["relTable"].Rows[k][j]);
}
I am trying to implement Best fit memory allocation. I have done the code, which i think should work but somehow it appears its getting stuck in the loop. I can't figure out why. The following is my code. I want to allocate jobs to the memory with the least memory waste.
public void bestFit(int job_size)
{
string str1 = "";
string str2 = "";
string str3 = "";
int memory_block = 99999;
int subscript = 0;
int initial_memory_waste = memory_block - job_array[0];
int job_counter = 0;
int counter = 1;
int memory_waste = 0;
while (counter <= memory_array.Length)
{
if (job_size > memory_array[counter - 1])
{
counter += 1;
}
else
memory_waste = memory_array[counter - 1] - job_size;
{
if (initial_memory_waste > memory_waste)
{
subscript = counter;
initial_memory_waste = memory_waste;
counter += 1;
}
}
}
queued_jobs = counter;
str3 = ("Job number: " + (queued_jobs).ToString() + " of size: " + job_size.ToString());
if (job_counter < job_array.Length)
{
bf_waiting_queue.Add(str3);
}
else
{
str1 = ("Job number: " + (job_counter).ToString() + " of size: " + job_size.ToString() + " is allocated to Memory block: " + (subscript).ToString() + " of size: " + memory_array[subscript - 1]).ToString();
memory_waste = memory_array[subscript - 1] - job_size;
str2 = ("Memory waste is: " + memory_waste.ToString());
bf_total_memory_waste += memory_waste;
memory_array[counter - 1] = (memory_array[counter - 1] - job_size);
bf_jobsInMemory.Add(str1 + "\t" + str2);
job_counter += 1;
counter = 1;
}
}
Aside from the braces issue others pointed out, your logic makes it possible for you to never increment 'counter', which is why you are stuck in the loop. The following reorganization is cleaner and guarantees you always increment 'counter'. Also, a couple of comments couldn't hurt.
while (counter <= memory_array.Length)
{
// If block fits, consider it
if (job_size <= memory_array[counter - 1])
{
memory_waste = memory_array[counter - 1] - job_size;
// If this block is less wasteful, remember it
if (initial_memory_waste > memory_waste)
{
subscript = counter;
initial_memory_waste = memory_waste;
}
}
// Next block
counter += 1;
}
This is the piece of code i have wriiten. It takes images from the directory and pixel matched it. When the program read 5 images from the directory of same pixels it wor completely fine but when we increase the number of images or images with different pixels it throws the exception "Argumentexcepton was unhandled". Please help
string[] image_name = new string [1000] ;
check = d.GetFiles("*.jpg").Length;
while (imageArray < check)
{
double percentage = 0;
count1 = 0;
count2 = 0;
progressBar1.Value++;
//error occurs on this line
img2 = new Bitmap(image_name[imageArray]);
progressBar1.Maximum = check;
if (img1.Width != img2.Width && img1.Height != img2.Height)
{
if (img2.Width > img1.Width || img2.Height > img1.Height)
{
img1 = ResizeBitmap(img1, img2.Width, img2.Height);
}
else
img1 = ResizeBitmap(img1, img2.Width, img2.Height);
}
for (int i = 0; i < img1.Width; i++)
{
for (int j = 0; j < img1.Height; j++)
{
img1_ref = img1.GetPixel(i, j).ToString();
img2_ref = img2.GetPixel(i, j).ToString();
if (img1_ref != img2_ref)
{
count2++;
break;
}
count1++;
}
}
percentage = (count2 + count1);
percentage = count1 / percentage;
percentage = percentage * 100;
if (percentage < 90)
{
msg1 = "Sorry, Images are not same , " + count1 + " same pixels found and " + count2 + " wrong pixels found";
}
else if (percentage == 100)
{
msg1 = " Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found";
ResultImagePanel rmp = new ResultImagePanel(image_name[imageArray], Convert.ToString(percentage) + "%", folderPath + "\\" + image_name[imageArray], msg1);
rmp.Location = new Point(0, 345 * counter);
panel1.Controls.Add(rmp);
}
else
{
msg1 = " Similar images , " + count1 + " same pixels found and " + count2 + " wrong pixels found";
ResultImagePanel rmp = new ResultImagePanel(image_name[imageArray], Convert.ToString(percentage) + "%", folderPath + "\\" + image_name[imageArray], msg1);
rmp.Location = new Point(0, 345 * counter);
panel1.Controls.Add(rmp);
}
counter++;
imageArray++;
}
}
image_name[imageArray] is string.Empty when that code is executed. Hence the error. You will need to fill in the image_name array with valid file names, before executing the code.
Implement your array image_name like this :
image_name = Directory.EnumerateFiles(path, "*.jpg").ToArray();