public byte[] GetWaitShot() { GifDecoder gifDecoder = new GifDecoder(); AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder(); MemoryStream outStream = new MemoryStream(); gifEncoder.SetFrameRate(10); gifEncoder.SetDelay(100); gifEncoder.SetRepeat(0); gifEncoder.SetTransparent(Color.Black); gifEncoder.Start(outStream); ResourceManager rm = new ResourceManager("webshot.serv.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly()); gifDecoder.Read(rm.GetStream("ajax_loader")); for (int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++) { gifEncoder.AddFrame(gifDecoder.GetFrame(i), thumbwidth, thumbheight); } gifEncoder.Finish(); byte[] buffer = outStream.ToArray(); outStream.Close(); return(buffer); }
public void CreateAnimatedGIFTest() { MemoryStream ms = new MemoryStream(); AnimatedGifEncoder encoder = new AnimatedGifEncoder(); // encoder.SetDelay(200); encoder.SetFrameRate(5); encoder.Start(ms); for (char i = 'a'; i <= 'z'; i++) { Console.Write(i.ToString()); encoder.AddFrame(ThumbnailBitmap.GetBitmapFromText(i.ToString(), 48, 100, 200)); } encoder.Finish(); Console.WriteLine(); }
public GifRecorder(FrameworkElement container, FrameworkElement root, string fileName, int fps) { _container = container; _root = root; _frameDelay = TimeSpan.FromMilliseconds(1000.0 / fps); _storyboardControllers = new List <StoryboardController>(); _renderTargetBitmap = new RenderTargetBitmap((int)_root.ActualWidth, (int)_root.ActualHeight, 96, 96, PixelFormats.Pbgra32); _writeableBitmap = new WriteableBitmap((int)_root.ActualWidth, (int)_root.ActualHeight, 96, 96, PixelFormats.Pbgra32, null); _encoder = new AnimatedGifEncoder(); _encoder.Start(fileName); _encoder.SetRepeat(0); _encoder.SetFrameRate(fps); CompositionTarget.Rendering += CompositionTarget_Rendering; }
public void ResizeAnimatedGIFTest() { string filename = Path.GetTempFileName(); Console.WriteLine("Creating: {0}", filename); string resizedfilename = Path.GetTempFileName(); Console.WriteLine("Resizing: {0}", resizedfilename); try { using (FileStream ms = File.OpenWrite(filename)) { AnimatedGifEncoder encoder = new AnimatedGifEncoder(); encoder.SetFrameRate(5); encoder.Start(ms); for (char i = 'a'; i <= 'e'; i++) { Console.Write(i.ToString()); encoder.AddFrame(ThumbnailBitmap.GetBitmapFromText( i.ToString(), 48, 300, 200)); } Console.WriteLine(); encoder.Finish(); ms.Flush(); } using (FileStream ms = File.OpenRead(filename)) { using (FileStream resizedms = File.OpenWrite(resizedfilename)) { AnimatedGifEncoder.Resize(ms, resizedms, 200, 150, 100); resizedms.Flush(); } } } finally { if (File.Exists(filename)) { File.Delete(filename); } if (File.Exists(resizedfilename)) { File.Delete(resizedfilename); } } }
/// <summary> /// Generate a typly gif /// </summary> public void MakeGif() { string gifName = _path + "\\typly.gif"; AnimatedGifEncoder e = new AnimatedGifEncoder(); e.Start(gifName); e.SetFrameRate(12); e.SetRepeat(0); //-1:no repeat,0:always repeat var images = GetSentences(); Sentences currentSentence = null; foreach (var item in images) { if (currentSentence != null && item.SentenceNumber != currentSentence.SentenceNumber) { foreach (var reversedImage in currentSentence.SentenceFile.Reverse()) { e.AddFrame(new Bitmap(reversedImage.FullName)); } } foreach (var image in item.SentenceFile) { e.AddFrame(new Bitmap(image.FullName)); } SetCursorBlink(e, item, 2); currentSentence = item; } //add last frame of cursor blinking for 2 seconds var twoCursors = images.Last(); SetCursorBlink(e, twoCursors, 6); e.Finish(); }
public void ConsumeAnimatedGIFTest() { MemoryStream ms = new MemoryStream(); AnimatedGifEncoder encoder = new AnimatedGifEncoder(); // encoder.SetDelay(200); encoder.SetFrameRate(5); encoder.Start(ms); for (char i = 'a'; i <= 'z'; i++) { Console.Write(i.ToString()); encoder.AddFrame(ThumbnailBitmap.GetBitmapFromText(i.ToString(), 48, 100, 200)); } Console.WriteLine(); encoder.Finish(); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); GifDecoder decoder = new GifDecoder(); decoder.Read(ms); Console.WriteLine("Frames: {0}", decoder.GetFrameCount()); Assert.AreEqual(26, decoder.GetFrameCount()); }
public void ResizeToThumbnailTest() { string filename = Path.GetTempFileName(); Console.WriteLine("Creating: {0}", filename); try { MemoryStream ms = new MemoryStream(); AnimatedGifEncoder encoder = new AnimatedGifEncoder(); encoder.SetFrameRate(5); encoder.Start(ms); for (char i = 'a'; i <= 'e'; i++) { Console.Write(i.ToString()); encoder.AddFrame(ThumbnailBitmap.GetBitmapFromText( i.ToString(), 48, 300, 200)); } Console.WriteLine(); encoder.Finish(); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); ThumbnailBitmap bitmap = new ThumbnailBitmap(ms); FileStream fs = File.Create(filename); byte[] th = bitmap.Thumbnail; fs.Write(th, 0, th.Length); fs.Close(); } finally { if (File.Exists(filename)) { File.Delete(filename); } } }
/// <summary> /// Thread method that encodes the list of frames. /// </summary> private void DoWork() { int countList = _listBitmap.Count; var processing = new Processing(); this.Invoke((Action) delegate //Needed because it's a cross thread call. { //Control ctrlParent = panelTransparent; //Processing processing = new Processing(); panelTransparent.Controls.Add(processing); processing.Dock = DockStyle.Fill; processing.SetMaximumValue(countList); processing.SetStatus(1); }); if (Settings.Default.STencodingCustom) // if NGif encoding { #region Ngif encoding int numImage = 0; using (_encoder = new AnimatedGifEncoder()) { _encoder.Start(_outputpath); _encoder.SetQuality(Settings.Default.STquality); _encoder.SetRepeat(Settings.Default.STloop ? (Settings.Default.STrepeatForever ? 0 : Settings.Default.STrepeatCount) : -1); // 0 = Always, -1 once try { foreach (var image in _listBitmap) { numImage++; this.BeginInvoke((Action)(() => processing.SetStatus(numImage))); _encoder.SetFrameRate(Convert.ToInt32(numMaxFps.Value)); _encoder.AddFrame(image); } } catch (Exception ex) { LogWriter.Log(ex, "Ngif encoding."); } } #endregion } else //if paint.NET encoding { #region paint.NET encoding //var imageArray = _listBitmap.ToArray(); var delay = 1000 / Convert.ToInt32(numMaxFps.Value); var repeat = (Settings.Default.STloop ? (Settings.Default.STrepeatForever ? 0 : Settings.Default.STrepeatCount) : -1); // 0 = Always, -1 once using (var stream = new MemoryStream()) { using (var encoderNet = new GifEncoder(stream, null, null, repeat)) { for (int i = 0; i < _listBitmap.Count; i++) { encoderNet.AddFrame((_listBitmap[i]).CopyImage(), 0, 0, TimeSpan.FromMilliseconds(delay)); this.Invoke((Action)(() => processing.SetStatus(i))); } } stream.Position = 0; using ( var fileStream = new FileStream(_outputpath, FileMode.Create, FileAccess.Write, FileShare.None, Constants.BufferSize, false)) { stream.WriteTo(fileStream); } } #endregion } #region Memory Clearing //TODO: Clean the list of delay. listFramesPrivate.Clear(); listFramesUndo.Clear(); listFramesUndoAll.Clear(); listFramesPrivate = null; listFramesUndo = null; listFramesUndoAll = null; _encoder = null; GC.Collect(); //call the garbage colector to empty the memory #endregion #region Finish try { this.Invoke((Action) delegate //must use invoke because it's a cross thread call { _caller.Text = Resources.Title_EncodingDone; _stage = (int)Stage.Stoped; panelTransparent.Controls.Clear(); //Clears the processing page. processing.Dispose(); _caller.Invalidate(); btnRecordPause.Text = Resources.btnRecordPause_Record; btnRecordPause.Image = Properties.Resources.Record; flowPanel.Enabled = true; //_caller.TopMost = false; _caller.TopMost = false; numMaxFps.Enabled = true; tbHeight.Enabled = true; tbWidth.Enabled = true; _caller.MaximizeBox = true; _caller.MinimizeBox = true; _actHook.KeyDown += KeyHookTarget; //Set again the keyboard hook method _actHook.Start(false, true); //start again the keyboard hook watcher }); } catch (Exception ex) { LogWriter.Log(ex, "Invoke error."); } #endregion }
public void Execute(object data) { try { lock (thisLock) { Bitmap clipImage = (Bitmap)data; ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); string strCodecToUse = null; ImageCodecInfo codecToUse = null; EncoderParameters encParams = null; switch (int.Parse(myOptions["SelectedFormat"])) { case 0: anigifTotalFrameNum = int.Parse(myOptions["AniGIFNumFrames"]); strCodecToUse = "AniGIF"; break; case 1: strCodecToUse = "BMP"; break; case 2: encParams = new EncoderParameters(2); encParams.Param[0] = new EncoderParameter(Encoder.Quality, long.Parse(myOptions["JPEGQuality"])); if (bool.Parse(myOptions["JPEGEncoding"])) { encParams.Param[1] = new EncoderParameter(Encoder.RenderMethod, (long)EncoderValue.RenderProgressive); } else { encParams.Param[1] = new EncoderParameter(Encoder.RenderMethod, (long)EncoderValue.RenderNonProgressive); } strCodecToUse = "JPEG"; break; case 3: strCodecToUse = "GIF"; break; case 4: switch (int.Parse(myOptions["TIFFCompression"])) { case 0: encParams = new EncoderParameters(1); encParams.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone); break; case 1: encParams = new EncoderParameters(2); encParams.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT3); encParams.Param[1] = new EncoderParameter(Encoder.ColorDepth, 1L); break; case 2: encParams = new EncoderParameters(2); encParams.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4); encParams.Param[1] = new EncoderParameter(Encoder.ColorDepth, 1L); break; case 3: encParams = new EncoderParameters(1); encParams.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW); break; case 4: encParams = new EncoderParameters(2); encParams.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionRle); encParams.Param[1] = new EncoderParameter(Encoder.ColorDepth, 1L); break; } strCodecToUse = "TIFF"; break; case 5: encParams = new EncoderParameters(1); if (bool.Parse(myOptions["PNGEncoding"])) { encParams.Param[0] = new EncoderParameter(Encoder.ScanMethod, (long)EncoderValue.ScanMethodInterlaced); } else { encParams.Param[0] = new EncoderParameter(Encoder.ScanMethod, (long)EncoderValue.ScanMethodNonInterlaced); } strCodecToUse = "PNG"; break; } if (!strCodecToUse.Equals("AniGIF")) { foreach (ImageCodecInfo codec in codecs) { if (codec.FormatDescription.Equals(strCodecToUse)) { codecToUse = codec; break; } } if (codecToUse != null) { string filename = myOptions["PrefixOutputName"] + fileNum + myOptions["SuffixOutputName"] + (codecToUse.FilenameExtension.Split(new char[] { ';' }))[0].Substring(1); clipImage.Save(filename, codecToUse, encParams); fileNum++; } } else { if (!aniGifEnc.IsStarted) { anigifFrameNum = 0; aniGifEnc.Start(myOptions["PrefixOutputName"] + fileNum + myOptions["SuffixOutputName"] + ".gif"); aniGifEnc.SetFrameRate(float.Parse(myOptions["AniGIFFrameRate"])); aniGifEnc.SetRepeat(int.Parse(myOptions["AniGIFRepeat"])); aniGifEnc.SetQuality(int.Parse(myOptions["AniGIFQuality"])); if (!myOptions["AniGIFTrasparent"].Equals("NULL")) { aniGifEnc.SetTransparent(Color.FromArgb(int.Parse(myOptions["AniGIFTrasparent"]))); } } aniGifEnc.AddFrame((Image)clipImage); anigifFrameNum++; MessageBox.Show("Frame " + anigifFrameNum.ToString() + " of " + anigifTotalFrameNum + " added.", "AniGIF Creation", MessageBoxButtons.OK, MessageBoxIcon.Information); if (anigifFrameNum >= anigifTotalFrameNum) { aniGifEnc.Finish(); fileNum++; } } } } catch (System.Runtime.InteropServices.ExternalException ee) { MessageBox.Show("Error writing file.\n" + ee.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (FormatException fe) { MessageBox.Show("Error parsing the configuration file.\n" + fe.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }