加载位图并按比例缩放显示

CBitmap bkBMP;  // BMP位图后台buffer

 

// 打开位图文件

void CGraphic::OnBnClickedBtnLoadbmp()
{
 // TODO: Add your control notification handler code here

 //--------------------------------------------------------------------
 // 打开BMP位图并显示出来
 //--------------------------------------------------------------------
 CFileDialog openDlg(true,"*.bmp",NULL,NULL,"*.bmp|*.bmp||"); // creat open BMP dialog
 if (openDlg.DoModal()==IDOK) // finish the photo choose
 {
  CString bmpName = openDlg.GetPathName(); // get BMP photo path

  // 通过LoadImage获取位图数据,转存人BMP后台buffer
  bkBMP.m_hObject = (HBITMAP)::LoadImage(NULL,bmpName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);  
 }
 else
  return;

 //--------------------------------------------------------------------
 // 将打开的BMP位图数据取出
 //--------------------------------------------------------------------
 CBitmap* m_bBmp; // all BMP photo information buffer
 m_bBmp = NULL;
 if (m_bBmp != NULL)
 {
  m_bBmp->DeleteObject();
  delete m_bBmp;
  m_bBmp = NULL;
 }
 m_bBmp = new CBitmap; // set RAM for m_pBmp

 LoadBitmapFile(openDlg.GetPathName(),*m_bBmp); // load all BMP photo information to buffer

 BITMAP infBmp; // 创建一个BMP结构体,存储BMP位图信息
 m_bBmp -> GetBitmap(&infBmp); // 这个结构体buffer接收打开的位图信息

 delete m_bBmp;

 // 将信息一一取出
 int bmType = infBmp.bmType;
 bmpCol = infBmp.bmWidth;
 bmpRow = infBmp.bmHeight;
 int widthBytes = infBmp.bmWidthBytes;
 int planes = infBmp.bmPlanes;
 int bitspixel = infBmp.bmBitsPixel;

 m_graDlg_staBMPInfm.Format("bmType=%d, bmWidth=%d, bmHeight=%d, bmWidthBytes=%d, bmPlanes=%d, bmBitsPixel=%d",
  bmType,bmpCol,bmpRow,widthBytes,planes,bitspixel);
 SetDlgItemText(IDC_STA_BMPINF,m_graDlg_staBMPInfm); // 显示信息


 Invalidate();       // 相应OnPaint消息

}

 

//  显示位图

void CGraphic::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 // TODO: Add your message handler code here 

CDC * pDC;
   pDC = GetDlgItem(IDC_BMP_DISPLAY)->GetDC();  // 获取picture控件DC

   CDC * bkDc = new CDC;  // 创建后台DC buffer
   bkDc -> CreateCompatibleDC(pDC); // 后台DC与picture控件DC相关联

   bkDc -> SelectObject(bkBMP); // 后台DC存入之前已导入的BMP位图

//   pDC -> BitBlt(0,0,bmpCol,bmpRow,bkDc,0,0,SRCCOPY);  // picture控件显示(这种显示方法图片不能缩放)
   pDC -> SetStretchBltMode(HALFTONE);  // 调整图片缩放颜色失真
   pDC -> StretchBlt(0,0,BMP_STRETCH_WIDTH,BMP_STRETCH_HEIGHT,bkDc,0,0,bmpCol,bmpRow,SRCCOPY);  // 将图片按比例缩放  

   delete bkDc; // 不加此句,更改窗口大小时会影响BMP位图显示

}

 

关于StretchBlt函数和BitBlt函数的用法 参考:http://blog.csdn.net/phenixyf/article/details/41827349

关于StretchBlt函数缩放后失真 参考:http://blog.csdn.net/phenixyf/article/details/41827715

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 OpenCV 中,可以通过 `convertScaleAbs` 函数将 8 位图像转换为 16 位图像。具体步骤如下: 1. 加载 8 位图像: ```python import cv2 img_8 = cv2.imread('path/to/8-bit/image.png', cv2.IMREAD_GRAYSCALE) ``` 2. 转换为 16 位图像: ```python img_16 = cv2.convertScaleAbs(img_8, alpha=(65535.0/255.0), beta=0) ``` 其中,`alpha` 参数为缩放系数,表示将 8 位图像的像素值缩放至 16 位图像的像素值范围(0~65535)。`beta` 参数为偏移量,表示在缩放后的像素值上加上一个常数。 转换后的 16 位图像 `img_16` 可以使用 OpenCV 的其他函数进行处理和显示。 ### 回答2: 将8位图转换为16位图可以使用OpenCV库中的函数cv::cvtcolor()。首先,我们需要加载8位图像,并创建一个16位图像的空白画布。然后使用cv::cvtColor()函数将8位图像转换为16位图像。 以下是一个示例代码: ```python import cv2 def convert_8bit_to_16bit_image(image_path): # 加载8位图像 img_8bit = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # 创建一个16位图像的空白画布 img_16bit = np.zeros_like(img_8bit, dtype=np.uint16) # 将8位图像转换为16位图像 cv2.cvtColor(img_8bit, cv2.COLOR_GRAY2BGR, img_16bit) return img_16bit image_path = "8bit_image.png" converted_image = convert_8bit_to_16bit_image(image_path) cv2.imwrite("16bit_image.png", converted_image) cv2.imshow("8-bit Image", cv2.imread(image_path)) cv2.imshow("16-bit Image", converted_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在这个例子中,我们首先加载一个名为"8bit_image.png"的8位图像。然后,我们创建一个与8位图像相同大小的空白16位图像画布。使用cv2.cvtColor()函数,我们将8位图像转换为16位图像。最后,将16位图像保存为"16bit_image.png"并显示在窗口中。 需要注意的是,在转换过程中,我们使用参数cv2.COLOR_GRAY2BGR来指定从灰度图像转换为16位BGR图像。你可以根据需要选择其他颜色空间。 ### 回答3: 要将8位图转换为16位图,可以使用OpenCV库中的函数cv2.convertScaleAbs()。 下面是转换过程的步骤: 1. 首先,使用cv2.imread()函数读取8位图像。请确保图像的路径正确,并且图像文件是8位灰度图。 2. 使用cv2.cvtColor()函数将8位图像转换为16位灰度图像。参数为输入图像和转换类型,这里是cv2.COLOR_GRAY2GRAY。 3. 然后,使用cv2.convertScaleAbs()函数将转换后的图像从8位转换为16位。该函数的参数为输入图像、输出图像和比例。 4. 最后,使用cv2.imshow()函数显示转换后的16位图像,并使用cv2.imwrite()函数将图像保存到指定路径上。 下面是一个示例代码: ```python import cv2 # 读取8位图像 img = cv2.imread("path_to_image.jpg", 0) # 将8位图像转换为16位 img_16bit = cv2.cvtColor(img, cv2.COLOR_GRAY2GRAY) # 将转换后的图像从8位转换为16位 img_16bit = cv2.convertScaleAbs(img_16bit, alpha=(65535 / 255)) # 显示16位图像 cv2.imshow("16-bit Image", img_16bit) cv2.waitKey(0) # 保存16位图像 cv2.imwrite("path_to_save_image.png", img_16bit) ``` 请注意,这只是一个示例,你需要替换"path_to_image.jpg"和"path_to_save_image.png"为你自己的图像路径和保存路径。 此外,你还可以根据自己的需求调整转换比例和其他参数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值