我用:
propItem.Value = System.Text.Encoding.UTF8.GetBytes(textBox1.Text + "\0");
其中textBox1.Text包含“MMM”,设置值并将其保存在文件中(propItem.Value是byte []),但是当我尝试读取我使用的文件时:
string myString = System.Text.Encoding.UTF8.GetString(propItem.Value);
并获取:“M \ 0M \ 0M \ 0 \ 0 \ 0”。任何人都可以建议如何获得正确的字符串,没有'\ 0'。我已经看到了关于类似问题的所有答案,但在我的案例中没有一个答案有效。
加载文件:
Image img0 = null;
string sourceFile;
private void btnLoad_Click(object sender, EventArgs e)
{
using (var selectFileDialog = new OpenFileDialog())
{
if (selectFileDialog.ShowDialog() == DialogResult.OK)
{
sourceFile = selectFileDialog.FileName;
img0 = Image.FromFile(sourceFile);
PropertyItem[] propItems = img0.PropertyItems;
textBox1.Text = "Nothing in the file.";
foreach (PropertyItem propItem in propItems)
{
if (propItem.Id == 0x9286)
{
string myString = System.Text.Encoding.UTF8.GetString(propItem.Value);
textBox1.Text = myString ;
}
}
}
}
}
答案 0 :(得分:0)
应该是:
string myString = System.Text.Encoding.Unicode.GetString(propItem.Value);