public void AddAsEmoteG(Gif gif, IMessage m, string name = "uwu")
        {
            var guild     = Program.GetGuildFromChannel(m.Channel);
            var guildUser = guild.GetUser(m.Author.Id);

            if (!guildUser.GuildPermissions.AddReactions)
            {
                throw new Exception("U no have permission for dis :c");
            }

            using MemoryStream s = new MemoryStream();
            int maxWidth  = gif.Item1.Select(x => x.Width).Max();
            int maxHeight = gif.Item1.Select(x => x.Height).Max();

            using (AnimatedGifCreator c = new AnimatedGifCreator(s, -1))
                for (int i = 0; i < gif.Item1.Length; i++)
                {
                    c.AddFrame(gif.Item1[i].CropImage(new Rectangle(0, 0, maxWidth, maxHeight)), gif.Item2[i], GifQuality.Bit8);
                }

            guild.CreateEmoteAsync(name, new Discord.Image(s)).Wait();

            foreach (Bitmap b in gif.Item1)
            {
                b.Dispose();
            }
        }
        public static void PngToGIF(string png, string info, string gif)
        {
            var reader = new BinaryReader(new FileStream(info, FileMode.Open));

            reader.ReadInt16();              // width
            reader.ReadInt16();              // height
            int rows  = reader.ReadInt16();
            int lines = reader.ReadInt16();

            if (rows == 1 & lines == 1)
            {
                return;
            }
            int rect_width  = reader.ReadInt16();
            int rect_height = reader.ReadInt16();

            reader.Close();

            var gif_creator = new AnimatedGifCreator(gif, 115, 0);
            var source      = new Bitmap(png);

            for (int i = 0; i < lines; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    var frame_rect = new Rectangle(j * rect_width, i * rect_height, rect_width, rect_height);
                    var frame      = CropImage(source, frame_rect);
                    gif_creator.AddFrame(frame);
                }
            }
            gif_creator.Dispose();
        }
Exemple #3
0
        public void SaveAsGIF(string path, GIFQuality quality)
        {
            if (imgCache != null && imgCache is HardDiskCache && !IsRecording)
            {
                Helpers.CreateDirectoryFromFilePath(path);

                HardDiskCache hdCache = imgCache as HardDiskCache;

                using (AnimatedGifCreator gifEncoder = new AnimatedGifCreator(path, delay))
                {
                    int i     = 0;
                    int count = hdCache.Count;

                    foreach (Image img in hdCache.GetImageEnumerator())
                    {
                        i++;
                        OnEncodingProgressChanged((int)((float)i / count * 100));

                        using (img)
                        {
                            gifEncoder.AddFrame(img, quality);
                        }
                    }
                }
            }
        }
Exemple #4
0
        private void Save(List <string> framesPath)
        {
            SaveFileDialog dialog = new SaveFileDialog();

            dialog.Filter   = "Gif Files(*.Gif)|*.Gif";
            dialog.FileName = "file";

            if (dialog.ShowDialog().Value)
            {
                Task.Factory.StartNew(() =>
                {
                    using (var gif = new AnimatedGifCreator(dialog.FileName, int.Parse(Properties.Settings.Default["Fps"].ToString())))
                    {
                        int value = 0;
                        foreach (var img in framesPath)
                        {
                            gif.AddFrame(img, delay: -1, quality: GifQuality.Bit8);
                            SaveUpdateStatus(++value, framesPath.Count);
                        }
                    }
                }).ContinueWith((a1) => {
                    ClearTempFiles();
                    SaveRecord();
                });
            }
        }
        public ActionResult Gif()
        {
            using (Bitmap image = GetValidateImage("123456"))
            {
                List <Bitmap> imageList = GetClearImageList(image);

                // 注意: 这里是测试,因此写个固定的文件名.
                // 实际环境下, 需要生成唯一的文件名,并定期清理.
                string webFileName = "/Output/test.gif";
                string fileName    = Server.MapPath(webFileName);

                using (AnimatedGifCreator gifCreator = AnimatedGif.AnimatedGif.Create(fileName, 300))
                {
                    //Enumerate through a List<System.Drawing.Image> or List<System.Drawing.Bitmap> for example
                    foreach (Image img in imageList)
                    {
                        using (img)
                        {
                            //Add the image to gifEncoder with default Quality
                            gifCreator.AddFrame(img, GifQuality.Default);
                        } //Image disposed
                    }
                }         // gifCreator.Finish and gifCreator.Dispose is called here


                return(File(webFileName, @"image/gif"));
            }
        }
