csharp/0xC0000054/pdn-gmic/src/OutputImageUtil.cs

OutputImageUtil.cs
/*
*  This file is part of pdn-gmic, a Paint.NET Effect that
*  that provides integration with G'MIC-Qt.
*
*  Copyright (C) 2018, 2019, 2020, 2021 Nicholas Hayes
*
*  pdn-gmic is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  pdn-gmic is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see .
*
*/

using PaintDotNet;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;

namespace GmicEffectPlugin
{
    internal static clast OutputImageUtil
    {
        /// 
        /// Saves all of the G'MIC output images to a folder.
        /// 
        /// The output images.
        /// The output folder.
        /// 
        ///  is null
        /// or
        ///  is null.
        /// 
        /// The file path is not valid.
        /// An error occurred when saving the image.
        /// An I/O error occurred.
        /// The caller does not have the required permission.
        /// The access requested is not permitted by the operating system for the specified path.
        public static void SaveAllToFolder(IReadOnlyList outputImages, string outputFolder)
        {
            if (outputImages is null)
            {
                ExceptionUtil.ThrowArgumentNullException(nameof(outputImages));
            }

            if (outputFolder is null)
            {
                ExceptionUtil.ThrowArgumentNullException(nameof(outputFolder));
            }

            DirectoryInfo directoryInfo = new DirectoryInfo(outputFolder);

            if (!directoryInfo.Exists)
            {
                directoryInfo.Create();
            }

            string currentTime = DateTime.Now.ToString("yyyyMMdd-THHmmss");

            for (int i = 0; i < outputImages.Count; i++)
            {
                string imageName = string.Format(CultureInfo.InvariantCulture, "{0}-{1}.png", currentTime, i);

                string path = Path.Combine(outputFolder, imageName);

                using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    using (System.Drawing.Bitmap image = outputImages[i].CreateAliasedBitmap())
                    {
                        image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    }
                }
            }
        }
    }
}