1

我正在创建一个从文件中读取信息并显示从所述文件中提取的图像的工具。它可以很好地读取信息和所有内容,但是在显示图像时它会冻结程序而不会出现错误。该程序只是变得没有响应。调试器也什么也没说,除非一段时间后它会说线程已经退出,但程序仍然没有响应。

我需要重命名一些东西,因为我不想惹麻烦

这是我用来提取和显示图像的代码:

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;
    }

我检查了我的 appdata 文件夹,并且在创建目录时,没有创建图像,所以我认为它在提取过程中失败。而且我的任何异常都没有被触发

编辑:关于异常......当程序开始搞砸时,我添加了所有异常,看看它是否会触发它们并缩小错误可能出现的位置。我不希望它处理这些异常。可能不是最好的方法,但我仍在学习 c#,所以我不认为它是最好的方法。如果有更好的方法可以做到这一点,请告诉我。

4

1 回答 1

3

我认为你的问题在这里:

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

它应该是:

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

i=i++实际上并没有增加变量

你可以在这里找到原因:i = i++ 不会增加 i。为什么?. 感谢@RenniePet 提供链接。

于 2013-08-02T01:38:40.540 回答