c++ - 在 Visual Studio C++ 的图片框中缩放图片

标签 c++ image visual-studio visual-c++

我在放大显示在图片框 Picture Control 中的图片时遇到问题。预期结果类似于以下链接:A scrollable, zoomable, and scalable picture box .图片在图片框内被放大。请注意图片框已放大。

但是,这可以在 .NET Framework 2.0 中实现。在网上查了一些资料,都没有用到Visual Studio的C/C++ Windows API。当我在 Visual Studio C++ 2010 的 Windows 窗体上工作时,如何在图片框 Picture Control 中缩放图片。感谢任何人的回复。

最佳答案

我遇到了同样的问题,我搜索了很多但没有在 cpp 中找到解决方案(在 c# 和 VB 中免费提供)。 所以最后我决定自己实现它。如果您的图像尺寸大于图片框,那么只有 1 级缩放(图像的原始尺寸)您可以使用我的实现。 我将帮助您实现。在当前实现中 如果您单击鼠标左键,它将放大并 如果您右击鼠标,它将缩小。

假设您已经读取了 bmpPicture 中的图像文件(.png, .jpg, .bmp, ...)。

Bitmap ^bmpPicture = gcnew Bitmap(strFilename);//如果没有它可以提供帮助

      private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {

     MouseEventArgs ^ms = (MouseEventArgs^)e;

                             if (ms->Button == System::Windows::Forms::MouseButtons::Left)
                             {
                                int pbx = ms->X;
                                int pby = ms->Y;

                                int  img_x = (pbx * mwidth / pbwidth * 1.0f);
                                int img_y = (pby * mheight / pbheight * 1.0f);

                                 pictureBox1->Location = System::Drawing::Point(pbx - img_x, pby - img_y);
                                 pictureBox1->SizeMode = PictureBoxSizeMode::AutoSize;
                                 pictureBox1->Image = bmpPicture;
    }
    else if(ms->Button == System::Windows::Forms::MouseButtons::Right)
    {

                                 int pbx = ms->X;
                                 int pby = ms->Y;

                                 pictureBox1->Location = System::Drawing::Point(0, 0);
                                 pictureBox1->SizeMode = PictureBoxSizeMode::StretchImage;
                                 pictureBox1->Image = bmpPicture;

    }
    }

    Hope this will help you...

关于c++ - 在 Visual Studio C++ 的图片框中缩放图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26056145/

相关文章:

html - 我无法移动图像,我该怎么办?

c# - 动态导航 2009 RE : SoapException: Standard Item Code 'foo' does not exist

c++ - 静态变量初始化顺序

c++ - 为什么 std::insert 需要 CopyConstructibility?

c++ - Netbeans C++ 程序没有终端输出

c++ - 在窗口中嵌入命令行?

html - 如何使图像网格适合所有屏幕分辨率?

iphone - 尝试为通用 iOS 开发创建优化图像,我应该从哪里开始?

c# - MyFirebaseMessagingService android :exported needs to be explicitly specified for element

c++ - 从头文件调用CPP函数