MakeGif.cs
//
// このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
// © 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
{
// このサンプルは、金魚の画像(BmpTransforms で使用されているものと同一)を読み込み、
// さまざまな変換を適用して複数のフレームを作成し、
// それらを組み合わせてアニメーション GIF を生成します。
public class MakeGif
{
public string DefaultMime { get => Common.Util.MimeTypes.GIF; }
public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
{
if (targetMime != Common.Util.MimeTypes.GIF)
throw new Exception("This sample only supports GIF output format.");
// GIFのサイズを適切な範囲に抑えます。
var side2 = Math.Min(400, Math.Min(pixelSize.Width, pixelSize.Height));
// 単一の画像を反転・回転させて、ターゲットGIF用のフレームを用意します。
var move1 = new GcBitmap[2];
var move2 = new GcBitmap[3];
using (var bmpSrc = new GcBitmap(Path.Combine("Resources", "Stock", "goldfish.jpg")))
{
bmpSrc.Opaque = opaque;
// 魚の頭が動かないように調整しながら、通常の画像と反転させた画像を作成します。
using (var tbmp = bmpSrc.Resize(side2, side2))
{
move1[0] = new GcBitmap(tbmp.PixelWidth, tbmp.PixelHeight, tbmp.Opaque, tbmp.DpiX, tbmp.DpiY);
move1[0].Clear(Color.White);
move1[0].BitBlt(tbmp, -(int)(side2 / 14f), 0);
}
using (var tbmp = move1[0].FlipRotate(FlipRotateAction.FlipHorizontal))
{
move1[1] = new GcBitmap(tbmp.PixelWidth, tbmp.PixelHeight, tbmp.Opaque, tbmp.DpiX, tbmp.DpiY);
move1[1].Clear(Color.White);
move1[1].BitBlt(tbmp, (int)(side2 / 14f), 0);
move1[1].BitBlt(tbmp, 0, 0);
}
move2[0] = move1[0].FlipRotate(FlipRotateAction.Rotate90);
move2[1] = move1[0].FlipRotate(FlipRotateAction.Rotate180);
move2[2] = move1[0].FlipRotate(FlipRotateAction.Rotate270);
}
// 動きを組み合わせます。
var bmps = new List<GcBitmap>();
for (int i = 0; i < 4; ++i)
{
bmps.Add(move1[0]);
bmps.Add(move1[1]);
}
bmps.Add(move1[0]);
for (int i = 0; i < 2; ++i)
{
bmps.Add(move1[0]);
bmps.Add(move2[0]);
bmps.Add(move2[1]);
bmps.Add(move2[2]);
}
bmps.Add(move1[0]);
// GIFを作成します。
var ms = new MemoryStream();
using (var gw = new GcGifWriter(ms))
{
gw.LogicalScreenWidth = bmps[0].PixelWidth;
gw.LogicalScreenHeight = bmps[0].PixelHeight;
gw.PixelAspectRatio = 1;
gw.AllowAddingTransparentColor = false;
foreach (var bmp in bmps)
gw.AppendFrame(bmp, 255, 0, 0, GifDisposalMethod.DoNotDispose, 16);
}
// GIFフレームの作成に使用したビットマップを破棄します。
foreach (var bmp in bmps.Distinct())
bmp.Dispose();
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
}
}