C# Halcon 图像放大缩小代码

 

private void hWindowControl1_HMouseWheel(object sender, HalconDotNet.HMouseEventArgs e)
        {
            if (ho_img != null)
            {
                try
                {
                    //获取鼠标位置
                    hwin1.GetMpositionSubPix(out mposition_row, out mposition_col, out button_state);
                    //获取显示控件像素位置
                    hwin1.GetPart(out current_beginRow1, out current_beginCol1, out current_endRow1, out current_endCol1);
                    //获取显示像素大小
                    Ht = current_endRow1 - current_beginRow1;
                    Wt = current_endCol1 - current_beginCol1;
                }
                catch (HalconException)
                {
                }
                if (e.Delta > 0)                 // 放大图像
                {
                    if (Ht > 4)
                    {
                        zoom_beginRow1 = (int)(current_beginRow1 + (mposition_row - current_beginRow1) * 0.300d);
                        zoom_beginCol1 = (int)(current_beginCol1 + (mposition_col - current_beginCol1) * 0.300d);
                        zoom_endRow1 = (int)(current_endRow1 - (current_endRow1 - mposition_row) * 0.300d);
                        zoom_endCol1 = (int)(current_endCol1 - (current_endCol1 - mposition_col) * 0.300d);
                        hwin1.SetPart(zoom_beginRow1, zoom_beginCol1, zoom_endRow1, zoom_endCol1);
                        hwin1.ClearWindow();
                        try
                        {
                            HSystem.SetSystem("flush_graphic", "true");
                            if (cbxImage.Text != ""&& Image.Count>0)
                               DispImage(Image[cbxImage.Text], true);
                            else
                                hwin1.DispObj(ho_img);
                            OnHMouseWheelEvent();
                            if (中心ToolStripMenuItem.Checked)
                                disp_win_cross(ho_img);
                            HSystem.SetSystem("flush_graphic", "false");
                        }
                        catch (HalconException)
                        {
                        }
                    }
                }

                else                           // 缩小图像
                {

                    if (Ht < 8000)
                    {
                        zoom_beginRow1 = (int)(mposition_row - (mposition_row - current_beginRow1) / 0.700d);
                        zoom_beginCol1 = (int)(mposition_col - (mposition_col - current_beginCol1) / 0.700d);
                        zoom_endRow1 = (int)(mposition_row + (current_endRow1 - mposition_row) / 0.700d);
                        zoom_endCol1 = (int)(mposition_col + (current_endCol1 - mposition_col) / 0.700d);
                        hwin1.SetPart(zoom_beginRow1, zoom_beginCol1, zoom_endRow1, zoom_endCol1);
                        hwin1.ClearWindow();
                        try
                        {
                            HSystem.SetSystem("flush_graphic", "true");
                            hwin1.DispObj(ho_img);
                            if (中心ToolStripMenuItem.Checked)
                                disp_win_cross(ho_img);
                            HSystem.SetSystem("flush_graphic", "false");
                           OnHMouseWheelEvent();
                        }
                        catch (HalconException)
                        {
                        }
                    }

                }

            }
        }

 

对不起,我之前的回答有误。hWindowControl控件是HALCON图像库中的控件,用于显示和处理图像,并不直接支持自适应缩放图片大小的功能。如果您需要在WinForms中实现自适应缩放图片大小的功能,可以使用PictureBox控件来显示图片并设置SizeMode属性为Zoom。以下是相应的代码示例: ```csharp private void AdjustImageSize() { // 获取PictureBox控件的尺寸 int controlWidth = pictureBox.Width; int controlHeight = pictureBox.Height; // 加载图片并调整大小 Image originalImage = Image.FromFile("image.jpg"); // 替换为您实际使用的图片路径 // 计算缩放比例 float scaleWidth = (float)controlWidth / originalImage.Width; float scaleHeight = (float)controlHeight / originalImage.Height; float scale = Math.Min(scaleWidth, scaleHeight); // 计算调整后的图片尺寸 int adjustedWidth = (int)(originalImage.Width * scale); int adjustedHeight = (int)(originalImage.Height * scale); // 创建调整后的图片 Image adjustedImage = new Bitmap(originalImage, adjustedWidth, adjustedHeight); // 在PictureBox控件中显示调整后的图片 pictureBox.Image = adjustedImage; pictureBox.SizeMode = PictureBoxSizeMode.Zoom; } ``` 在上述代码中,我们通过计算缩放比例,调整原始图片的大小,并创建调整后的图片对象。然后,将调整后的图片显示在PictureBox控件中,并将SizeMode属性设置为Zoom以实现自适应缩放图片大小的效果。 请确保已经在窗体设计器中添加了一个PictureBox控件,并将其命名为pictureBox。 希望这次能够解决您的问题。如果您有任何其他疑问,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值