MakeIco.cs
//
// This code is part of Document Solutions for Imaging demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;

namespace DsImagingWeb.Demos
{
    // This sample loads a large PNG image, resizes it to several smaller sizes
    // commonly used in icons, and creates a multi-frame .ICO from those.
    // See also the ReadIco sample which renders the frames from the .ICO
    // created by this sample.
    public class MakeIco
    {
        public string DefaultMime { get => Common.Util.MimeTypes.ICO; }

        public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            if (targetMime != Common.Util.MimeTypes.ICO)
                throw new Exception("This sample only supports ICO output format.");

            var srcPath = Path.Combine("Resources", "ImagesBis", "mescius-logomark-c.png");
            var srcBmp = new GcBitmap();

            using (var s = File.OpenRead(srcPath))
                srcBmp.Load(s);

            using (var bmp16 = srcBmp.Resize(16, 16))
            using (var bmp24 = srcBmp.Resize(24, 24))
            using (var bmp32 = srcBmp.Resize(32, 32))
            using (var bmp48 = srcBmp.Resize(48, 48))
            using (var bmp256 = srcBmp.Resize(256, 256))
            using (var ico = new GcIco())
            {
                ico.Frames.Add(new IcoFrame(bmp256, IcoFrameEncoding.Png));
                ico.Frames.Add(new IcoFrame(bmp48, IcoFrameEncoding.Png));
                ico.Frames.Add(new IcoFrame(bmp32, IcoFrameEncoding.Png));
                ico.Frames.Add(new IcoFrame(bmp24, IcoFrameEncoding.Png));
                ico.Frames.Add(new IcoFrame(bmp16, IcoFrameEncoding.Png));

                var ms = new MemoryStream();
                ico.Save(ms);
                ms.Seek(0, SeekOrigin.Begin);
                return ms;
            }
        }
    }
}