UpdateGif.cs
//
// このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Drawing;
namespace DsImagingWeb.Demos
{
// このサンプルは、既存の GIF を読み込み、そのフレームを変更します。
// 具体的には、カラーマトリクスを適用して色を変更し、
// 画像を水平方向に反転します。
// サンプルで読み込まれるGIFは、IndexedGif によって生成されたものです。
public class UpdateGif
{
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.");
// 色を緑がかった青に変更するための行列です。
var colorMatrix = new ColorMatrix5x4()
{
M11 = 0.2f,
M12 = 0.3f,
M22 = 0.5f
};
var ms = new MemoryStream();
// 元のGIFを読み込み、加工したものを書き込みます(パレットの変更、およびフレームの反転と回転を行います)。
using (var gr = new GcGifReader(Path.Combine("Resources", "Gifs", "goldfish-indexed.gif")))
using (var gw = new GcGifWriter(ms))
{
var pal = gr.GetGlobalPalette();
// このサンプルは、グローバルパレットを持つGIFでのみ動作します。
if (pal == null)
throw new Exception("Source GIF does not have a global palette.");
// カラー行列を使用してパレットを更新します。
using (var palBmp = new GcBitmap(pal, pal.Length, 1, true))
palBmp.ApplyColorMatrix(colorMatrix);
// ターゲットのパレットとその他のプロパティを設定します。
gw.GlobalPalette = pal;
gw.LogicalScreenWidth = gr.LogicalScreenWidth;
gw.LogicalScreenHeight = gr.LogicalScreenHeight;
gw.PixelAspectRatio = gr.PixelAspectRatio;
gw.AllowAddingTransparentColor = false;
using (var tbmp = new GcBitmap())
for (int i = 0; i < gr.Frames.Count; i++)
{
var frame = gr.Frames[i];
frame.ToGcBitmap(tbmp, i - 1);
// 画像を左右に反転させます(これにより、魚の「回転」が逆になります)。
using (var bmp = tbmp.FlipRotate(FlipRotateAction.FlipHorizontal))
{
// カラー行列を適用し、フレームをターゲットのGIFに追加します。
bmp.ApplyColorMatrix(colorMatrix);
gw.AppendFrame(bmp, pal, DitheringMethod.NoDithering, 0, 0, GifDisposalMethod.DoNotDispose, frame.DelayTime, false);
}
}
}
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
}
}