Solved

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

Posted on 2016-10-08
5
32 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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

20 Experts available now in Live!

Get 1:1 Help Now