Exemple #6
0
 private static void AddFrame(AnimatedGifCreator gif, SKImageInfo info, Action <SKCanvas> action)
 {
     using var surface = SKSurface.Create(info);
     action(surface.Canvas);
     using SKImage image = surface.Snapshot();
     using var bitmap    = image.ToBitmap();
     gif.AddFrame(bitmap, quality: GifQuality.Bit8);
 }
Exemple #7
0
        private void MakeGIF(string saveTo, Action <int, int> callback)
        {
            var files = Directory.GetFiles(ScreenRecordOption.CachePath);

            // 保存每个文件流,以方便在后面关闭文件
            var temp = new List <FileStream>(files.Length);

            int qualityValue = 3;

            this.InvokeMethod(() =>
            {
                qualityValue = tbQuality.Value;
            });
            GifQuality quality;

            switch (qualityValue)
            {
            case 1:
                quality = GifQuality.Grayscale;
                break;

            case 2:
                quality = GifQuality.Bit4;
                break;

            case 3:
                quality = GifQuality.Bit8;
                break;

            default:
                quality = GifQuality.Default;
                break;
            }
            var gifStream = new FileStream(saveTo, FileMode.Create);
            var gif       = new AnimatedGifCreator(gifStream,
                                                   1000 / ScreenRecordOption.Fps, ScreenRecordOption.RepeatCount);

            for (int i = 0; i < files.Length; i++)
            {
                callback(i, files.Length);
                var stream = LoadImage(files[i]);
                temp.Add(stream);
                gif.AddFrame(Image.FromStream(stream), quality: quality);
            }
            callback(files.Length, files.Length);
            gif.Dispose();

            foreach (var item in temp)
            {
                item.Close();
                item.Dispose();
            }
            temp.Clear();

            GC.Collect();
        }
 /// <summary> Gifアニメーションをファイルに保存
 /// <param name="savePath">保存先ファイル名</param>
 /// <param name="delay">フレームディレイ(ms)</param>
 public void Save(string savePath, int delay)
 {
     using (AnimatedGifCreator gifCreator = AnimatedGif.AnimatedGif.Create(savePath, delay, 1)) { //NoRepeat
         foreach (MemoryStream frame in this.ImageList)
         {
             using (Image img = (Image)Decompress(frame)){
                 gifCreator.AddFrame(img, delay, GifQuality.Default);
                 frame.Dispose();
             }
         }
     }
     //Clear Image buffer
     this.ImageList.Clear();
     this.ImageList.TrimExcess();
 }
Exemple #9
0
 //--Not Auto-Play
 private void GenerateByAnimatedGifCreator()
 {
     //Create new Animated GIF Creator with Path to C:\awesomegif.gif and 33ms delay between frames (=30 fps)
     using (AnimatedGifCreator gifCreator = new AnimatedGifCreator(@"D:\Playground\BackYard\bucket\output\awesomegif6.gif", 33))
     {
         //Enumerate through a List<System.Drawing.Image> or List<System.Drawing.Bitmap> for example
         List <Image> imgList = new List <Image>();
         //for (int i = 0; i <= 100; i++)
         //{
         //    imgList.Add(Image.FromFile(@"D:\0-Desktop\gif-research\Comp 1\"+string.Format("000",i)+".png"));
         //}
         imgList.Add(Image.FromFile(@"D:\0-Desktop\gif-research\set-2\sleepy-1.png"));
         imgList.Add(Image.FromFile(@"D:\0-Desktop\gif-research\set-2\sleepy-2.png"));
         foreach (Image img in imgList)
         {
             using (img)
             {
                 //Add the image to gifEncoder with default Quality
                 gifCreator.AddFrame(img, GifQuality.Default);
             } //Image disposed
         }
     }         // gifCreator.Finish and gifCreator.Dispose is called here
 }
