public static void WriteBackgroundForFrames(Image background, IEnumerable<Frame> frames, string outputPath)
 {
     using (var output = File.OpenWrite(outputPath))
     using (var outputWriter = new GifEncoder(output))
     {
         foreach (var frame in frames)
         {
             outputWriter.FrameDelay = TimeSpan.FromMilliseconds(frame.Duration);
             outputWriter.AddFrame(background);
         }
     }
 }
Example #2
0
        public override int Run(string[] remainingArguments)
        {
            using(var output = File.OpenWrite(Output))
            using(var outputWriter = new GifEncoder(output))
            {
                foreach(var inputFile in Inputs)
                {
                    ImageResizerUtil.ProcessImage(
                        new PluginList().WithAnimatedGifExtensions().WithPlugin(new AnimationVisitorExtension((bitmap, graphic, delay) =>
                            {
                                outputWriter.FrameDelay = TimeSpan.FromMilliseconds(delay);
                                outputWriter.AddFrame(bitmap);
                            })).Plugins,
                            inputFile);
                }
            }

            return 0;
        }
Example #3
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ClearContent();
            // squirt it out
            context.Response.BufferOutput = false;

            System.Data.SqlClient.SqlConnection m_sqlConnection;

            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["picConnect"].ConnectionString;

            m_sqlConnection = new System.Data.SqlClient.SqlConnection(connStr);

            m_sqlConnection.Open();

            // prepare the statement
            System.Data.SqlClient.SqlCommand insertCmd = new System.Data.SqlClient.SqlCommand("getPicRange", m_sqlConnection);
            insertCmd.CommandType = CommandType.StoredProcedure;

            System.Data.SqlClient.SqlDataReader read = insertCmd.ExecuteReader();

            System.IO.MemoryStream gif=new System.IO.MemoryStream();

            BumpKit.GifEncoder encoder = new GifEncoder(gif);

            encoder.FrameDelay = System.TimeSpan.FromMilliseconds(250);

            while (read.Read())
            {
                using (System.IO.MemoryStream image = new System.IO.MemoryStream((byte[])read.GetSqlBinary(read.GetOrdinal("pic"))))
                {
                    encoder.AddFrame(System.Drawing.Image.FromStream(image));
                }

            }

            context.Response.AddHeader("Content-Length", System.Convert.ToString(gif.Length));
            context.Response.AddHeader("Content-Type", "image/gif");

            gif.WriteTo(context.Response.OutputStream);

            m_sqlConnection.Close();
        }
Example #4
0
 public void create(int years, int step, string path, int width, int height)
 {
     startedEvent.Invoke(this, EventArgs.Empty);
     double seconds = years * 365.25 * 24 * 60 * 60;
     double frames = Math.Floor(seconds / step);
     int fontSize = (int)(width * .1);
     using (FileStream gif = File.OpenWrite(Path.Combine(path, "Longer_" + frames + "_frames.gif")))
     using (GifEncoder encoder = new GifEncoder(gif)) {
         encoder.FrameDelay = TimeSpan.FromSeconds(step);
         for (double i = 0; i < frames; i++) {
             using (Bitmap frame = new Bitmap(width, height))
             using (Graphics g = Graphics.FromImage(frame))
             using (Font font = new Font("Helvetica", fontSize)) {
                 string frameText = (i + 1).ToString();
                 SizeF textSize = g.MeasureString(frameText, font);
                 g.DrawString(frameText, font, Brushes.White, width / 2 - textSize.Width / 2, height / 2 - textSize.Height / 2);
                 encoder.AddFrame(frame);
             }
             progressUpdateEvent.Invoke(this, new ProgressUpdateEventArgs((int)((i+1D) / frames * 100D)));
         }
     }
     finishedEvent.Invoke(this, EventArgs.Empty);
 }
Example #5
0
        private void button1_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog { Title = "Please select an image to open:", Filter = "Jpeg|*.jpg|Jpeg|*.jpeg|Png|*.png|Bitmap|*.bmp" };
            if (dlg.ShowDialog() != DialogResult.OK)
                return;

            var img = Image.FromFile(dlg.FileName);

            // Scale to fit
            _scaleToFit.BackgroundImage = img.ScaleToFit(_scaleToFit.Size, false);

            // Scale to fit (overflow)
            _scaleToOverflow.BackgroundImage = img.ScaleToFit(_scaleToOverflow.Size, false, ScalingMode.Overflow);

            // Stretch to fit
            _stretch.BackgroundImage = img.Stretch(_stretch.Size, false);

            // Rotate and fit
            _rotateFit.BackgroundImage = img.ScaleToFit(_rotateFit.Size, false).Rotate(45, ScalingMode.FitContent).ScaleToFit(_rotateOverflow.Size);
            
            // Rotate and overflow
            _rotateOverflow.BackgroundImage = img.ScaleToFit(_rotateOverflow.Size, false).Rotate(45);

            // Fast pixel manipulation (UnsafeContext)
            _pixelManipulation.BackgroundImage = img.ScaleToFit(_pixelManipulation.Size, false);
            using (var context = _pixelManipulation.BackgroundImage.CreateUnsafeContext())
            {
                for (var y = 0; y < context.Height; y++)
                    for (var x = 0; x < context.Width; x++)
                    {
                        var pixel = context.GetRawPixel(x, y);
                        var level = Math.Max(pixel.Red, Math.Max(pixel.Green, pixel.Blue));
                        context.SetPixel(x, y, pixel.Alpha, level, level, level);
                    }
            }
            _pixelManipulation.Refresh();

            // Detect Edges
            var edge = new Bitmap(_edgeDetection.Width, _edgeDetection.Height);
            using (var gfx = Graphics.FromImage(edge))
            {
                gfx.DrawEllipse(Pens.Black, 25, 25, 50, 50);
                var bounds = edge.DetectPadding();
                gfx.DrawLine(Pens.Red, bounds.Left, 0, bounds.Left, edge.Height);
                gfx.DrawLine(Pens.Red, bounds.Right, 0, bounds.Right, edge.Height);
                gfx.DrawLine(Pens.Red, 0, bounds.Top, edge.Width, bounds.Top);
                gfx.DrawLine(Pens.Red, 0, bounds.Bottom, edge.Width, bounds.Bottom);
            }
            _edgeDetection.BackgroundImage = edge;

            // Create animated Gif
            var gifImage = img.ScaleToFit(_gifGeneration.Size, false);
            var gifStream = new MemoryStream(); // NOTE: Disposing this stream will break this demo - I don't know why.
            using (var encoder = new GifEncoder(gifStream))
                for (var angle = 0; angle < 360; angle += 10)
                    using (var frame = gifImage.Rotate(angle, false))
                    {
                        encoder.AddFrame(frame, 0, 0, TimeSpan.FromSeconds(0));
                    }
            gifStream.Position = 0;
            _gifGeneration.Image = Image.FromStream(gifStream);

            // Draw text with effects
            _textGen.BackgroundImage = img.ScaleToFit(_textGen.Size, false, ScalingMode.Overflow);
            using (var gfx = Graphics.FromImage(_textGen.BackgroundImage))
            {
                gfx.DrawString("A B C 1 2 3", new Font(FontFamily.Families.First(f => f.Name.Contains("Times")), 15), Brushes.Green, 15, 25, 10,
                    new[] {Color.Yellow, Color.Blue, Color.Red, Color.Green, Color.Purple, Color.Black},
                    new[] { 0, (float).20, (float).40, (float).60, (float).80, 1 });
            }
        }