Trouble achieving accurate video cropping and aspect ratio in libVLC WinForms player
Posted: 20 Apr 2023 11:48
I am having difficulty achieving accurate video cropping and aspect ratio using CropGeometry and AspectRatio in LibVLCSharp WinForms player. The issue is that the selected portion is not always correct, sometimes resulting in a thin strip. Here is the code I am currently using
in the event of the termination of the selection of the area to increase:
in the event of the termination of the selection of the area to increase:
Code: Select all
private void vlcControlMain_MouseUp(object sender, MouseEventArgs e)
{
isSelecting = false;
selectionEnd = e.Location;
if (selectionStart != selectionEnd)
{
int x = Math.Min(selectionStart.X, selectionEnd.X);
int y = Math.Min(selectionStart.Y, selectionEnd.Y);
int width = Math.Abs(selectionEnd.X - selectionStart.X);
int height = Math.Abs(selectionEnd.Y - selectionStart.Y);
if (width % 2 != 0)
{
width++;
}
if (height % 2 != 0)
{
height++;
}
if (width < 10 || width > vlcControlMain.Width || height < 10 || height > vlcControlMain.Height)
{
return;
}
double aspectRatio = Math.Round((double)width / (double)height, 2);
string aspectRatioString = string.Format("{0}:{1}", width, height);
if (x < 0 || y < 0 || x + width > vlcControlMain.Width || y + height > vlcControlMain.Height)
{
return;
}
vlcControlMain.MediaPlayer.CropGeometry = $"{width}x{height}+{x}+{y}";
vlcControlMain.MediaPlayer.AspectRatio = aspectRatioString;
int playerWidth = vlcControlMain.Width;
int playerHeight = vlcControlMain.Height;
Point playerLocation = vlcControlMain.Location;
string logText = $"Selection size: {width}x{height}, player size: {playerWidth}x{playerHeight}, player location: {playerLocation}, selection start: {selectionStart}, selection end: {selectionEnd}, crop: {vlcControlMain.MediaPlayer.CropGeometry}, aspect ratio: {aspectRatioString}";
}
else
{
vlcControlMain.MediaPlayer.CropGeometry = null;
vlcControlMain.MediaPlayer.AspectRatio = null;
vlcControlMain.Width = videoWidth;
vlcControlMain.Height = videoHeight;
vlcControlMain.Location = videoPosition;
}
}