Exemplo n.º 1
0
        protected Image CombineScreenshots(List <ScreenshotInfo> screenshots)
        {
            List <Image> images     = new List <Image>();
            Image        finalImage = null;

            try
            {
                string infoString       = "";
                int    infoStringHeight = 0;

                if (Options.AddMovieInfo)
                {
                    infoString = MediaFile.GetMTNString();
                    using (Font font = new Font("Arial", 14))
                    {
                        infoStringHeight = Helpers.MeasureText(infoString, font).Height;
                    }
                }

                foreach (ScreenshotInfo screenshot in screenshots)
                {
                    Image img = Image.FromFile(screenshot.LocalPath);

                    if (Options.MaxThumbnailWidth > 0 && img.Width > Options.MaxThumbnailWidth)
                    {
                        int maxThumbnailHeight = (int)((float)Options.MaxThumbnailWidth / img.Width * img.Height);
                        img = ImageHelpers.ResizeImage(img, Options.MaxThumbnailWidth, maxThumbnailHeight);
                    }

                    images.Add(img);
                }

                int columnCount = Options.ColumnCount;

                int thumbWidth = images[0].Width;

                int width = Options.Padding * 2 +
                            thumbWidth * columnCount +
                            (columnCount - 1) * Options.Spacing;

                int rowCount = (int)Math.Ceiling(images.Count / (float)columnCount);

                int thumbHeight = images[0].Height;

                int height = Options.Padding * 3 +
                             infoStringHeight +
                             thumbHeight * rowCount +
                             (rowCount - 1) * Options.Spacing;

                finalImage = new Bitmap(width, height);

                using (Graphics g = Graphics.FromImage(finalImage))
                {
                    g.Clear(Color.WhiteSmoke);

                    if (!string.IsNullOrEmpty(infoString))
                    {
                        using (Font font = new Font("Arial", 14))
                        {
                            g.DrawString(infoString, font, Brushes.Black, Options.Padding, Options.Padding);
                        }
                    }

                    int i       = 0;
                    int offsetY = Options.Padding * 2 + infoStringHeight;

                    for (int y = 0; y < rowCount; y++)
                    {
                        int offsetX = Options.Padding;

                        for (int x = 0; x < columnCount; x++)
                        {
                            if (Options.DrawShadow)
                            {
                                int shadowOffset = 3;

                                using (Brush shadowBrush = new SolidBrush(Color.FromArgb(50, Color.Black)))
                                {
                                    g.FillRectangle(shadowBrush, offsetX + shadowOffset, offsetY + shadowOffset, thumbWidth, thumbHeight);
                                }
                            }

                            g.DrawImage(images[i], offsetX, offsetY, thumbWidth, thumbHeight);

                            if (Options.AddTimestamp)
                            {
                                int timestampOffset = 10;

                                using (Font font = new Font("Arial", 12))
                                {
                                    g.DrawTextWithShadow(screenshots[i].Timestamp.ToString(),
                                                         new Point(offsetX + timestampOffset, offsetY + timestampOffset), font, Brushes.White, Brushes.Black);
                                }
                            }

                            i++;

                            if (i >= images.Count)
                            {
                                return(finalImage);
                            }

                            offsetX += thumbWidth + Options.Spacing;
                        }

                        offsetY += thumbHeight + Options.Spacing;
                    }
                }

                return(finalImage);
            }
            catch
            {
                if (finalImage != null)
                {
                    finalImage.Dispose();
                }

                throw;
            }
            finally
            {
                foreach (Image image in images)
                {
                    if (image != null)
                    {
                        image.Dispose();
                    }
                }
            }
        }
Exemplo n.º 2
0
        public override void TakeScreenshot()
        {
            string tempFp     = Path.Combine(ScreenshotDir, MplayerScreenshotFileName);
            int    time_slice = (int)(MediaFile.SegmentDuration / ((Options.NumberOfScreenshots + 1) * 1000));

            if (File.Exists(tempFp))
            {
                File.Delete(tempFp);
            }

            List <string>     fpPaths = new List <string>();
            List <Screenshot> tempSS  = new List <Screenshot>();

            for (int i = 0; i < Options.NumberOfScreenshots; i++)
            {
                int    time_slice_elapsed = time_slice * (i + 1);
                string arg = string.Format("-nosound -ss {0} -zoom -vf screenshot -frames 1 -vo png:z=9:outdir=\\\"{1}\\\" \"{2}\"", time_slice_elapsed,
                                           ScreenshotDir,
                                           MediaFile.FilePath);
                ProcessStartInfo psi = new ProcessStartInfo(Program.Settings.MPlayerPath);
                psi.WindowStyle = ProcessWindowStyle.Minimized;
                psi.Arguments   = arg;

                Process p = new Process();
                p.StartInfo = psi;
                p.Start();
                p.WaitForExit(1000 * 30);

                string temp_fp = Path.Combine(ScreenshotDir, string.Format("{0}-{1}.png", Path.GetFileNameWithoutExtension(MediaFile.FilePath), time_slice_elapsed));

                if (File.Exists(temp_fp))
                {
                    File.Delete(temp_fp);
                }

                if (File.Exists(tempFp))
                {
                    File.Move(tempFp, temp_fp);
                    fpPaths.Add(temp_fp);
                    tempSS.Add(new Screenshot(temp_fp)
                    {
                        Args = arg
                    });
                }
            }

            if (fpPaths.Count > 0)
            {
                if (Options.CombineScreenshots)
                {
                    System.Drawing.Image img = Combine(fpPaths.ToArray(), 5, Options.CombineScreenshotsAddMovieInfo ? MediaFile.GetMTNString() : "");
                    string temp_fp           = Path.Combine(ScreenshotDir, Path.GetFileNameWithoutExtension(MediaFile.FilePath) + "_s.png");
                    img.Save(temp_fp, ImageFormat.Png);
                    Screenshots.Add(new Screenshot(temp_fp)
                    {
                        Args = tempSS[0].Args
                    });
                }
                else
                {
                    Screenshots.AddRange(tempSS);
                }
            }
        }