C#-如何保存字典中的图像<;字符串、图像>;到文件夹

C#-如何保存字典中的图像<;字符串、图像>;到文件夹,c#,dictionary,game-engine,C#,Dictionary,Game Engine,我目前正在开发一个2D游戏引擎,到目前为止,我已经添加了一个加载和删除图像的功能,但现在我想保存它们,这是字典: 公共字典图像=新字典() 字符串是名称,例如,当用户想要添加图像时,他们单击显示为“选择图像”的按钮,然后将一个pitcurebox设置为打开的图像,然后有一个文本框,当他们单击显示为“添加图像”的按钮时,将执行此操作。添加(textBox1.Text,pictureBox1.image)然后我想保存所有添加到文件夹中的图像 我在互联网上到处寻找这个问题,但没有人给我答案,我真的被卡

我目前正在开发一个2D游戏引擎,到目前为止,我已经添加了一个加载和删除图像的功能,但现在我想保存它们,这是字典:

公共字典图像=新字典()

字符串是名称,例如,当用户想要添加图像时,他们单击显示为“选择图像”的按钮,然后将一个pitcurebox设置为打开的图像,然后有一个文本框,当他们单击显示为“添加图像”的按钮时,将执行此操作。添加(textBox1.Text,pictureBox1.image)然后我想保存所有添加到文件夹中的图像


我在互联网上到处寻找这个问题,但没有人给我答案,我真的被卡住了,提前谢谢。

使用BinaryFormatter将图像保存到单个文件夹,以将词典序列化/反序列化为如下文件:

public void Save(string filename)
{
    var bin_formater = new BinaryFormatter();
    using (var stream = File.Create(filename))
    {
        bin_formater.Serialize(stream, images); //images is your dictionary
    }
}
foreach (var imgX in images.Select(kvp => kvp.Value))
{
    imgX.Save("figure_a_file_path_and_name", ImageFormat.Jpeg);
}

使用BinaryFormatter将图像保存到单个文件夹,以便将词典序列化/反序列化为以下文件:

public void Save(string filename)
{
    var bin_formater = new BinaryFormatter();
    using (var stream = File.Create(filename))
    {
        bin_formater.Serialize(stream, images); //images is your dictionary
    }
}
foreach (var imgX in images.Select(kvp => kvp.Value))
{
    imgX.Save("figure_a_file_path_and_name", ImageFormat.Jpeg);
}

Image
类具有
Save
方法。所以你可以这样做:

public void Save(string filename)
{
    var bin_formater = new BinaryFormatter();
    using (var stream = File.Create(filename))
    {
        bin_formater.Serialize(stream, images); //images is your dictionary
    }
}
foreach (var imgX in images.Select(kvp => kvp.Value))
{
    imgX.Save("figure_a_file_path_and_name", ImageFormat.Jpeg);
}
更新 如果要将字典中的字符串用作文件名,请稍微更改上述代码:

var folder = "figure_the_folder_path\\";

foreach (var entry in images)
{
    var destinationFile = string.Concat(folder, entry.Key); 
    var img = entry.Value;
    img.Save(destinationFile, ImageFormat.Jpeg);
}

Image
类具有
Save
方法。所以你可以这样做:

public void Save(string filename)
{
    var bin_formater = new BinaryFormatter();
    using (var stream = File.Create(filename))
    {
        bin_formater.Serialize(stream, images); //images is your dictionary
    }
}
foreach (var imgX in images.Select(kvp => kvp.Value))
{
    imgX.Save("figure_a_file_path_and_name", ImageFormat.Jpeg);
}
更新 如果要将字典中的字符串用作文件名,请稍微更改上述代码:

var folder = "figure_the_folder_path\\";

foreach (var entry in images)
{
    var destinationFile = string.Concat(folder, entry.Key); 
    var img = entry.Value;
    img.Save(destinationFile, ImageFormat.Jpeg);
}

您希望以什么格式保存它?将所有图像保存为文件夹中的文件?仅保存图像?如Jpeg、png等。?或者整个信息字符串+图像以及如何再次使用保存的图像/数据?请详细说明以获得具体答案。字符串是名称,例如,当用户想要添加图像时,他们单击显示为“选择图像”的按钮,然后将一个pitcurebox设置为已打开的图像,然后有一个文本框,当他们单击显示为“添加图像”的按钮时,将执行此操作
images.add(textBox1.Text,pictureBox1.Image)
然后我想保存添加到文件夹中的所有图像。您想保存的格式是什么?将所有图像保存为文件夹中的文件?仅图像?作为Jpeg、png等?或整个信息字符串+图像以及如何再次使用保存的图像/数据?请详细说明以获得具体答案。字符串是名称,例如,当son想添加一个图像他们单击一个显示“选择图像”的按钮,然后一个pitcurebox将被设置为打开的图像,然后有一个文本框,当他们单击一个显示“添加图像”的按钮时,它将执行此操作。
图像。添加(textBox1.Text,pictureBox1.image)
然后我想保存所有添加到文件夹中的图像