Show image in picturebox according to search criteria

0
 public void ShowAssistanceInfo(string Name, string EmployeeNO)
    {
        try
        {
            string Path = "ruta donde estan las imagenes";

            string[] filePaths = Directory.GetFiles(Path, EmployeeNO + "*.jpg");

            if (filePaths.Length > 0 )
            {
                picBox.ImageLocation = filePaths[0].ToString();
            }

        }
        catch (Exception exc)
        {
            MessageBox.Show(" ERROR!!!! Form=" + this.Name + " , Method = " + System.Reflection.MethodInfo.GetCurrentMethod().Name + ", Error Message = " + exc.Message);
        }
    }

The above code is used to show images in a picturebox according to what you say in a textbox, you are using Active Directory to get information from users, what you do is the following:

When typing 1111 you must load the image of the employee 1111 as well as your info in the AD User Info zone (the image is in a route of the equipment stored as the employee number ie 1111.jpg), when typing pperez it is the employee 1111 must likewise be able to see his info and photo and if Pedro Perez is typed the same!

Only the part of the photo has been achieved when typing the employee number you want to achieve from the other 2 forms that are mentioned but the solution has not been found.

Also when looking for a user that no longer exists in AD or that is disable, a message box is displayed indicating that the user was not found, until there is all right, but if the image of that user exists in the image path the image is loaded even if your info is not displayed and the messagebox has been displayed.

How could these problems be solved?

PS: If any other part of the code is necessary to be able to provide some kind of help, please let me know, thank you very much

This code is what you have to capture the AD info and then show it in the form:

private void ShowUserInfo()  /*esto nos va a servir mostrar la info de los usuarios en base a si se busca por username o employeeID 
                                                            y si no existe en AD simplemente dice que no se encontro*/
        {
            Cursor.Current = Cursors.Default;
            pnlBlock.BringToFront();
            pnlBlock.Visible = true;

            SearchResult SR = null;

            if (txtSearchUser.Text.Trim().IndexOf("0") >= 0)
                SR = BusqPorEmployNum(GetDirectorySearcher(), txtSearchUser.Text.Trim());
            else
                SR = BusqPorUserName(GetDirectorySearcher(), txtSearchUser.Text.Trim());

            if (SR != null)
                GetUserInfo(SR);
            else
                MessageBox.Show("User Not Found, Try again!", "Search Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void GetUserInfo(SearchResult SR) // obtiene la info de los usuarios de AD y los carga a los label 
        {
            Cursor.Current = Cursors.Default;
            pnlBlock.Visible = false;

            if (SR.GetDirectoryEntry().Properties["samaccountname"].Value != null)
                lblUsername.Text = "Username : " + SR.GetDirectoryEntry().Properties["samaccountname"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["givenName"].Value != null)
                lblName.Text = "First Name : " + SR.GetDirectoryEntry().Properties["givenName"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["initials"].Value != null)
                lblMidName.Text = "Middle Name : " + SR.GetDirectoryEntry().Properties["initials"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["sn"].Value != null)
                lblLastName.Text = "Last Name : " + SR.GetDirectoryEntry().Properties["sn"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["mail"].Value != null)
                lblEmail.Text = "Email ID : " + SR.GetDirectoryEntry().Properties["mail"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["title"].Value != null)
                lblTitle.Text = "Title : " + SR.GetDirectoryEntry().Properties["title"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["company"].Value != null)
                lblCompany.Text = "Company : " + SR.GetDirectoryEntry().Properties["company"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["l"].Value != null)
                lblCity.Text = "City : " + SR.GetDirectoryEntry().Properties["l"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["employeeNumber"].Value != null)
                lblEmplNum.Text = "Employee Number : " + SR.GetDirectoryEntry().Properties["employeeNumber"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["co"].Value != null)
                lblCountry.Text = "Country : " + SR.GetDirectoryEntry().Properties["co"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["postalCode"].Value != null)
                lblPostcod.Text = "Postal Code : " + SR.GetDirectoryEntry().Properties["postalCode"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["telephoneNumber"].Value != null)
                lblTelef.Text = "Telephone No. : " + SR.GetDirectoryEntry().Properties["telephoneNumber"].Value.ToString();

            if (SR.GetDirectoryEntry().Properties["uidNumber"].Value != null)
                lblUid.Text = "Batch Number : " + SR.GetDirectoryEntry().Properties["uidNumber"].Value.ToString();

        }

Also, in the following image I am trying to be as detailed as possible regarding my question:

It does not matter if it is entered in the search engine (as exemplified in the image) the image must always appear because the image is saved in the folder that I mentioned at the beginning of my question by the employee number, that is, if I look for 0001, pperez or Pedro Perez should always appear the image because I should always compare it with the employee number of what is being searched, also I would like to know how I can do to avoid appearing the image of an employee number that does not appear in AD This is because we have already done it with the code we have and if the image exists in the route, it is always shown even though it does not exist in AD

    
asked by sullivan96 26.01.2018 в 18:48
source

1 answer

0

What does not appear in your code is the call to the ShowAssistanceInfo method that loads the image.

I understand that you should do it in the GetUserInfo method that is responsible for filling in the user's information in the form. Just as you give value to labels, you should do so with the image.

You could do something like this:

if (SR.GetDirectoryEntry().Properties["employeeNumber"].Value != null)
    var employNumber = SR.GetDirectoryEntry().Properties["employeeNumber"].Value.ToString();
    lblEmplNum.Text = "Employee Number : " + employNumber;
    if (File.Exists(Path.Combine(image_folder_path, $"{employNumber}.jpg")){
        picBox.ImageLocation = Path.Combine(image_folder_path, $"{employNumber}.jpg");
    }
    else{
        picBox.ImageLocation = Path.Combine(image_folder_path, "noImage.jpg");
    }
}
else{
    picBox.ImageLocation = Path.Combine(image_folder_path, "noImage.jpg");
}

When recovering the employee number, it is checked if the image file exists and it is loaded in PictureBox . If the file does not exist or the employee number has no value, a default image is loaded.

    
answered by 29.01.2018 / 15:54
source