为什么位图中的数据超出范围
本文关键字:范围 数据 位图 为什么 | 更新日期: 2023-09-27 17:58:41
我正在为天际创建一个保存游戏管理器,但遇到了一个问题。当我单独创建 SaveGame 对象时,保存的位图部分可以正常工作。但是,当我在循环中调用该方法时,位图采用错误的值,主要是类似于另一个保存游戏的值。
TL;DR - 为什么我的窗体的列表框显示字符保存的正确信息,但嵌入的图片除外?它似乎不是选择正确的图片,而是选择上次处理的图片。该过程与通过打开文件对话框选择的过程有何不同?
编辑:更新 - 我查看了每个SaveGame对象存储的位图,发现在scanDirectoryForSaves中创建SaveGames期间不知何故弄乱了它。位图和使用我不知道的字节指针是否存在对象范围问题?
以下是保存游戏对象的静态工厂的代码:
public string Name { get; private set; }
public int SaveNumber { get; private set; }
public int PictureWidth { get; private set; }
public int PictureHeight { get; private set; }
public Bitmap Picture { get; private set; }
public DateTime SaveDate { get; private set; }
public string FileName { get; private set; }
public static SaveGame ReadSaveGame(string Filename)
{
SaveGame save = new SaveGame();
save.FileName = Filename;
byte[] file = File.ReadAllBytes(Filename);
int headerWidth = BitConverter.ToInt32(file, 13);
save.SaveNumber = BitConverter.ToInt32(file, 21);
short nameWidth = BitConverter.ToInt16(file, 25);
save.Name = System.Text.Encoding.UTF8.GetString(file, 27, nameWidth);
save.PictureWidth = BitConverter.ToInt32(file, 13 + headerWidth - 4);
save.PictureHeight = BitConverter.ToInt32(file, 13 + headerWidth);
save.readPictureData(file, 13 + headerWidth + 4, save.PictureWidth, save.PictureHeight);
save.SaveDate = DateTime.FromFileTime((long)BitConverter.ToUInt64(file, 13 + headerWidth - 12));
return save;
}
private void readPictureData(byte[] file, int startIndex, int width, int height)
{
IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement(file, startIndex);
Picture = new Bitmap(width, height, 3 * width, System.Drawing.Imaging.PixelFormat.Format24bppRgb, pointer);
}
在我的窗体上,我使用一种方法来读取某个目录中的所有保存文件,从中创建 SaveGame 对象,并根据字符名称将它们存储在字典中。
private Dictionary<string, List<SaveGame>> scanDirectoryForSaves(string directory)
{
Dictionary<string, List<SaveGame>> saves = new Dictionary<string, List<SaveGame>>();
DirectoryInfo info = new DirectoryInfo(directory);
foreach (FileInfo file in info.GetFiles())
{
if (file.Name.ToLower().EndsWith(".ess") || file.Name.ToLower().EndsWith(".bak"))
{
string filepath = String.Format(@"{0}'{1}", directory, file.Name);
SaveGame save = SaveGame.ReadSaveGame(filepath);
if (!saves.ContainsKey(save.Name))
{
saves.Add(save.Name, new List<SaveGame>());
}
saves[save.Name].Add(save);
}
}
foreach (List<SaveGame> saveList in saves.Values)
{
saveList.Sort();
}
return saves;
}
我将键添加到列表框中。在列表框中选择名称时,窗体上将显示该字符的最新保存。每个字符的名称、日期和其他字段都是正确的,但位图是保存游戏图片的特定字符的变体。
我正在调用相同的方法来更新从打开的文件对话框中选择保存以及列表框中的表单字段。
private void updateLabels(SaveGame save)
{
nameLabel.Text = "Name: " + save.Name;
filenameLabel.Text = "File: " + save.FileName;
saveNumberLabel.Text = "Save Number: " + save.SaveNumber;
saveDateLabel.Text = "Save Date: " + save.SaveDate;
saveGamePictureBox.Image = save.Picture;
saveGamePictureBox.Image = ScaleImage(
saveGamePictureBox.Image, saveGamePictureBox.Width, saveGamePictureBox.Height);
saveGamePictureBox.Invalidate();
}
当你使用采用IntPtr
的构造函数创建Bitmap
时,IntPtr
必须指向一个内存块,该内存块在Bitmap
对象的生存期内仍然有效。您负责确保内存块不会被移动或解除分配。
但是,您的代码正在传递指向 file
的IntPtr
,这是一个托管字节数组。由于ReadSaveGame
返回后没有任何引用file
,因此垃圾回收器可以自由回收内存并将其重用于下一个文件。结果:位图损坏。
尽管您可以通过使用 GCHandle 将数组固定在内存中来解决此问题,但让Bitmap
管理自己的内存可能更容易、更安全。首先创建一个空Bitmap
,然后设置其位:
private void readPictureData(byte[] file, int startIndex, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData data = bitmap.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
Marshal.Copy(file, startIndex, data.Scan0, width * height * 3);
bitmap.UnlockBits(data);
Picture = bitmap;
}