C
C#2y ago
Vinicius

✅ Skiarsharp - Bestway to check if image is landscape or portrait mode.

Actually my problem: an image with 4000x6000 going to landscape. But image like 845x852 won't. First approach I saw is width > height = landscape. What is the best way I can check if is portrait or landscape image?
private bool TryProcessOriginalFile(string path)
{
using var stream = File.OpenRead(Path.Combine(path, "original"));
var bmpLossless = SKBitmap.Decode(stream);

if (bmpLossless == null)
{
return false;
}
//Only using it isnt trustful
if (bmpLossless.Width > bmpLossless.Height)
{
bmpLossless = RotateLandscapeToPortrait(bmpLossless);
}

var outPathJpg = Path.Combine(path, "original.jpg");
using (var outFileJpg = new FileStream(outPathJpg, FileMode.Create))
{
bmpLossless.Encode(SKEncodedImageFormat.Jpeg, 70).SaveTo(outFileJpg);
}
bmpLossless.Dispose();
return true;
}

private static SKBitmap RotateLandscapeToPortrait(SKBitmap bmpLossless)
{
var rotated = new SKBitmap(bmpLossless.Height, bmpLossless.Width);
using (var canvas = new SKCanvas(rotated))
{
canvas.Clear();
canvas.Translate(0, bmpLossless.Width);
canvas.RotateDegrees(-90);
canvas.DrawBitmap(bmpLossless, new SKPoint());
}

return rotated;
}
private bool TryProcessOriginalFile(string path)
{
using var stream = File.OpenRead(Path.Combine(path, "original"));
var bmpLossless = SKBitmap.Decode(stream);

if (bmpLossless == null)
{
return false;
}
//Only using it isnt trustful
if (bmpLossless.Width > bmpLossless.Height)
{
bmpLossless = RotateLandscapeToPortrait(bmpLossless);
}

var outPathJpg = Path.Combine(path, "original.jpg");
using (var outFileJpg = new FileStream(outPathJpg, FileMode.Create))
{
bmpLossless.Encode(SKEncodedImageFormat.Jpeg, 70).SaveTo(outFileJpg);
}
bmpLossless.Dispose();
return true;
}

private static SKBitmap RotateLandscapeToPortrait(SKBitmap bmpLossless)
{
var rotated = new SKBitmap(bmpLossless.Height, bmpLossless.Width);
using (var canvas = new SKCanvas(rotated))
{
canvas.Clear();
canvas.Translate(0, bmpLossless.Width);
canvas.RotateDegrees(-90);
canvas.DrawBitmap(bmpLossless, new SKPoint());
}

return rotated;
}
6 Replies
TheBoxyBear
TheBoxyBear2y ago
There's no real way of doing it than just Height > Width Or divide to find the ratio and determine a threshold you determine to be portrait Even the phones that takes the pictures struggle and just keep it right side up based on the orientation of the device and zoom it so the whole thing fits on the screen
Vinicius
ViniciusOP2y ago
Im doing radio calculate after that its hard to show the bigger picture but
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Vinicius
ViniciusOP2y ago
I still dont know how to get the cordinates how says which mode it is
Vinicius
ViniciusOP2y ago
useful ?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server