Пример #1
0
        private void WriteFrame(Stream outputStream)
        {
            GifFrame    thisFrame = _frames[_encodingFrame];
            Image       thisImage = thisFrame.TheImage;
            ColourTable act       = SetActiveColourTable();
            int         transparentColourIndex;

            if (_transparent == Color.Empty)
            {
                transparentColourIndex = 0;
            }
            else
            {
                // TESTME: WriteFrame - _transparent != Color.Empty
                transparentColourIndex = FindClosest(_transparent, act);
            }
            WriteGraphicCtrlExt(thisFrame,
                                transparentColourIndex,
                                outputStream);
            ColourTable lct;

            if (_strategy == ColourTableStrategy.UseLocal)
            {
                lct = act;
            }
            else
            {
                lct = null;
            }
            WriteImageDescriptor(thisImage.Size,
                                 thisFrame.Position,
                                 lct,
                                 outputStream);

            // Write a local colour table if the strategy is to do so
            if (_strategy == ColourTableStrategy.UseLocal)
            {
                act.WriteToStream(outputStream);
                IndexedPixels ip;
                if (_quantizerType == QuantizerType.UseSuppliedPalette)
                {
                    ip = MakeIndexedPixels(act, thisImage);
                }
                else
                {
                    ip = _pixelAnalysis.IndexedPixels;
                }
                WritePixels(ip, outputStream);
            }
            else             // global colour table
            {
                IndexedPixels ip;
                if (_quantizerType == QuantizerType.UseSuppliedPalette)
                {
                    ip = MakeIndexedPixels(act, thisImage);
                }
                else
                {
                    ip = _pixelAnalysis.IndexedPixelsCollection[_encodingFrame];
                }
                WritePixels(ip, outputStream);
            }
        }
Пример #2
0
        public ActionResult Timer(long? timestamp)
        {
            string title = null;
            try
            {
                byte[] output = null;
                using (MemoryStream stream = new MemoryStream())
                {
                    int repeatCount = 0; //repeat forever
                    DateTime end = Epoch.AddSeconds(timestamp ?? 0);
                    title = end.ToString();
                    TimeSpan remaining = end - DateTime.UtcNow;
                    if (remaining.TotalSeconds < 0)
                    {
                        remaining = TimeSpan.FromSeconds(0);
                    }

                    if (remaining.TotalSeconds <= 60)
                    {
                        repeatCount = -1; //don't repeat
                    }

                    using (Image background = Image.FromFile(Server.MapPath(BackgroundPath)))
                    {
                        using (Font font = new Font(FontName, FontSize, FontBold ? FontStyle.Bold : FontStyle.Regular, GraphicsUnit.Pixel))
                        {
                            using (var disposer = new Disposer())
                            {
                                var target = new SynchronizeInvokeStub();
                                AnimatedGifEncoder encoder = new AnimatedGifEncoder(target);
                                encoder.RepeatCount = repeatCount;
                                encoder.OutputStream = stream;
                                encoder.QuantizerType = GifComponents.Quantizing.QuantizerType.Octree;
                                {

                                    int count = 0;
                                    while (remaining.TotalSeconds >= 0 && count < 60)
                                    {
                                        Bitmap bitmap = disposer.Track(new Bitmap(background));
                                        using (Graphics g = Graphics.FromImage(bitmap))
                                        {
                                            StringFormat format = new StringFormat();
                                            format.Alignment = StringAlignment.Center;
                                            format.LineAlignment = StringAlignment.Center;
                                            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                                            string days, hours, minutes, seconds;
                                            if (remaining.Days > 99)
                                            {
                                                days = "--";
                                                hours = "--";
                                                minutes = "--";
                                                seconds = "--";
                                                count = 99; //causes the loop to end after this one
                                            }
                                            else
                                            {
                                                days = remaining.Days.ToString("00");
                                                hours = remaining.Hours.ToString("00");
                                                minutes = remaining.Minutes.ToString("00");
                                                seconds = remaining.Seconds.ToString("00");
                                            }
                                            g.DrawString(days, font, Brushes.White, new RectangleF(ImageX, ImageY, ImageWidth / 4, ImageHeight), format);
                                            g.DrawString(hours, font, Brushes.White, new RectangleF(ImageX + (ImageWidth / 4), ImageY, ImageWidth / 4, ImageHeight), format);
                                            g.DrawString(minutes, font, Brushes.White, new RectangleF(ImageX + (2 * ImageWidth / 4), ImageY, ImageWidth / 4, ImageHeight), format);
                                            g.DrawString(seconds, font, Brushes.White, new RectangleF(ImageX + (3 * ImageWidth / 4), ImageY, ImageWidth / 4, ImageHeight), format);
                                            g.DrawString(":", font, Brushes.White, new RectangleF(ImageX + (ImageWidth / 4) - ImageWidth / 8, ImageY, ImageWidth / 4, ImageHeight), format);
                                            g.DrawString(":", font, Brushes.White, new RectangleF(ImageX + (2 * ImageWidth / 4) - ImageWidth / 8, ImageY, ImageWidth / 4, ImageHeight), format);
                                            g.DrawString(":", font, Brushes.White, new RectangleF(ImageX + (3 * ImageWidth / 4) - ImageWidth / 8, ImageY, ImageWidth / 4, ImageHeight), format);
                                        }

                                        var frame = new GifFrame(bitmap);
                                        frame.Delay = 100;
                                        encoder.AddFrame(frame);

                                        count++;
                                        remaining = remaining.Subtract(OneSecond);
                                    }
                                }
                                encoder.Start();
                                encoder.WaitUntilDone();
                            }
                        }
                    }
                    output = stream.ToArray();
                }
                return new FileContentResult(output, "image/gif");
            }
            catch
            {
                return new FilePathResult(Server.MapPath(BackgroundPath), "image/gif");
            }
            finally
            {
                Clicky.TrackRequest(Request, ActionType.PageView, "Timer: " + title);
            }
        }
Пример #3
0
 /// <summary>
 /// Adds a frame to the animation.
 /// </summary>
 /// <param name="frame">
 /// The frame to add to the animation.
 /// </param>
 public void AddFrame(GifFrame frame)
 {
     _frames.Add(frame);
 }