簡體   English   中英

如何將圖像列表保存到文件夾?

[英]How can i save a list of images to a folder?

我正在嘗試將圖像列表保存到用戶確定的文件夾中,在這種情況下,我具有此列表。

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>();

到用戶確定的文件夾?

您可以使用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混合使用,

如果您有名稱,則可以將圖像轉換為字節數組,然后使用File.WriteAllBytes 確保您傳遞給WriteAllBytes的文件名具有.png擴展名。 可能有一種更簡單的方法,但是我並沒有像原始數據那樣真正地處理像這樣的媒體,所以這就是我想到的。

您可以通過以下方式保存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等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM