图像自适应窗口控件显示,以窗口中心为基准
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();
}