Exemple #10
0
        private unsafe void CreateGifBtnClick(object sender, EventArgs e)
        {
            using (SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                Filter = "Gif File|*.gif"
            })
            {
                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                string filePath = saveFileDialog.FileName;

                int timeInSeconds   = int.Parse(Interaction.InputBox("Enter the length of the gif (in seconds)"));
                int framesPerSecond = int.Parse(Interaction.InputBox("Enter how many frames per second"));

                Bitmap frame;
                using (AnimatedGifCreator gif = AnimatedGif.AnimatedGif.Create(filePath, framesPerSecond))
                {
                    int[] frameBytes = new int[_panelSize * _panelSize * 1];
                    for (int i = 0; i < frameBytes.Length; i++)
                    {
                        frameBytes[i] = Byte.MaxValue;
                    }
                    frame = new Bitmap(_panelSize, _panelSize, PixelFormat.Format32bppRgb);
                    int split = _lines.Count / (timeInSeconds * framesPerSecond);
                    if (_lines.Count % (timeInSeconds * framesPerSecond) != 0)
                    {
                        split = _lines.Count / (timeInSeconds * framesPerSecond - 1);
                    }
                    int lineChange = (int)lineChangeNumeric.Value;
                    fixed(int *frameBytesPtr = frameBytes)
                    {
                        for (int i = 0; i < timeInSeconds; i++)
                        {
                            for (int j = 0; j < framesPerSecond; j++)
                            {
                                int startingLineIndex = (i * framesPerSecond + j) * split;
                                int endingLineIndex   = Math.Min(_lines.Count, startingLineIndex + split);
                                Console.WriteLine("{0} - {1} out of {2}", startingLineIndex, endingLineIndex, _lines.Count);
                                for (int currentLineIndex = startingLineIndex; currentLineIndex < endingLineIndex; currentLineIndex++)
                                {
                                    (PointF point1, PointF point2) = _lines[currentLineIndex];
                                    ApplyLine(frameBytesPtr, point1, point2, lineChange);
                                }

                                BitmapData frameBitmapData = frame.LockBits(
                                    new Rectangle(0, 0, frame.Width, frame.Height),
                                    ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);

                                for (int y = 0; y < _panelSize; y++)
                                {
                                    for (int x = 0; x < _panelSize; x++)
                                    {
                                        int   grayValue     = frameBytesPtr[y * _panelSize + x];
                                        byte  grayValueByte = (byte)Math.Max(grayValue, 0);
                                        byte *framePtr      = (byte *)frameBitmapData.Scan0 + y * frameBitmapData.Stride +
                                                              x * (32 / 8);
                                        framePtr[0] = grayValueByte;
                                        framePtr[1] = grayValueByte;
                                        framePtr[2] = grayValueByte;
                                    }
                                }

                                frame.UnlockBits(frameBitmapData);

                                gif.AddFrame(frame, -3, GifQuality.Bit8);
                            }
                        }
                    }
                }

                frame.Dispose();
            }
        }
        // GIF
        public void StartRecordingGif()
        {
            if (processingGif | recordingGif)
            {
                return;
            }

            if (snipper.gifArea.Width == 0 || snipper.gifArea.Height == 0)
            {
                if (!shownGifSnipperHelp)
                {
                    shownGifSnipperHelp = true;
                    DialogResult result = MessageBox.Show("You need to select an gif area in the SelectionForm™ before recording a gif!", "Missing Gif Area", MessageBoxButtons.OK);
                    if (result == DialogResult.OK)
                    {
                        shownGifSnipperHelp = false;
                    }
                }
                return;
            }

            processingGif = true;
            recordingGif  = true;
            gifShots.Clear();

            Task.Factory.StartNew(() =>
            {
                List <Task> runners = new List <Task>();
                for (int i = 0; i < 750 && recordingGif; i++)
                {
                    runners.Add(Task.Factory.StartNew(() =>
                    {
                        gifShots.Add(ScreenshotHelper.GetRectScreenshot(snipper.gifArea));
                        Debug.WriteLine($"Made gif shot {i}!");
                    }));

                    Task.Delay(33).Wait();
                }
                Debug.WriteLine("waiting for close...");
                Task.WaitAll(runners.ToArray());
                Debug.WriteLine("closed");

                string path = config.Default.path + "\\" + GetScreenshotName() + ".gif";
                using (FileStream s = new FileStream(path, FileMode.Create))
                {
                    using AnimatedGifCreator c = new AnimatedGifCreator(s, 40);
                    foreach (Bitmap b in gifShots)
                    {
                        c.AddFrame(b, -1, GifQuality.Default);
                    }
                }

                images.Add(new Screenshot(path));
                imagesIndex = images.Count - 1;
                CurrentScreenshot.Save();

                this.InvokeIfRequired(() => UpdateUI());

                processingGif = false;
            });
        }
