如何将图像列表保存到文件夹?

时间:2013-11-27 22:32:06

标签: c# wpf image list

我正在尝试将我的图片列表保存到用户确定的文件夹中,在这种情况下我有这个列表。

List<System.Drawing.Image> ListOfSystemDrawingImage = new List<System.Drawing.Image>();

        ListOfSystemDrawingImage.Add(MatrizWPF.Properties.Resources.Earth);
        ListOfSystemDrawingImage.Add(MatrizWPF.Properties.Resources.Grass);
        ListOfSystemDrawingImage.Add(MatrizWPF.Properties.Resources.Rabbit);
        ListOfSystemDrawingImage.Add(MatrizWPF.Properties.Resources.Wolf);

地球,草地,兔子和狼是PNG图像的方式相同。

我的问题是,如何存储我的

List<System.Drawing.Image> listOfSystemDrawingImage = new List<System.Drawing.Image>();

到用户确定的文件夹?

3 个答案:

答案 0 :(得分:1)

您可以使用System.Windows.Forms.FolderBrowserDialog为用户选择目标文件夹,并使用Image.Save以您的照片格式保存图片

示例:

List<System.Drawing.Image> listOfSystemDrawingImage = new List<System.Drawing.Image>();

System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    int index = 0;
    foreach (var image in listOfSystemDrawingImage)
    {
        image.Save(string.Format("{0}\\Image{1}.png", dialog.SelectedPath, index), System.Drawing.Imaging.ImageFormat.Png);
        index++;
    }
}

但是,我不建议将Window.Forms和System.Drawing与WPF混合使用,

答案 1 :(得分:0)

如果您有名称,只需将图像转换为字节数组,然后使用File.WriteAllBytes。确保您传递给WriteAllBytes的文件名的扩展名为.png。可能有一种更简单的方法,但我并不像原始数据那样处理这类媒体,所以这就是我想到的。

答案 2 :(得分:0)

您可以通过以下方式保存System.Drawing.Image列表:

string folder = @"C:\temp";
int count = 1;
foreach(Image image in listOfSystemDrawingImage)
{
    string path = Path.Combine(folder, String.Format("image{0}.png", count));
    image.Save(path);
    count++;
}

我不知道你在哪里存储图片名称,所以我只称它们为image1.png,image2.png等。

相关问题