2011-06-05 297 views
1

我想调整图像大小,同时保持在网页上显示图像的纵横比。最大图像尺寸可以是640x480。可以使用什么等式来调整图像大小?我不关心新调整大小的图像大小。分辨率应该接近640x480像素调整图像大小保持纵横比

回答

6

我解释使用C伪码。首先计算要调整图像的长宽比(“testImage”):

double rat = (double)testImage.Width/(double)testImage.Height; 

然后我们有一个640×480的画面的纵横比进行比较。如果testImage的比率(“鼠标”)大于640x480图片的比例,那么我们知道如果我们调整图片的大小以使其宽度变为640,则其高度不会超过480.如果testImage的宽高比较小,则我们可以调整图片的大小,使高度变为480而不会超过640像素的宽度。在JAVA

const double rat640x480 = 640.0/480.0; 
if (rat > rat640x480) 
    testImage.Resize(Width := 640, Height := 640/rat); 
else 
    testImage.Resize(Width := 480 * rat, Height := 480); 
0

代码变得

double ratio640x480 = 640.0/480.0; 
double sourceRatio = (double) bitmap.getWidth()/(double) bitmap.getHeight(); 

if (sourceRatio > ratio640x480) 
    bitmap = Bitmap.createScaledBitmap(bitmap, 640, (int) (640/sourceRatio), true); 
else 
    bitmap = Bitmap.createScaledBitmap(bitmap, (int) (480 * sourceRatio), 480, true); 
相关问题