// 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 takes a color JPEG, and creates bilevel bitmaps from it
    // using all available dithering methods. The resulting b/w images
    // are added as frames to a multi-frame TIFF.
    // The original color image is added as the last page.
    public class DitheringInTiff
        public Stream GenerateImageStream(ImageEncoding targetEncoding, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
            if (targetEncoding != ImageEncoding.Tiff)
                throw new Exception("This sample only supports TIFF output format.");

            var path = Path.Combine("Resources", "Images", "minerva.jpg");
            var font = Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
            var ms = new MemoryStream();
            using (var tw = new GcTiffWriter(ms))
            using (var bmp = new GcBitmap(path))
                // Create text layout for labels:
                var tl = new TextLayout(bmp.DpiX);
                tl.DefaultFormat.Font = font;
                tl.DefaultFormat.FontSize = 16;
                tl.DefaultFormat.BackColor = Color.White;
                // Loop through all dithering methods (starting with no dithering):
                var methods = typeof(DitheringMethod).GetEnumValues();
                foreach (DitheringMethod method in methods)
                    using (var tbmp = bmp.Clone())
                        // Apply dithering:
                        // Draw label:
                        using (var g = tbmp.CreateGraphics())
                            g.DrawTextLayout(tl, new PointF(0, tbmp.Height - tl.ContentHeight));
                        // Convert to bilevel bitmap:
                        using (var f = tbmp.ToBilevelBitmap())
                // Add original image:
            ms.Seek(0, SeekOrigin.Begin);
            return ms;

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