【问题标题】:aspect ratio of image crashing the program使程序崩溃的图像的纵横比
【发布时间】:2013-07-19 12:31:11
【问题描述】:

这是此代码用于保持图像的纵横比的代码,当控件转到 int WindowRatio = WidthOfPreviewPane / HeightOfPreviewPane; 时它会崩溃; 谁能告诉我为什么??

int WidthOfPreviewPane = RECTWIDTH(m_rcParent); 
int HeightOfPreviewPane = RECTHEIGHT(m_rcParent) ; 

int ImageRatio = WidthOfImage / HeightOfImage;
int WindowRatio = WidthOfPreviewPane / HeightOfPreviewPane;

if (WindowRatio > ImageRatio && WidthOfPreviewPane< WidthOfImage)
{
    m_iFinalHeight = HeightOfPreviewPane;
    m_iFinalWidth = m_iFinalHeight * ImageRatio;
    MessageBox(NULL, L"1",L"Error", 
            MB_ICONERROR | MB_OK);
}
else if (WindowRatio < ImageRatio && WidthOfPreviewPane< WidthOfImage)
{
    m_iFinalWidth = WidthOfPreviewPane;
    m_iFinalHeight = m_iFinalWidth / ImageRatio;
        MessageBox(NULL, L"2",L"Error", 
            MB_ICONERROR | MB_OK);
}
else if(WindowRatio > ImageRatio && WidthOfPreviewPane> WidthOfImage)
{
    m_iFinalHeight = HeightOfImage;
    m_iFinalWidth = WidthOfImage;
        MessageBox(NULL, L"3",L"Error", 
            MB_ICONERROR | MB_OK);

}
else if(WindowRatio < ImageRatio && WidthOfPreviewPane> WidthOfImage)
{
    m_iFinalHeight = HeightOfImage;
    m_iFinalWidth = WidthOfImage;
        MessageBox(NULL, L"4",L"Error", 
            MB_ICONERROR | MB_OK);

}

【问题讨论】:

  • 假设窗口?您是否尝试在调试器中运行代码?窗口(m_rcParent)是否可见,否则大小为 0 导致 devisionbyzero
  • 在问题行运行前检查HeightOfPreviewPane的值是多少
  • HeightOfPreviewPane 0?检查一下。
  • 是的 WidthOfPreviewPane 和 HeightOfPreviewPane 都为零..我的预览窗格怎么可能完全打开..
  • 是的,算法是绝对正确的,有一个小问题,因为我必须设置窗口位置..它由 if(WidthOfPreviewPane!= 0 && HeightOfPreviewPane!=0 ) 解决,只有让光标离开当条件正确时在里面。这是一个很大的帮助 anniekim

标签: c windows winapi preview-pane


【解决方案1】:

这个算法的逻辑是正确的,最后我发现 WidthOfPreviewPane 和 HeightOfPreviewPane=0 是因为我编写此代码的函数最后被初始化了,所以这两个在我调试它们时没有初始化,我避免了通过将它们置于 if 条件中来解决问题,如果它们的值不为 0 并且效果很好,它将让控件进入内部。 看到这个-

 if(WidthOfPreviewPane!= 0 && HeightOfPreviewPane!=0 )
            {
                  conditions here......

            }

这就解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-25
    • 2019-04-02
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多