Пример #1
0
        public async Task SaveAnimeGifAsync(AnimeGifSettings settings, string path) {
            var animeGif = new GifAnimationGenerator(settings.ScreenSize);
            BitmapBufferStack buffer = new BitmapBufferStack(settings.ScreenSize);
            var count = ItemCount;
            var index = 0;
            foreach (var frame in Items) {
                frame.Render(settings, buffer);
                animeGif.AddImage(buffer.GetPrimaryBuffer(), frame.Delay);
                await Task.Delay(1);
                ++index;
                Progress = (float)index  / (float)count;
                ProgressChanged(this, EventArgs.Empty);
            }
            animeGif.SaveTo(path);

            var fi = new FileInfo(path);
            if (fi.Length >= RecommendedFileSize) {
                var recommendedSizeMB = Math.Round((float)RecommendedFileSize / 1024f / 1024f, 2);
                var actualSizeMB = Math.Round((float)fi.Length / 1024f / 1024f, 2);
                MessageBox.Show(
                    "ファイルサイズが " + recommendedSizeMB + " MB を超えています ( " + actualSizeMB + " MB )",
                    Application.ProductName,  MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Пример #2
0
        /// <summary>バッファを指定してスクリーンをレンダリングする</summary>
        public void Render(AnimeGifSettings settings, BitmapBufferStack buffer) {
            Rectangle viewRect = new Rectangle(Point.Empty, buffer.PrimaryBufferSize);

            // 画像
            using (var g = buffer.CreateGraphics()) {
                g.Clear(Color.FromArgb(settings.BackColorARGB));
                var srcRect = new Rectangle(Point.Empty, Size);
                var destRect = Miscs.GetInternalContactRect(viewRect, srcRect.Size);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                g.DrawImage(FoldedImage.Data, destRect, srcRect, GraphicsUnit.Pixel);
            }

            // プログレスバー
            var captionBottomMargin = 0; 
            if (settings.ShowProgressBar && IndexProbider != null) {
                buffer.Push();

                Color innerColor = Color.FromArgb(settings.CaptionTextStyle.TextColorARGB);
                Color outerColor = Miscs.GetShadowColorOf(innerColor);

                var barMargin = viewRect.Width / 200;
                var barHeight = viewRect.Width / 75;

                var progressBarRect = viewRect;
                progressBarRect.X += barMargin;
                progressBarRect.Y = progressBarRect.Bottom - barHeight - barMargin;
                progressBarRect.Width -= barMargin * 2;
                progressBarRect.Height = barHeight;
                captionBottomMargin = viewRect.Bottom - progressBarRect.Y;

                using (var g = buffer.CreateGraphics())
                using (var innerBrush = new SolidBrush(innerColor))
                using (var outerBrush = new SolidBrush(outerColor)) {
                    g.FillRectangle(outerBrush, progressBarRect);
                    progressBarRect.Inflate(-1, -1);
                    if (this.IndexProbider.ItemCount >= 2) {
                        progressBarRect.Width = progressBarRect.Width * Index / (IndexProbider.ItemCount - 1);
                    }
                    g.FillRectangle(innerBrush, progressBarRect);
                }
                buffer.Pop(alpha: 0.5f);
            }

            // ウォーターマーク
            var watermarkText = settings.WaterMarkText;
            if (!string.IsNullOrWhiteSpace(watermarkText)) {
                buffer.Push();

                Color innerColor = Color.FromArgb(settings.WaterMarkTextStyle.TextColorARGB);
                Color outerColor = Miscs.GetShadowColorOf(innerColor);

                using (var g = buffer.CreateGraphics())
                using (var innerBrush = new SolidBrush(innerColor))
                using (var outerBrush = new SolidBrush(outerColor)) {
                    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    var format = new StringFormat();
                    format.Alignment = StringAlignment.Far;
                    format.LineAlignment = StringAlignment.Near;
                    drawText(g, watermarkText, settings.WaterMarkTextStyle.GetFont(),
                        outerBrush, innerBrush, viewRect, format);
                }
                buffer.Pop(alpha: 0.5f);
            }

            // キャプション
            var captionText = CaptionText;
            if (!string.IsNullOrWhiteSpace(captionText)) {
                buffer.Push();

                Color innerColor = Color.FromArgb(settings.CaptionTextStyle.TextColorARGB);
                Color outerColor = Miscs.GetShadowColorOf(innerColor);
                var font = settings.CaptionTextStyle.GetFont();

                using (var g = buffer.CreateGraphics())
                using (var innerBrush = new SolidBrush(innerColor))
                using (var outerBrush = new SolidBrush(outerColor)) {
                    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    var format = new StringFormat();
                    format.Alignment = StringAlignment.Near;
                    format.LineAlignment = StringAlignment.Far;

                    Rectangle captionRect = viewRect;
                    
                    captionRect.Height -= captionBottomMargin;

                    float descentByEm = font.FontFamily.GetCellDescent(font.Style); // ベースラインから下の高さ(em単位)
                    float pointsPerEm = font.SizeInPoints / font.FontFamily.GetEmHeight(font.Style);
                    float pixelsPerPoint = g.DpiY / 72f; // 1 inch = 72 pt.
                    captionRect.Y += (int)(descentByEm * pointsPerEm * pixelsPerPoint); 
                    drawText(g, captionText, font, outerBrush, innerBrush, captionRect, format);
                }
                buffer.Pop();
            }

        }