radek-k / FFMediaToolkit

FFMediaToolkit is a cross-platform video decoder/encoder library for .NET that uses FFmpeg native libraries. It supports video frames extraction, reading stream metadata and creating videos from bitmaps in any format supported by FFmpeg.
MIT License
352 stars 56 forks source link

Maintain ration of input no resize #102

Closed Norc89 closed 2 years ago

Norc89 commented 2 years ago

Hi, I'm merging images and videos into one big video. The problem which i have is that if not all content is in the same ratio then content is stretched. I would like to have back borders on side. If is this possible how to set this ?

radek-k commented 2 years ago

It could be done using System.Drawing.Graphics class. See the example:

private static Bitmap ScaleImage(Bitmap image, int targetWidth, int targetHeight)
{
    var output = new Bitmap(targetWidth, targetHeight, PixelFormat.Format24bppRgb);
    using (var g = Graphics.FromImage(output))
    {
        g.InterpolationMode = InterpolationMode.High;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.AntiAlias;

        var scale = Math.Min(targetWidth / (double)image.Width, targetHeight / (double)image.Height);
        var scaleWidth = (int)(image.Width * scale);
        var scaleHeight = (int)(image.Height * scale);

        g.DrawImage(image, (targetWidth - scaleWidth) / 2, (targetHeight - scaleHeight) / 2, scaleWidth, scaleHeight);
    }
    return output;
}