由两个整数除以不返回预期结果 [英] Dividing by two integers does not return expected result

查看:90
本文介绍了由两个整数除以不返回预期结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个需要实时显示的预览程序,但预览,当然是按比例缩小。然而,当我缩放图片框下来,大小不正确。对于规模为正确的宽度和高度必须在4:3的比例。下面的代码:

I'm currently writing a program that requires a preview of a live display, but the preview, of course, is scaled down. However, when I scale the PictureBox down, the size is incorrect. For the scale to be correct the width and height need to be at a 4:3 ratio. Here's the code:

private void FindOptimalRes(PictureBox picBox)
{
    double h = Height / 4;
    double ratio = 4 / 3;
    picBox.Size = new Size((int)(h * ratio), (int)h);
}

在测试中,身高(形式的高度)为400,所以,新的大小的宽度应为133。但它始终被调整至100倍; 100!为什么呢?

In testing, Height (the height of the form) is 400, so, the width of the new size should be 133. But it always gets resized to 100×100! Why?

推荐答案

4 3 都是 INT S,所以它被转到 1 。让他们的东西浮点:

4 and 3 are both ints, so it gets turned to 1. Make them something floating-point:

double ratio = 4.0 / 3.0;

请注意,你也犯同样的错误与身高(也没关系,现在,但它会 - 将其更改为 4.0 )。如果这是实际的代码,为什么由四个鸿沟由四个一遍吗?

Note that you're also making the same mistake with Height (it doesn't matter right now, but it will - change it to 4.0). And if this is the actual code, why divide by four to multiply by four again?

private void FindOptimalRes(PictureBox picBox)
{
    picBox.Size = new Size(Height / 3, Height / 4);
}

这篇关于由两个整数除以不返回预期结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