My problem is that I have this upload queue, and when I'm putting x number amount of files in the queue it only shows me the last file because the for loop goes around too fast.
for (i = 0; i < uploadFileList.Count; i++)
{
if (percentage == 100)
{
projects_tab.IsEnabled = true;
wait_for_upload_text.Visibility = Visibility.Hidden;
ModelUploadTXT.Text = "Upload done!";
FooterProgressBar.Value = 0;
FooterProgressBar.Foreground = Brushes.LimeGreen;
cancel_upload_model_button.Visibility = Visibility.Hidden;
SelectedFileText.Text = "Choose model(s) to import!";
try
{
uploadClient.Dispose();
}
catch (Exception asd)
{
}
}
else
{
choosedProjetName = uploadFileList[i];
ShowHome();
cancel_upload_model_button.Visibility = Visibility.Visible;
ModelUploadTXT.Text = "Uploading " + choosedProjetName + FooterProgressBar.Value.ToString("f0") + " % " + (bytesIn / 1000000).ToString("f2") + "Mb /" + (totalBytes / 1000000).ToString("f2") + "Mb";
FooterProgressBar.Foreground = Brushes.Orange;
}
}
I've tried to use
Task.Delay(1)
System.Theading.Thread.Sleep(1)
to work it around, but they haven't worked. So what I basically need is to wait percentage to go 100 and then go to next index.
Try using While condition to hold the code.
for (i = 0; i < uploadFileList.Count; i++)
{
choosedProjetName = uploadFileList[i];
ShowHome();
cancel_upload_model_button.Visibility = Visibility.Visible;
ModelUploadTXT.Text = "Uploading " + choosedProjetName + FooterProgressBar.Value.ToString("f0") + " % " + (bytesIn / 1000000).ToString("f2") + "Mb /" + (totalBytes / 1000000).ToString("f2") + "Mb";
FooterProgressBar.Foreground = Brushes.Orange;
while (percentage < 100)
{
continue;
}
projects_tab.IsEnabled = true;
wait_for_upload_text.Visibility = Visibility.Hidden;
ModelUploadTXT.Text = "Upload done!";
FooterProgressBar.Value = 0;
FooterProgressBar.Foreground = Brushes.LimeGreen;
cancel_upload_model_button.Visibility = Visibility.Hidden;
SelectedFileText.Text = "Choose model(s) to import!";
try
{
uploadClient.Dispose();
}
catch (Exception asd)
{
}
}
Related
So im making a hangman gam3 it worked now im trying to make it w 2 players it aint working
He4e is the code for everybutton (letter)
When he clicks it
if (label13.Text == "Player 1 Guess")
{
if (Fullword.Contains(button2.Text))
{
char[] temp = Underscore_Word.ToCharArray();
char[] found = Fullword.ToCharArray();
for (int i = 0; i < found.Length; i++)
{
if (found[i] == button2.Text.ElementAt(0))
{
temp[i] = button2.Text.ElementAt(0);
}
}
Underscore_Word = new string(temp);
PrintLabel();
}
else
{
faux++;
}
if (faux < 7)
{
pictureBox1.Image = images[faux];
}
else
{
label1.Text = "you lose !!! ";
User.score--;
label7.Text = "Score of " + Methods.displayName + ": " + User.score.ToString();
label2.Visible = true;
label2.Text = "The word was :" + "\"" + Fullword + "\" ";
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "lose.wav";
player.Play();
}
if (Underscore_Word.Equals(Fullword))
{
label1.Text = "You Win!!";
User.score++;
label7.Text = "Score of " + Methods.displayName + ": " + User.score.ToString();
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "appllaud.wav";
player.Play();
}
label13.Text = "Player 2 Guess";
else
{
if (Fullword.Contains(button2.Text))
{
char[] temp = Underscore_Word.ToCharArray();
char[] found = Fullword.ToCharArray();
for (int i = 0; i < found.Length; i++)
{
if (found[i] == button2.Text.ElementAt(0))
{
temp[i] = button2.Text.ElementAt(0);
}
}
Underscore_Word = new string(temp);
PrintLabel();
}
else
{
faux_player2++;
}
if (faux_player2 < 7)
{
pictureBox2.Image = images[faux_player2];
}
else
{
label1.Text = "you lose !!! ";
User.score--;
label14.Text = "Score of " + Methods.displayName_playertwo + ": " + User.score_Player2.ToString();
label2.Visible = true;
label2.Text = "The word was :" + "\"" + Fullword + "\" ";
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "lose.wav";
player.Play();
}
if (Underscore_Word.Equals(Fullword))
{
label1.Text = "You Win!!";
User.score_Player2++;
label14.Text = "Score of " + Methods.displayName_playertwo + ": " + User.score_Player2.ToString(); ;
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "appllaud.wav";
player.Play();
}
label13.Text = "Player 1 Guess";
}
im having problems with the label its staying player 1 guess its alternating when the player gets a wrong guess please help
You are messing a } after the label13.Text = "Player 2 Guess"; before the else
if (label13.Text == "Player 1 Guess")
{
// code ....
}
else
{
// code ....
}
I would also Advice you to try to write cleaner code and try to not repeat yourself.
I am trying to use "Anviz Original SDK"
i added simple looping to connect multiple devices.
it was running okay if i only run it for 1 times, but in the second (sometimes the third) it keep crashing and showing erorr "Access Violation" at the output box of Visual Studio.
I've read some posts here on stackoverflow which suggest that there might be a bad pointer somewhere, but i cannot find where. I think My pointers are not corrupt.
The DLL Import for the function are using this code:
[DllImport("tc-b_new_sdk.dll")]
here is the function to loop and connect to device:
private void pictureBox3_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listViewDevice.Items)
{
if (item.Selected)
{
//int result = connectDevice(item.SubItems[3].Text, "5010");
int ret = 0;
byte[] Ipstr = new byte[16];
//string_to_byte(this.Add_dev_Ip.Text,Ipstr, (byte)Add_dev_Ip.Text.Length);
Ipstr = System.Text.Encoding.Default.GetBytes(item.SubItems[3].Text);
int Port = Convert.ToInt32("5010");
ret = AnvizNew.CCHex_ClientConnect(anviz_handle, Ipstr, Port);
}
}
}
and here is the Timer Tick function to get the data:
private void timer1_Tick(object sender, EventArgs e)
{
try
{
int ret = 0;
int[] Type = new int[1];
int[] dev_idx = new int[1];
IntPtr pBuff;
int len = 32000;
pBuff = Marshal.AllocHGlobal(len);
while (true)
{
if (anviz_handle == IntPtr.Zero)
{
break;
}
ret = AnvizNew.CChex_Update(anviz_handle, dev_idx, Type, pBuff, len);
//dbg_info("Update~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
if (ret > 0)
{
dbg_info("Msg Type : " + Type[0]);
switch (Type[0])
{
case (int)AnvizNew.MsgType.CCHEX_RET_DEV_LOGIN_TYPE:
{
AnvizNew.CCHEX_RET_DEV_LOGIN_STRU dev_info;
dev_info = (AnvizNew.CCHEX_RET_DEV_LOGIN_STRU)Marshal.PtrToStructure(pBuff, typeof(AnvizNew.CCHEX_RET_DEV_LOGIN_STRU));
string info_buff = "Dev Login --- [MachineId:" + dev_info.MachineId
+ " Version:" + byte_to_string(dev_info.Version)
+ " DevType:" + byte_to_string(dev_info.DevType)
+ " Addr:" + byte_to_string(dev_info.Addr)
+ "]";
//log_add_string(info_buff + " " + dev_info.DevTypeFlag.ToString());
log_add_string(info_buff + " " + Convert.ToString(dev_info.DevTypeFlag, 16));
ListViewItem foundItem = null;
if (foundItem == null)//just test has machineid
{
int i = 0;
for (i = 0; i < listViewDevice.Items.Count; i++)
{
foundItem = this.listViewDevice.Items[i];
if (foundItem.SubItems[1].Text.ToString() == dev_info.MachineId.ToString() && Convert.ToInt32(foundItem.Text.ToString()) != dev_info.DevIdx)
{
foundItem.Text = dev_info.DevIdx.ToString();
break;
}
else
{
}
//foundItem.Text = dev_info.DevIdx.ToString();
foundItem = null;
}
}
DevCount++;
//DevTypeFlag.Add(dev_info.DevIdx, (int)dev_info.DevTypeFlag);
}
break;
case (int)AnvizNew.MsgType.CCHEX_RET_DEV_LOGIN_CHANGE_TYPE:
{
AnvizNew.CCHEX_RET_DEV_LOGIN_STRU dev_info;
dev_info = (AnvizNew.CCHEX_RET_DEV_LOGIN_STRU)Marshal.PtrToStructure(pBuff, typeof(AnvizNew.CCHEX_RET_DEV_LOGIN_STRU));
string info_buff = "Login Change --- [MachineId:" + dev_info.MachineId
+ " Version:" + byte_to_string(dev_info.Version)
+ " DevType:" + byte_to_string(dev_info.DevType)
+ " Addr:" + byte_to_string(dev_info.Addr)
+ "]";
log_add_string(info_buff + " " + Convert.ToString(dev_info.DevTypeFlag, 16));
//log_add_string(info_buff + " " + dev_info.DevTypeFlag.ToString());
//DevTypeFlag[dev_info.DevIdx] = (int)dev_info.DevTypeFlag;
}
break;
case (int)AnvizNew.MsgType.CCHEX_RET_DEV_LOGOUT_TYPE:
{
AnvizNew.CCHEX_RET_DEV_LOGOUT_STRU dev_info;
dev_info = (AnvizNew.CCHEX_RET_DEV_LOGOUT_STRU)Marshal.PtrToStructure(pBuff, typeof(AnvizNew.CCHEX_RET_DEV_LOGOUT_STRU));
string info_buff = "Dev Logout --- [MachineId:" + dev_info.MachineId
+ " Version:" + byte_to_string(dev_info.Version)
+ " DevType:" + byte_to_string(dev_info.DevType)
+ " Live:" + dev_info.Live
+ " Addr:" + byte_to_string(dev_info.Addr)
+ "]";
log_add_string(info_buff);
DevCount--;
ListViewItem foundItem = null;
if (foundItem == null)//just test has machineid
{
int i = 0;
for (i = 0; i < listViewDevice.Items.Count; i++)
{
foundItem = this.listViewDevice.Items[i];
if (foundItem.SubItems[1].Text.ToString() == dev_info.MachineId.ToString() && Convert.ToInt32(foundItem.Text.ToString()) == dev_info.DevIdx)
{
break;
}
foundItem = null;
}
}
//ListViewItem foundItem = this.listViewDevice.FindItemWithText(dev_info.MachineId.ToString(), false, 0);
if (foundItem != null)
{
// DevTypeFlag.Remove(dev_idx[0]);
foundItem.Text = "0";
//this.listViewDevice.Items.Remove(foundItem);
}
}
break;
}
}
else if (0 == ret)
{
//MY_SLEEP(10);
//Marshal.FreeHGlobal(pBuff); // free the memory
break;
}
else if (0 > ret)
{
//buff to small
len = len * 2;
Marshal.FreeHGlobal(pBuff);
pBuff = Marshal.AllocHGlobal(len);
}
else
{
//没有找到消息类型
//Marshal.FreeHGlobal(pBuff); // free the memory
break;
}
//Marshal.FreeHGlobal(pBuff); // free the memory
}
//Marshal.FreeHGlobal(buff_fin);
Marshal.FreeHGlobal(pBuff); // free the memory
}
catch (Exception ex)
{
dbg_info("Error " + ex.Message);
}
}
Really appreciate for any helps.
Thanks.
I'm trying to wait for 20 seconds before adding + 1 value to i (Int), But i want to do it without Thread.Sleep.
This is my code, By the way I'm not a Pro programmer.
private void Refresh_App_TimerNH_Tick(object sender, EventArgs e)
{
label18.Text = "Timer Activated";
int i = 0;
i = i + 1;
if (i == 16)
{
i = 0;
}
else
{
}
if (i == 1)
{
webBrowser1.Refresh();
userIdLabel1.BackColor = Color.Red;
label20.Text = "+1";
//**i want to add 20 second gap here**
i = i + 1;
}
else
{
}
if (i == 2)
{
webBrowser2.Refresh();
userIdLabel2.BackColor = Color.Red;
label20.Text = "+2";
i = i + 1;
}
else
{
}
if (i == 3)
{
webBrowser3.Refresh();
userIdLabel3.BackColor = Color.Red;
label20.Text = "+3";
i = i + 1;
}
else
{
}
if (i == 4)
{
webBrowser4.Refresh();
userIdLabel4.BackColor = Color.Red;
label20.Text = "+4";
i = i + 1;
}
else
{
}
if (i == 5)
{
webBrowser5.Refresh();
userIdLabel5.BackColor = Color.Red;
label20.Text = "+5";
i = i + 1;
}
else
{
}
if (i == 6)
{
webBrowser6.Refresh();
userIdLabel6.BackColor = Color.Red;
label20.Text = "+6";
i = i + 1;
}
else
{
}
if (i == 7)
{
webBrowser7.Refresh();
userIdLabel7.BackColor = Color.Red;
label20.Text = "+7";
i = i + 1;
}
else
{
}
if (i == 8)
{
webBrowser8.Refresh();
userIdLabel8.BackColor = Color.Red;
label20.Text = "+8";
i = i + 1;
}
else
{
}
if (i == 9)
{
webBrowser9.Refresh();
userIdLabel9.BackColor = Color.Red;
label20.Text = "+9";
i = i + 1;
}
else
{
}
if (i == 10)
{
webBrowser10.Refresh();
userIdLabel10.BackColor = Color.Red;
label20.Text = "+10";
i = i + 1;
}
else
{
}
if (i == 11)
{
webBrowser11.Refresh();
userIdLabel11.BackColor = Color.Red;
label20.Text = "+11";
i = i + 1;
}
else
{
}
if (i == 12)
{
webBrowser12.Refresh();
userIdLabel12.BackColor = Color.Red;
label20.Text = "+12";
i = i + 1;
}
else
{
}
if (i == 13)
{
webBrowser13.Refresh();
userIdLabel13.BackColor = Color.Red;
label20.Text = "+13";
i = i + 1;
}
else
{
}
if (i == 14)
{
webBrowser14.Refresh();
userIdLabel14.BackColor = Color.Red;
label20.Text = "+14";
i = i + 1;
}
else
{
}
if (i == 15)
{
webBrowser15.Refresh();
userIdLabel15.BackColor = Color.Red;
label20.Text = "+15";
i = i + 1;
}
else
{
}
if (i == 16)
{
webBrowser16.Refresh();
userIdLabel16.BackColor = Color.Red;
label20.Text = "+16";
i = i + 1;
}
else
{
}
Refresh_App_TimerNH.Stop();
label18.Text = "Timer De-Activated";
Refresh_App_TimerNH.Start();
}
I think it might be easy but not for me, Because i'm new to c#
First off, Tim S's answer -- break up your logic into smaller chunks and simplify the timer logic -- is good. But to answer your specific question, which was "how do I delay between statements without Sleep", is: make the method async and then use await Task.Delay(...).
You are right to avoid Sleep; using it is a bad programming practice, and will hang your application. await Task.Delay does an asynchronous wait -- that is, the method returns immediately, the app keeps on processing UI messages, and when the delay is done, the program schedules the remainder of the method to execute later.
Note that during an asynchronous wait, by design messages keep on getting processed. If one of those messages causes your event handler to run again then you can get into the very confusing situation of having multiple control points in the same non-recursive method. Try to avoid that.
Right now your logic is convoluted, and probably very different from what you want it to do.
Your code will be much simpler if you put your webBrowserX and userIdLabelX items in some sort of list together.
public class MyPair
{
public WebBrowser WebBrowser { get; set; }
public Label UserIdLabel { get; set; }
}
private List<MyPair> pairs;
private int refreshIndex = 0;
private void StartTimer()
{
pairs = // populate pairs somehow
refreshIndex = 0;
var timer = new System.Windows.Forms.Timer();
timer.Interval = 20000
timer.Tick += MyTickHandler;
timer.Start();
label18.Text = "Timer Activated";
}
private void MyTickHandler(object sender, EventArgs e)
{
pairs[refreshIndex].WebBrowser.Refresh();
pairs[refreshIndex].UserIdLabel.BackColor = Color.Red;
label20.Text = "+" + (refreshIndex + 1);
refreshIndex = (refreshIndex + 1) % pairs.Count;
}
Note that this timer never deactivates, it loops through the list of pairs repeatedly.
I've created a form that allows for seat booking it has two dropdown menus and a label that changes showing them what seats they have booked. the label resets when I press the reset button but the drop down menus don't, they stay on the selected seats e.g. one menu saying "A" and the other saying "4" I need these to reset to blank can anyone help?
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 courseworkTask3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (comboBox1.SelectedIndex <= 0)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string temp = comboBox1.Text;
int dropBox1enter;
if (int.TryParse(temp, out dropBox1enter))
{
}
int i = 0;
int seatsFound = 0;
int maxFoundSeats = 0;
int maxFoundSeatsStart = 0;
int maxFoundSeatsEnd = 0;
bool[] rowA = new bool[10];
rowA[0] = true;
rowA[1] = true;
rowA[2] = false;
rowA[3] = false;
rowA[4] = false;
rowA[5] = true;
rowA[6] = true;
rowA[7] = false;
rowA[8] = false;
rowA[9] = false;
bool[] rowB = new bool[10];
rowB[0] = true;
rowB[1] = false;
rowB[2] = false;
rowB[3] = false;
rowB[4] = false;
rowB[5] = true;
rowB[6] = true;
rowB[7] = false;
rowB[8] = false;
rowB[9] = false;
bool[] rowC = new bool[10];
rowC[0] = true;
rowC[1] = false;
rowC[2] = false;
rowC[3] = false;
rowC[4] = false;
rowC[5] = true;
rowC[6] = true;
rowC[7] = false;
rowC[8] = false;
rowC[9] = false;
bool[] rowD = new bool[10];
rowD[0] = true;
rowD[1] = false;
rowD[2] = false;
rowD[3] = false;
rowD[4] = false;
rowD[5] = true;
rowD[6] = true;
rowD[7] = false;
rowD[8] = false;
rowD[9] = false;
bool[] rowE = new bool[10];
rowE[0] = true;
rowE[1] = false;
rowE[2] = false;
rowE[3] = false;
rowE[4] = false;
rowE[5] = true;
rowE[6] = true;
rowE[7] = false;
rowE[8] = false;
rowE[9] = false;
if (comboBox2.Text == "A")
{
while (i < 10 && maxFoundSeats < dropBox1enter)
{
if (rowA[i] == false)
{
seatsFound++;
if (seatsFound > maxFoundSeats)
{
maxFoundSeats = seatsFound;
maxFoundSeatsStart = 1 + i - seatsFound;
maxFoundSeatsEnd = i;
}
}
else
{
seatsFound = 0;
}
i++;
} if (maxFoundSeats == dropBox1enter)
{
label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row A starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";
}
else
{
label4.Text = "The seats you have selected are unavailable. #The maximum available is " + maxFoundSeats + " in row A From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
label4.Text = label4.Text.Replace("#", System.Environment.NewLine);
}
}
else
{
if (comboBox2.Text == "B")
{
while (i < 10 && maxFoundSeats < dropBox1enter)
{
if (rowB[i] == false)
{
seatsFound++;
if (seatsFound > maxFoundSeats)
{
maxFoundSeats = seatsFound;
maxFoundSeatsStart = 1 + i - seatsFound;
maxFoundSeatsEnd = i;
}
}
else
{
seatsFound = 0;
}
i++;
}
if (maxFoundSeats == dropBox1enter)
{
label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row B starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";
}
else
{
label4.Text = "The seats you have selected are unavailable. #The maximum available is " + maxFoundSeats + " in row B From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
label4.Text = label4.Text.Replace("#", System.Environment.NewLine);
}
}
else
{
if (comboBox2.Text == "C")
{
while (i < 10 && maxFoundSeats < dropBox1enter)
{
if (rowC[i] == false)
{
seatsFound++;
if (seatsFound > maxFoundSeats)
{
maxFoundSeats = seatsFound;
maxFoundSeatsStart = 1 + i - seatsFound;
maxFoundSeatsEnd = i;
}
}
else
{
seatsFound = 0;
}
i++;
}
if (maxFoundSeats == dropBox1enter)
{
label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row C starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";
}
else
{
label4.Text = "The seats you have selected are unavailable. #The maximum available is " + maxFoundSeats + " in row C From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
label4.Text = label4.Text.Replace("#", System.Environment.NewLine);
}
}
else
{
if (comboBox2.Text == "D")
{
while (i < 10 && maxFoundSeats < dropBox1enter)
{
if (rowD[i] == false)
{
seatsFound++;
if (seatsFound > maxFoundSeats)
{
maxFoundSeats = seatsFound;
maxFoundSeatsStart = 1 + i - seatsFound;
maxFoundSeatsEnd = i;
}
}
else
{
seatsFound = 0;
}
i++;
}
if (maxFoundSeats == dropBox1enter)
{
label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row D starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";
}
else
{
label4.Text = "The seats you have selected are unavailable. #The maximum available is " + maxFoundSeats + " in row D From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
label4.Text = label4.Text.Replace("#", System.Environment.NewLine);
}
}
else
{
if (comboBox2.Text == "E")
{
while (i < 10 && maxFoundSeats < dropBox1enter)
{
if (rowE[i] == false)
{
seatsFound++;
if (seatsFound > maxFoundSeats)
{
maxFoundSeats = seatsFound;
maxFoundSeatsStart = 1 + i - seatsFound;
maxFoundSeatsEnd = i;
}
}
else
{
seatsFound = 0;
}
i++;
}
if (maxFoundSeats == dropBox1enter)
{
label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row E starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";
}
else
{
label4.Text = "The seats you have selected are unavailable. #The maximum available is " + maxFoundSeats + " in row E From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
label4.Text = label4.Text.Replace("#", System.Environment.NewLine);
}
}
} //end E
} //end D
} //end C
}
}
private void dropBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void dropBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text != null && comboBox2.Text != null)
{
button1.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
label4.Text = "";
}
}
}
As #Rohit Prakash pointed out in the comments to your question, setting the SelectedIndex of a ComboBox to -1 will reset any selections to the Combobox:
(I'm assuming "button2" is your Reset button, you should really name your controls more appropriately)
private void button2_Click(object sender, EventArgs e)
{
label4.Text = "";
comboBox1.SelectedIndex = -1;
comboBox2.SelectedIndex = -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();