Ok founding pixel bounds of the image was easy, but how do I scale those bounds into image aspect ratio keeping the bounds relatively on the same place as it was in the original image? Do you have that code?
private static Rectangle ImageBounds(Image<Rgba32> image)
{
bool RowIsTransparent(Image<Rgba32> img, int y)
{
for (int x = 0; x < image.Width; x++) { if(img[x,y].A != 0) return false; }
return true;
}
// Return true if this column is all white.
bool ColumnIsTransparent(Image<Rgba32> img, int x)
{
for (int y = 0; y < image.Height; y++) { if(img[x,y].A != 0) return false; }
return true;
}
// ymin.
int ymin = image.Height - 1;
for (int y = 0; y < image.Height; y++) { if (!RowIsTransparent(image, y)) { ymin = y; break; } }
// ymax.
int ymax = 0;
for (int y = image.Height - 1; y >= ymin; y
---
) { if (!RowIsTransparent(image, y)) { ymax = y; break; } }
// xmin.
int xmin = image.Width - 1;
for (int x = 0; x < image.Width; x++) { if (!ColumnIsTransparent(image, x)) { xmin = x; break; } }
// xmax.
int xmax = 0;
for (int x = image.Width - 1; x >= xmin; x
---
) { if (!ColumnIsTransparent(image, x)) { xmax = x; break; } }
// Build the rectangle.
return new Rectangle(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
}