Exemple #12
0
        private void ToGif(string outputFile)
        {
            var bounds = new FrameBounds[frames.Count];

            int i = 0;

            foreach (var x in frames)
            {
                bounds[i++] = new FrameBounds(x.X, x.Y, x.Width, x.Height, x);
                Console.WriteLine("{0} {1} {2} {3}", x.X, x.Y, x.Width, x.Height);
            }


            var minLeft     = bounds.Min(x => x.left);
            var minTop      = bounds.Min(x => x.top);
            var totalBounds = new FrameBounds()
            {
                left   = bounds.Min(x => x.left),
                top    = bounds.Min(x => x.top),    // Top of the image
                right  = bounds.Max(x => x.right),
                bottom = bounds.Max(x => x.bottom), // Bottom of the image
            };


            Console.WriteLine("{0} {1}", minLeft, minTop);

            foreach (var fb in bounds)
            {
                Console.WriteLine(fb.ToString());
                fb.Move(-minLeft, -minTop);
                Console.WriteLine(fb.ToString());
            }

            Console.WriteLine("{0} {1}", totalBounds.left, totalBounds.top);

            int w = totalBounds.Width;
            int h = totalBounds.Height;

            int minx = 0, miny = 0;

            using (var gif = new AnimatedGifCreator(outputFile, 0, 0))
            {
                foreach (var b in bounds)
                {
                    using (var bm = new Bitmap(w + 1, h + 1))
                        using (var g = Graphics.FromImage(bm))
                        {
                            int x = b.left;
                            int y = b.top;
                            Console.WriteLine("Drawing image {0} - {1}, {2} - {3}", x, x + b.Width, y, y + b.Height);

                            g.DrawImageUnscaled(
                                b.frame.Image.Tile,
                                x,
                                y
                                );

                            gif.AddFrame(
                                bm,
                                b.frame.delay,
                                GifQuality.Bit8
                                );
                        }
                }
            }
        }
