MakeIco.cs
//
// This code is part of GrapeCity Documents for Imaging samples.
// Copyright (c) GrapeCity, 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 GcImagingWeb.Samples
{
    // 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.
    // [SampleImageFormats(ImageFormat.TIFF)]
    public class MakeIco
    {
        public Stream GenerateImageStream(ImageEncoding targetEncoding, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            if (targetEncoding != ImageEncoding.Ico)
                throw new Exception("This sample only supports ICO output format.");

            var srcPath = Path.Combine("Resources", "ImagesBis", "gcd-hex-logo.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;
            }
        }

        public ImageEncoding DefaultEncoding { get => ImageEncoding.Ico; }
    }
}