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); } }
public static void ExtractAnimation(WzSubProperty parent, string savePath, bool apng, bool apngFirstFrame) { List<Bitmap> bmpList = new List<Bitmap>(parent.WzProperties.Count); List<int> delayList = new List<int>(parent.WzProperties.Count); Point biggestPng = new Point(0, 0); Point SmallestEmptySpace = new Point(65535, 65535); Point MaximumPngMappingEndingPts = new Point(0, 0); foreach (IWzImageProperty subprop in parent.WzProperties) { if (subprop is WzCanvasProperty) { //WzVectorProperty origin = (WzVectorProperty)subprop["origin"]; WzPngProperty png = ((WzCanvasProperty)subprop).PngProperty; if (png.Height > biggestPng.Y) biggestPng.Y = png.Height; if (png.Width > biggestPng.X) biggestPng.X = png.Width; } } List<WzCanvasProperty> sortedProps = new List<WzCanvasProperty>(); foreach (IWzImageProperty subprop in parent.WzProperties) { if (subprop is WzCanvasProperty) { sortedProps.Add((WzCanvasProperty)subprop); WzPngProperty png = ((WzCanvasProperty)subprop).PngProperty; WzVectorProperty origin = (WzVectorProperty)subprop["origin"]; Point StartPoints = new Point(biggestPng.X - origin.X.Value, biggestPng.Y - origin.Y.Value); Point PngMapppingEndingPts = new Point(StartPoints.X + png.Width, StartPoints.Y + png.Height); if (StartPoints.X < SmallestEmptySpace.X) SmallestEmptySpace.X = StartPoints.X; if (StartPoints.Y < SmallestEmptySpace.Y) SmallestEmptySpace.Y = StartPoints.Y; if (PngMapppingEndingPts.X > MaximumPngMappingEndingPts.X) MaximumPngMappingEndingPts.X = PngMapppingEndingPts.X; if (PngMapppingEndingPts.Y > MaximumPngMappingEndingPts.Y) MaximumPngMappingEndingPts.Y = PngMapppingEndingPts.Y; } } sortedProps.Sort(new Comparison<WzCanvasProperty>(PropertySorter)); /* foreach (IWzImageProperty subprop in parent.WzProperties) { if (subprop is WzCanvasProperty) { WzCompressedIntProperty delayProp = (WzCompressedIntProperty)subprop["delay"]; if (delayProp != null) delay = delayProp.Value; } }*/ /* Brush bgcolor = null; switch (toolStripComboBox2.SelectedIndex) { case 0: bgcolor = Brushes.Widthhite; break; case 1: bgcolor = Brushes.Black; break; default: bgcolor = Brushes.Black; break; }*/ for (int i = 0; i<sortedProps.Count; i++) { WzCanvasProperty subprop = sortedProps[i]; if (i.ToString() != subprop.Name) { Warning.Error("Something f****d up at animation builder, frame " + i.ToString()); return; } Bitmap bmp = subprop.PngProperty.GetPNG(false); WzVectorProperty origin = (WzVectorProperty)subprop["origin"]; // if (apng) bmpList.Add(OptimizeBitmapTransparent(bmp, origin, biggestPng, SmallestEmptySpace, MaximumPngMappingEndingPts)); /* else bmpList.Add(OptimizeBitmap(bmp, origin, biggestPng, SmallestEmptySpace, MaximumPngMappingEndingPts, bgcolor));*/ WzCompressedIntProperty delayProp = (WzCompressedIntProperty)subprop["delay"]; int delay =100; if (delayProp != null) delay = delayProp.Value; delayList.Add(delay); //} } if (apng) { //List<Frame> frameList = new List<Frame>(); /* List<int> delayList = new List<int>(); foreach (TreeNode subnode in parent.Nodes) { if (subnode.Tag2 is PNG) { TreeNode delayNode = FindNodeInSubnodes(subnode, "delay"); if (delayNode == null) delayList.Add(0); else delayList.Add((int)delayNode.Tag2); } } if (delayList.Count != bmp.Count) { MessageBox.Show("Weird error, seems like there are more PNGs than delay values"); return; }*/ Apng apngBuilder = new Apng(); if (apngFirstFrame) apngBuilder.AddFrame(new Frame(CreateIncompatibilityFrame(new Size(bmpList[0].Width, bmpList[0].Height)),1,1)); for (int i = 0; i < bmpList.Count; i++) apngBuilder.AddFrame(new Frame(bmpList[i], getNumByDelay(delayList[i]), getDenByDelay(delayList[i]))); apngBuilder.WriteApng(savePath, apngFirstFrame, true); //createapng(frameList, savePath); } else { AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder(); for (int i = 0; i < bmpList.Count; i++) gifEncoder.AddFrame(new GifFrame(bmpList[i]) { Delay = delayList[i] / 10 }); gifEncoder.WriteToFile(savePath); } }