Exemple #13
0
        private void ToGif(string outputFile)
        {
            var bounds = new FrameBounds[frames.Count];

            int i = 0;

            foreach (var x in frames)
            {
                bounds[i++] = new FrameBounds(x.OffsetX, x.OffsetY, x.Width, x.Height, x);
                Console.WriteLine("{0} {1} {2} {3}", x.OffsetX, x.OffsetY, x.Width, x.Height);
            }


            var minLeft     = bounds.Min(x => x.left);
            var minTop      = bounds.Min(x => x.top);
            var totalBounds = new FrameBounds()
            {
                left   = bounds.Min(x => x.left),
                top    = bounds.Min(x => x.top),    // Top of the image
                right  = bounds.Max(x => x.right),
                bottom = bounds.Max(x => x.bottom), // Bottom of the image
            };

            // Make sure that the image is in bounds
            foreach (var fb in bounds)
            {
                fb.Move(-minLeft, -minTop);
            }


            int w = totalBounds.Width;
            int h = totalBounds.Height;

            bool checkerBoard = useCheckerboard.Checked;

            using (var backgroundChecker = new Bitmap(w + 1, h + 1))
            {
                const int backgroundCheckerWidth  = 8;
                const int backgroundCheckerHeight = 8;

                using (var b = new Bitmap(backgroundCheckerWidth, backgroundCheckerHeight))
                {
                    using (var g = Graphics.FromImage(b))
                    {
                        var c1          = new SolidBrush(Color.Gray);
                        var c2          = new SolidBrush(Color.DarkGray);
                        var blockWidth  = backgroundCheckerWidth / 2;
                        var blockHeight = backgroundCheckerHeight / 2;

                        g.FillRectangle(c1, 0, 0, blockWidth, blockHeight);
                        g.FillRectangle(c2, blockWidth, 0, blockWidth, blockHeight);

                        g.FillRectangle(c1, blockWidth, blockHeight, blockWidth, blockHeight);
                        g.FillRectangle(c2, 0, blockHeight, blockWidth, blockHeight);
                    }

                    using (var g = Graphics.FromImage(backgroundChecker))
                    {
                        var checkersX = (w / backgroundCheckerWidth) + 1;
                        var checkersY = (h / backgroundCheckerHeight) + 1;
                        for (int y = 0; y < checkersY; y++)
                        {
                            for (int x = 0; x < checkersX; x++)
                            {
                                g.DrawImage(b, new Point(x * backgroundCheckerWidth, y * backgroundCheckerHeight));
                            }
                        }
                    }
                }

                using (var gif = new AnimatedGifCreator(outputFile, 0, 0))
                {
                    using (var bm = new Bitmap(w + 1, h + 1))
                        using (var g = Graphics.FromImage(bm))
                        {
                            foreach (var b in bounds)
                            {
                                g.Clear(Color.FromArgb(0));

                                if (checkerBoard)
                                {
                                    g.DrawImageUnscaled(backgroundChecker, 0, 0);
                                }

                                int x = b.left;
                                int y = b.top;
                                Console.WriteLine("Drawing image {0} - {1}, {2} - {3}", x, x + b.Width, y, y + b.Height);

                                g.DrawImageUnscaled(
                                    b.frame.Tile,
                                    x,
                                    y
                                    );


                                gif.AddFrame(
                                    bm,
                                    b.frame.delay,
                                    GifQuality.Bit8
                                    );
                            }
                        }
                }
            }
        }
Exemple #14
0
    public static void SaveToGif2(float time = 0)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.Filter = "Gif Image|*.gif";
        saveFileDialog.Title  = "Save a Gif File";

        GifQuality gifQuality = new GifQuality();

        switch (App.globalValues.GifQuality)
        {
        case "Default":
            gifQuality = GifQuality.Default;
            break;

        case "Bit8":
            gifQuality = GifQuality.Bit8;
            break;

        case "Bit4":
            gifQuality = GifQuality.Bit4;
            break;

        case "Grayscale":
            gifQuality = GifQuality.Grayscale;
            break;

        default:
            gifQuality = GifQuality.Default;
            break;
        }
        string fileName = GetFileNameNoEx(App.globalValues.SelectAtlasFile);

        if (App.globalValues.SelectAnimeName != "")
        {
            fileName += $"_{App.globalValues.SelectAnimeName}";
        }
        if (App.globalValues.SelectSkin != "")
        {
            fileName += $"_{App.globalValues.SelectSkin}";
        }

        saveFileDialog.FileName = fileName;
        saveFileDialog.ShowDialog();

        string[] pngList = Directory.GetFiles($"{App.rootDir}\\Temp\\", "*.png", SearchOption.TopDirectoryOnly);


        int delay = 0;

        if (time == 0)
        {
            delay = 1000 / App.globalValues.Speed;
        }
        else
        {
            delay = (int)(time * 1000 * (App.globalValues.Speed / 30f) / pngList.Length);
        }

        if (saveFileDialog.FileName != "")
        {
            using (AnimatedGifCreator gifCreator = AnimatedGif.AnimatedGif.Create(saveFileDialog.FileName, delay, App.globalValues.IsLoop == true ? 0 : 1))
            {
                foreach (string path in pngList)
                {
                    using (FileStream fsimg = new FileStream(path, FileMode.Open))
                    {
                        using (Image img = Image.FromStream(fsimg))
                        {
                            gifCreator.AddFrame(img, -1, gifQuality);
                        }
                    }
                }
            }
        }
        else
        {
        }
        ClearCacheFile();

        GC.Collect();
    }
