Solved

how can i unlock a picture file that was loaded in a picturebox in visual c#?

Posted on 2016-10-08
5
39 Views
Last Modified: 2016-11-27
I am developing a mini game in which the user must recognize the person's face and type in that person's name. Each picture is "saved" in a folder with the person's name, so the game uses both the picture and the filename... This is my code :

using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace FaceNameGame
{
    public partial class LevelOnePartTwo : Form
    {
        int point = 0, buttoncount = 0, i=0, h=3, hbuttoncount=0;
        public LevelOnePartTwo()
        {
            InitializeComponent();
        }

        private void LevelOnePartTwo_Load(object sender, EventArgs e)
        {

            string[] files = Directory.GetFiles(@"C:\Users\Marina\Desktop\FaceNameGame\images\Level1\used", "*.jpg", SearchOption.AllDirectories);
            PhotoPicBx.Load(files[i]);
            NameTxtBx.Focus();
            Pointlbl.Text = point.ToString();
            

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void PhotoPicBx_Click(object sender, EventArgs e)
        {

        }

        private void TemplatePicBx_Click(object sender, EventArgs e)
        {

        }

        private void Hintbtn_Click(object sender, EventArgs e)
        {
            hbuttoncount++;
            h--;
            string nameused21 = PhotoPicBx.ImageLocation.ToString();
            string nameused22 = nameused21.Substring(nameused21.LastIndexOf("\\"));
            string nameused23 = nameused22.Remove(nameused22.Length - 4, 4);
            string nameused24 = nameused23.Remove(0, 1);
            string nameused2h = nameused24.Remove(nameused24.Length - h, h);
            NameTxtBx.Text = nameused2h;
            
            if (point > 0 && h>=0)
            {
                point = point - 5;
                Pointlbl.Text = point.ToString();
            }

            if (h <= -1)
            {
                h = 0;
            }

            if (hbuttoncount == 3)
            {
                Hintbtn.Hide();
            }
        }

        int s = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            s++;
            int m = s / 60;
            int h = m / 60;
            Timelbl.Text = "" + h.ToString() + " : " + m.ToString() + " : " + s.ToString();
        }

        private void Checkbtn_Click(object sender, EventArgs e)
        {

            string nameused1 = PhotoPicBx.ImageLocation.ToString();
            string nameused2 = nameused1.Substring(nameused1.LastIndexOf("\\"));
            string nameused3 = nameused2.Remove(nameused2.Length - 4, 4);
            string nameused = nameused3.Remove(0, 1);

            int comp = NameTxtBx.Text.CompareTo(nameused);


            if (comp == 0)
            {
                point = point + 50;
                Pointlbl.Text = point.ToString();
                NameTxtBx.Text = "";
                buttoncount++;




                if (buttoncount <= 2)
                {
                    if (i < 2)
                    {
                        i++;
                        string[] files = Directory.GetFiles(@"C:\Users\Marina\Desktop\FaceNameGame\images\Level1\used", "*.jpg", SearchOption.AllDirectories);
                        PhotoPicBx.Load(files[i]);
                        NameTxtBx.Focus();
                        h = 3;
                        hbuttoncount = 0;
                        Hintbtn.Show();

                    }

                }
            }

            int pointcount = Int32.Parse(Pointlbl.Text);


            if (buttoncount > 2)
            {

                SqlConnection sqlConnection1 = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Marina\\Desktop\\FaceNameGame\\facenamegame\\mygamedb.mdf;Integrated Security=True;Connect Timeout=30");
                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                DateTime time = DateTime.Now;              // Use current time
                cmd.CommandText = "insert into Userrecords (Username, Points, Logindate,Level) values(@Username, @Points, @Time, 'Level 1')";
                cmd.Connection = sqlConnection1;





                {


                    cmd.Parameters.AddWithValue("@Username",LoginInfo.UserID );
                    cmd.Parameters.AddWithValue("@Points", pointcount);
                    cmd.Parameters.AddWithValue("@Time", time);
                   
                



                    sqlConnection1.Open();
                    cmd.ExecuteNonQuery();
                    sqlConnection1.Close();
                   
                }


                if (pointcount >= 130)
                {
                    LevelOneSuccessScreen lvlonesucscreen = new LevelOneSuccessScreen();
                    lvlonesucscreen.Show();
                    this.Close();
                }

              if (pointcount < 130)
                {
                    LevelOneRepeatScreen lvlonerepscreen = new LevelOneRepeatScreen();
                    lvlonerepscreen.Show();
                    this.Close();
                   
                }
        }
        }



    }
}

Open in new window

The problem is that this form redirects to the first form that must delete the whole folder of pictures and creates a new one (with the same name but with different photos, when the player must repeat the level)...but Visual Studio always throws an exception that the last picture is used by another process and  can't be deleted...Can i fix this in a way that i can still use the picture with its filename (i mean each photo must be shown with its name underneath)?
0
Comment
Question by:Marina K
  • 2
5 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 41836190
I'm not sure how to fix. But obviously if a file (image) is used by a picture box, e.g., you can't delete it. And you can't really do anything about it.

So what would be a possible way to solve the problem?
1. Unload the image, may be load some "temp" picture while you are creating a new folder with different images. And as soon as your images become unlocked - delete current folder and create a new one.

2. Use two folders with different names interchangeably:
you have a folder A with images for your controls,
create a folder B with new images,
reload images to controls
clear the A folder
then repeate in the same order but starting from B...
0
 
LVL 11

Expert Comment

by:louisfr
ID: 41836414
If you use the ImageLocation property instead of the Load method, the file should not be locked.
0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
ID: 41853210
0
 
LVL 11

Expert Comment

by:louisfr
ID: 41900596
Ark's comment explains why the author has the problem. Mine explains how to solve it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now