ReadIco.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 the .ICO generated by the MakeIco sample,
    // and renders all frames found in it along the diagonal of the
    // resulting image.
    public class ReadIco
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            int x = 0, y = 0;

            var srcPath = Path.Combine("Resources", "ImagesBis", "GcDocs.ico");
            using (var bmp = new GcBitmap())
            using (var ico = new GcIco(srcPath))
            {
                var w = ico.Frames.Sum(f_ => f_.Width);
                var h = ico.Frames.Sum(f_ => f_.Height);
                bmp.CreateImage(w, h, false);
                bmp.Clear();
                for (int i = 0; i < ico.Frames.Count; ++i)
                {
                    using (var tbmp = ico.Frames[i].ToGcBitmap())
                    {
                        bmp.BitBlt(tbmp, x, y);
                        x += tbmp.PixelWidth;
                        y += tbmp.PixelHeight;
                    }
                }
                return bmp.Clip(new Rectangle(0, 0, x, y));
            }
        }
    }
}