user1425470
user1425470

Reputation: 53

c# application becomes unresponsive when loading an image

I'm creating a tool that reads information from a file and also displays an image extracted from said file as well. it reads the info just fine and everything but when it comes to displaying the image it freezes the program without an error. the program just becomes unresponsive. the debugger doesn't say anything either except after some time it will say the thread has exited with still no response from the program.

i need to rename some stuff as i don't want to get in trouble

Here's the code I'm using to extract and display the image:

try
        {
            global.meta.filetype = STFSRead.readString_C(0, 4);
            textBox2.Text = global.meta.filetype;
            textBox3.Text = STFSRead.detectType(global.meta.contype.ToString("X4"));
            textBox4.Text = global.meta.metaversion.ToString();
            textBox5.Text = global.meta.id1;
            textBox6.Text = global.meta.version.ToString("X");
            textBox7.Text = global.meta.version2.ToString("X");
            textBox8.Text = global.meta.id2;
            textBox9.Text = global.meta.id3;
            textBox10.Text = global.meta.id4;
            textBox11.Text = global.meta.id5;
            textBox12.Text = global.meta.displayname;
            textBox13.Text = global.meta.titlename;
            textBox14.Text = STFSRead.detectSomeInfo(global.meta.aflag.ToString("X2"));
            pictureBox1.Image = STFSRead.loadImage();
        }
        catch
        {
            throw new Exception("What did you do?\n All this is suppose to do is read a file, how did you screw that up?");
        }

public static Image loadImage()
    {
        Exception failed = new Exception("LoadImage failed. Contact a developer");
        string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/appname/cache/";
        string imgname = global.meta.id.Replace(" ", "") + ".png";
        if (Directory.Exists(@path))
        {
            if (File.Exists(@path + imgname))
            {
                using (Image img = Image.FromFile(@path + imgname))
                {
                    return img;
                }
                throw failed;
            }
            else if (!File.Exists(@path + imgname))
            {
                if (extractData(imgname, 0x171A, 0x4000))
                {
                    using (Image img = Image.FromFile(@path + imgname))
                    {
                        return img;
                    }
                    throw failed;
                }
                else
                    throw failed;
            }
            else
                throw failed;
        }
        else if(!Directory.Exists(@path)){
            Directory.CreateDirectory(@path);
            if (File.Exists(@path + imgname))
            {
                using (Image img = Image.FromFile(@path + imgname))
                {
                    return img;
                }
                throw failed;
            }
            else if (!File.Exists(@path+imgname))
            {
                if (extractData(imgname, 0x171A, 0x4000))
                {
                    using (Image img = Image.FromFile(@path + imgname))
                    {
                        return img;
                    }
                    throw failed;
                }
                else
                    throw failed;
            }
            else
                throw failed;
        }
        else
            throw failed;
    }

public static bool extractData(string filename, long startpos, long length)
    {
        string data = STFSRead.readString_A(startpos, length);
        string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+"/appname/cache/";
        if (Directory.Exists(@path))
        {
            using (StreamWriter file = new StreamWriter(@path + filename))
            {
                file.Write(data);
                file.Close();
            }
        }
        else if (!Directory.Exists(@path))
        {
            Directory.CreateDirectory(@path);
            using (StreamWriter file = new StreamWriter(@path + filename))
            {
                file.Write(data);
                file.Close();
            }
        }
        if (File.Exists(@path + filename))
            return true;
        else
            throw new Exception("Couldn't extract file "+filename+" at location "+startpos+" with a length of "+length);
    }

public static string readString_A(long startpos, long length)
    {
        string s = "";
        for (long i = startpos; i < startpos + length; i = i++)
        {
            global.fs.Position = i;
            byte[] buf = new byte[1];
            global.fs.Read(buf, 0, 1);
            if (buf[0] == 0x00) // Null symbol
            {
            }
            else
            {
                char c = Convert.ToChar(buf[0]);
                s += c;
           }
        }
        if (s == "" || s == " ")
        {
            s = "";
        }
        return s;
    }

I have checked my appdata folder and while the directory gets created, the image does not get created so i'm thinking its failing during extraction. and none of my exceptions get triggered either

Edit: about the exceptions.... i added all of them when the program started screwing up to see if it will trigger them and narrow down where the error might be. i don't want it to handle those exceptions. might not be the best way to do that but i am still learning c# so i don't expect it to be the best way. if there is a better way to do this then let me know.

Upvotes: 1

Views: 225

Answers (1)

fcuesta
fcuesta

Reputation: 4520

I think your problem is here:

for (long i = startpos; i < startpos + length; i = i++)

It should be:

for (long i = startpos; i < startpos + length; i++)

i=i++ is not actually incremeting the variable

You can find why here: i = i++ doesn't increment i. Why?. Thanks to @RenniePet for the link.

Upvotes: 3

Related Questions