我正在创建一个工具,用于从文件中读取信息,并且还显示从所述文件中提取的图像。它读取信息就好了,但是当涉及到显示图像时,它会冻结程序而不会出错。该计划变得没有反应。调试器没有说什么,除了一段时间之后它会说线程已经退出但仍然没有来自程序的响应。
我需要重命名一些东西,因为我不想遇到麻烦
这是我用来提取和显示图像的代码:
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#所以我不认为它是最好的方式。如果有更好的方法,请告诉我。
答案 0 :(得分:3)
我认为你的问题在这里:
for (long i = startpos; i < startpos + length; i = i++)
应该是:
for (long i = startpos; i < startpos + length; i++)
i=i++
实际上并没有增加变量
您可以在此处找到原因:i = i++ doesn't increment i. Why?。感谢@RenniePet的链接。