Video subtitles

Jun 13, 2011 at 4:30 PM
Edited Jun 13, 2011 at 4:33 PM

Hello.

First of all let me just say that I think this is a great library, truly amazing.

Now, onto my problem =)
I've been trying to write a DShow filter do display subtitles on video.
So far I've managed to get the video, and write subtitle text on a transparent overlay bmp, but text rendering is slow and not of very nice quality.
  Can you please help me and maybe point out what I'm doing wrong...

Here's my code, I'm calling this method for every video frame...

private void PrintSub(clsTitl titl, IntPtr pBuffer)
        {
            using (Graphics g = Graphics.FromImage(bitmapOverlay))
            {
                g.Clear(Color.Transparent);

                float sirinaStringa = 0, visinaStringa = 0, bla = 0, blu = 0;

                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //Drawing the back ground color
                Color m_clrBkgd = Color.FromArgb(255, 255, 128);
                SolidBrush brushBkgnd = new SolidBrush(m_clrBkgd);
                //e.Graphics.FillRectangle(brushBkgnd, 0, 0, this.ClientSize.Width, this.ClientSize.Width);
                FontFamily fontFamily = new FontFamily("Arial");

                StringFormat strFormat = new StringFormat();
                strFormat.Alignment = StringAlignment.Center;

                PngOutlineText m_PngOutlineText = new PngOutlineText();
                m_PngOutlineText.EnableShadow(true);

                //Rem to SetNullShadow() to release memory if a previous shadow has been set.
                m_PngOutlineText.SetNullShadow();
                m_PngOutlineText.DiffusedShadow(Color.FromArgb(255, 0, 0, 0), 2, new Point(2, 2));


                m_PngOutlineText.MeasureString(g, fontFamily, FontStyle.Bold, 32, titl.SubtitleContent,
                    new Point(10, 10), strFormat, ref bla, ref blu, ref sirinaStringa, ref visinaStringa);

                m_PngOutlineText.TextGlow(
                    Color.FromArgb(255, 255, 255),
                    Color.FromArgb(200, 0, 0, 0),
                    5);

                Bitmap m_pPngImage = new Bitmap((int)(sirinaStringa + 5.0f), (int)(visinaStringa + 5.0f),
                    System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                m_PngOutlineText.SetPngImage(m_pPngImage);
                m_PngOutlineText.SetShadowBkgd(m_clrBkgd, m_pPngImage.Width, m_pPngImage.Height);

                //Pozicija na ekranu
                m_PngOutlineText.DrawString(g, fontFamily, FontStyle.Bold, 32, titl.SubtitleContent, new Point(0, 0), strFormat);
                m_pPngImage.RotateFlip(RotateFlipType.RotateNoneFlipY);

                using (Bitmap v = new Bitmap(m_videoWidth, m_videoHeight, m_stride,
                    PixelFormat.Format32bppArgb, pBuffer))
                {
                    using (Graphics graf = Graphics.FromImage(v))
                    {
                        int pozicija = (m_videoWidth / 2);// -(m_pPngImage.Width / 2);
                        graf.DrawImage(m_pPngImage, pozicija, 0, m_pPngImage.Width, m_pPngImage.Height);
                    }
                }
            }
        }

Jun 14, 2011 at 12:08 AM

It seems that m_PngOutlineText.MeasureString is a real performance killer...
Instead I tried with graphics.MeasureString to get the dimensions and it works without any problems.