Halcon显示图片自适应窗口控件中心 缩放平移图像

12 篇文章 0 订阅
10 篇文章 0 订阅

图像自适应窗口控件显示,以窗口中心为基准

read_image (Image,'tooth_rim.png')

winWidth:=640
winHeight:=880
dev_resize_window_fit_size (0, 0, winWidth, winHeight, -1, -1)

get_image_size (Image, imgWidth, imgHeight)

ScaleWidth:=imgWidth/(winWidth*1.0)
ScaleHeight:=imgHeight/(winHeight*1.0)

if(ScaleWidth>=ScaleHeight)
    row1:= -(1.0) * ((winHeight * ScaleWidth) - imgHeight) / 2
    column1 := 0                    
    row2 := row1 + winHeight * ScaleWidth
    column2:= column1 + winWidth * ScaleWidth      
else
    row1:= 0
    column1 := -(1.0) * ((winWidth * ScaleHeight) - imgWidth) / 2                    
    row2 := row1 + winHeight * ScaleHeight
    column2:= column1 + winWidth * ScaleHeight
endif   
dev_clear_window ()
dev_set_part (row1, column1, row2, column2)
dev_display (Image)

                                                          

 

鼠标缩放平移图像

private void mouseWheel(object sender, HMouseEventArgs e)
{
    if (inMeasureLine)
    {
        return;
    }
    double Row, Column;
    int Button;
    try
    {
        viewPort.HalconWindow.GetMpositionSubPix(out Row, out Column, out Button);
    }
    catch (HalconException)
    {
        return;
    }

    double mode = 1;
    if (e.Delta > 0)
    {
        mode = 1;
    }
    else
    {
        mode = -1;
    }
    DispImageZoom(mode, Row, Column);
}


void DispImageZoom(double mode, double Mouse_row, double Mouse_col)
{
    try
    {
        viewPort.HalconWindow.GetPart(out current_beginRow, out current_beginCol, out current_endRow, out current_endCol);
    }
    catch
    {
        return;
    }

    if (mode > 0)   // 放大图像
    {
        zoom_beginRow = (int)(current_beginRow + (Mouse_row - current_beginRow) * 0.300d);
        zoom_beginCol = (int)(current_beginCol + (Mouse_col - current_beginCol) * 0.300d);
        zoom_endRow = (int)(current_endRow - (current_endRow - Mouse_row) * 0.300d);
        zoom_endCol = (int)(current_endCol - (current_endCol - Mouse_col) * 0.300d);
    }
    else            // 缩小图像
    {
        zoom_beginRow = (int)(Mouse_row - (Mouse_row - current_beginRow) / 0.700d);
        zoom_beginCol = (int)(Mouse_col - (Mouse_col - current_beginCol) / 0.700d);
        zoom_endRow = (int)(Mouse_row + (current_endRow - Mouse_row) / 0.700d);
        zoom_endCol = (int)(Mouse_col + (current_endCol - Mouse_col) / 0.700d);
    }

    try
    {
        int hw_width, hw_height;
        hw_width = viewPort.WindowSize.Width;
        hw_height = viewPort.WindowSize.Height;

        bool _isOutOfArea = true;
        bool _isOutOfSize = true;
        bool _isOutOfPixel = true;  //避免像素过大

        _isOutOfArea = zoom_beginRow >= imageHeight || zoom_endRow <= 0 || zoom_beginCol >= imageWidth || zoom_endCol < 0;
        _isOutOfSize = (zoom_endRow - zoom_beginRow) > imageHeight * 20 || (zoom_endCol - zoom_beginCol) > imageWidth * 20;
        _isOutOfPixel = hw_height / (zoom_endRow - zoom_beginRow) > 500 || hw_width / (zoom_endCol - zoom_beginCol) > 500;

        if (_isOutOfArea || _isOutOfSize || _isOutOfPixel)
        {
            return;
        }

        //viewPort.HalconWindow.ClearWindow();

        viewPort.HalconWindow.SetPaint(new HTuple("default"));
        //              保持图像显示比例
        viewPort.HalconWindow.SetPart(zoom_beginRow, zoom_beginCol, zoom_endRow, zoom_beginCol + (zoom_endRow - zoom_beginRow) * hw_width / hw_height);

        int w = (zoom_endRow - zoom_beginRow) * hw_width / hw_height;
        int w0 = current_endCol - current_beginCol;
        double scale = (double)w / w0;
        ZoomWndFactor *= scale;
        Repaint();

    }
    catch    //ex.Message;
    {
    //DispImageFit();
    }
}

/*******************************************************************/
//motionX = ((currX - startX)); //mouseDown获取startX、startY;mouseMove获取currX、currX
//motionY = ((currY - startY));
//mouseMove调用moveImage()
/*******************************************************************/
private void moveImage(double motionX, double motionY)
{
    viewPort.HalconWindow.GetPart(out current_beginRow, out current_beginCol, out  current_endRow, out current_endCol);

    ImgRow1 = current_beginRow - (int)motionY;
    ImgRow2 = current_endRow - (int)motionY;

    ImgCol1 = current_beginCol - (int)motionX;
    ImgCol2 = current_endCol - (int)motionX;

    viewPort.HalconWindow.SetPart((int)ImgRow1, (int)ImgCol1, (int)ImgRow2, (int)ImgCol2);
    Repaint();
}

 

  • 6
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值