Exemple #15
0
    public static void SaveToGif(List <MemoryStream> lms, float time = 0)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.Filter = "Gif Image|*.gif";
        saveFileDialog.Title  = "Save a Gif File";

        GifQuality GQ = new GifQuality();

        switch (App.GV.GifQuality)
        {
        case "Default":
            GQ = GifQuality.Default;
            break;

        case "Bit8":
            GQ = GifQuality.Bit8;
            break;

        case "Bit4":
            GQ = GifQuality.Bit4;
            break;

        case "Grayscale":
            GQ = GifQuality.Grayscale;
            break;

        default:
            GQ = GifQuality.Default;
            break;
        }
        string fileName = GetFileNameNoEx(App.GV.SelectFile);

        if (App.GV.SelectAnimeName != "")
        {
            fileName += $"_{App.GV.SelectAnimeName}";
        }
        if (App.GV.SelectSkin != "")
        {
            fileName += $"_{App.GV.SelectSkin}";
        }

        saveFileDialog.FileName = fileName;
        saveFileDialog.ShowDialog();
        int delay = 0;

        if (time == 0)
        {
            delay = 1000 / App.GV.Speed;
        }
        else
        {
            delay = (int)(time * 1000 / lms.Count);
        }

        if (saveFileDialog.FileName != "")
        {
            using (AnimatedGifCreator gifCreator = AnimatedGif.AnimatedGif.Create(saveFileDialog.FileName, delay, App.GV.IsLoop == true ? 0 : 1))
            {
                foreach (MemoryStream msimg in lms)
                {
                    using (msimg)
                    {
                        using (Image img = Image.FromStream(msimg))
                        {
                            gifCreator.AddFrame(img, -1, GQ);
                        }
                    }
                }
            }
        }
        else
        {
            foreach (MemoryStream ms in lms)
            {
                ms.Dispose();
            }
        }
        lms.Clear();
        GC.Collect();
    }
 public void AddFrame(Image frame)
 {
     _gif.AddFrame(frame, delay: -1, quality: GifQuality.Bit8);
 }
Exemple #17
0
    public static void SaveToGif(List <MemoryStream> lms)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.Filter = "Gif Image|*.gif";
        saveFileDialog.Title  = "Save a Gif File";

        GifQuality GQ = new GifQuality();

        switch (App.GV.GifQuality)
        {
        case "Default":
            GQ = GifQuality.Default;
            break;

        case "Bit8":
            GQ = GifQuality.Bit8;
            break;

        case "Bit4":
            GQ = GifQuality.Bit4;
            break;

        case "Grayscale":
            GQ = GifQuality.Grayscale;
            break;

        default:
            GQ = GifQuality.Default;
            break;
        }

        string fileName = GetFileNameNoEx(App.GV.SelectFile);

        if (App.GV.SelectAnimeName != "")
        {
            fileName += $"_{App.GV.SelectAnimeName}";
        }
        if (App.GV.SelectSkin != "")
        {
            fileName += $"_{App.GV.SelectSkin}";
        }

        saveFileDialog.FileName = fileName;
        saveFileDialog.ShowDialog();

        if (saveFileDialog.FileName != "")
        {
            using (AnimatedGifCreator gifCreator = AnimatedGif.AnimatedGif.Create(saveFileDialog.FileName, 1000 / App.GV.Speed))
            {
                foreach (MemoryStream img in lms)
                {
                    using (img)
                    {
                        gifCreator.AddFrame(Image.FromStream(img), GQ);
                        img.Dispose();
                    }
                }
            }
        }
        else
        {
            foreach (MemoryStream ms in lms)
            {
                ms.Dispose();
            }
        }
        lms.Clear();
    }