sskodje / ScreenRecorderLib

A .NET library for screen recording in Windows, using native Microsoft Media Foundation for realtime encoding to h264 video or PNG images.
MIT License
414 stars 94 forks source link

Recording with system time on overlay #311

Closed manikandan300 closed 2 months ago

manikandan300 commented 3 months ago

Hi

, I am trying to add current time in overlay but it is not appearing after recording, please let know how should i record current time in video recording.

 var timestampOverlay = new ImageOverlay
            {
                AnchorPoint = ScreenRecorderLib.Anchor.BottomLeft,
                Offset = new ScreenSize(200, 300),
                Size = new ScreenSize(30, 30),
                SourcePath = null,
                SourceStream = new MemoryStream(getTimeStamp())
            };

     public byte[] getTimeStamp()
    {
        using (Bitmap timestampBitmap = new Bitmap(200, 50))
        {
            using (Graphics g = Graphics.FromImage(timestampBitmap))
            {
                var currentTime = DateTime.Now.ToString("HH:mm:ss");
                g.DrawString(currentTime, new Font("Arial", 20), Brushes.Blue, 0, 0);
            }
            return BitmapToByteArray(timestampBitmap);

        }

    }

    Thanks
sskodje commented 3 months ago

Hi! I tested your code, and it works, with the caveat that your BitmapToByteArray function works.

There's a couple things you can check. Firstly, you need to supply a valid image stream to the overlay before starting the recording. Currently it will error out if it's an empty stream or invalid, and it won't retry on update. If you don't want it to show it can be a blank PNG for example, but it must be a valid image.

Secondly, you need to save a reference to your ImageOverlay and use the same instance to call

GetDynamicOptionsBuilder().SetUpdatedOverlay(timestampOverlay ).Apply();

every time you update the timestamp.

manikandan300 commented 2 months ago

Thank for providing fix , it is working fine , I can able to see timestamp after recording, which is useful feature

           Recorder _rec;
           private ImageOverlay timestampOverlay;

        timestampOverlay = new ImageOverlay
        {
            AnchorPoint = Anchor.BottomLeft,
            Offset = new ScreenSize(10, 10),
            Size = new ScreenSize(200, 50),
            SourcePath = "logo.png",
            SourceStream = new MemoryStream(getTimeStamp())
        };

        recorderOptions.OverlayOptions.Overlays.Add(timestampOverlay);

         private byte[] getTimeStamp()
    {
        using (Bitmap timestampBitmap = new Bitmap(200, 50))
        {
            using (Graphics g = Graphics.FromImage(timestampBitmap))
            {
                var currentTime = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                g.DrawString(currentTime, new Font("Arial", 14), Brushes.Blue, 0, 0);
            }
            return BitmapToByteArray(timestampBitmap);
        }
    }

    private byte[] BitmapToByteArray(Bitmap bitmap)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            bitmap.Save(memoryStream, ImageFormat.Png);

            return memoryStream.ToArray();
        }
    }

    private void UpdateProgress(){
        _rec.GetDynamicOptionsBuilder().SetUpdatedOverlay(timestampOverlay).Apply();
    }