Reputation: 113
What I want is to redure picture size to 480k pixels or less for any picture with size greater than 480k pixels;
-- maxSize = 480k
-- picture1.Size = 1600*300 = 480k = maxSize => that's OK.
-- picture2.Size = 2600*200 = 520k => problem (should be reduced to maxSize or less).
picture2.Size / maxSize = 1.083
picture2.Size/1.083 = 480148 (~= maxSize) => that's OK.
Let's suppose that 1.083 is the ratio for picture resizing: ratio = 1.083;
How to apply this ratio to keep picture aspect ratio?
Upvotes: 0
Views: 140
Reputation: 11928
You just want to solve the system:
newWidth * newHeight = maxSize;
newWidth / newHeight = picture2.Width / picture2.Height
and the solution is :
double newWidth = Math.Sqrt(picture2.Width * maxSize / picture2.Height);
double newHeight = Math.Sqrt(picture2.Height * maxSize / picture2.Width);
this way, the aspect ratio of picture2
will be preserved, and the size won't exceed maxSize
Upvotes: 1
Reputation: 4077
(2600-x)/(200-y) = 2600/200 and x*y = 1.083
Now the picture can be rescaled to a size less than maxSize and aspect ratio also remains the same.
Upvotes: 0