MFC 绘制透明的PNG图像

//头文件

#include "CvvImage.h"

//绘制透明的PNG图像
void CTestDlg::overlayImage(const Mat &background, const Mat &foreground,Mat &output, Point2i location)
{
    background.copyTo(output);

  // start at the row indicated by location, or at row 0 if location.y is negative.
    for(int y = std::max(location.y , 0); y < background.rows; ++y)
   {
        int fY = y - location.y; // because of the translation

        // we are done of we have processed all rows of the foreground image.
        if(fY >= foreground.rows)
            break;

        // start at the column indicated by location,
        // or at column 0 if location.x is negative.
        for(int x = std::max(location.x, 0); x < background.cols; ++x)
        {
            int fX = x - location.x; // because of the translation.

            // we are done with this row if the column is outside of the foreground image.
            if(fX >= foreground.cols)
                break;
            // determine the opacity of the foregrond pixel, using its fourth (alpha) channel.
            double opacity = ((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3]) / 255.;

            // and now combine the background and foreground pixel, using the opacity,
            // but only if opacity > 0.
            for(int c = 0; opacity > 0 && c < output.channels(); ++c)
            {
                unsigned char foregroundPx = foreground.data[fY * foreground.step + fX * foreground.channels() + c];
                unsigned char backgroundPx =background.data[y * background.step + x * background.channels() + c];
                output.data[y*output.step + output.channels()*x + c] =
                backgroundPx * (1.-opacity) + foregroundPx * opacity;
            }
        }
     }
}

void CTestDlg::OnPaint()
{
    CPaintDC dc(this); // 用于绘制的设备上下文
    if (IsIconic())
    {
        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // 使图标在工作区矩形中居中
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // 绘制图标
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
    if(m_bOpenCameraSuc)
    {
        CvvImage realVideoImg; //实时视频图像对象
        //input= m_frameColor.operator _IplImage();  //Opencv2.4.9版本下的转换
        CString ImgPath = "D:\\1.png";
        Mat bgImage = imread((LPCSTR)ImgPath,-1);//720*682的车牌图像
        Mat result;
        overlayImage(m_frameColor, bgImage, result, cv::Point(0,0));
    
        IplImage input = IplImage(result); //Opencv3.2版本以上的转换
        realVideoImg.CopyOf(&input); //复制帧图像
        CRect rect;
        GetDlgItem(IDC_VIDEO)->GetClientRect(&rect);
        CDC dcMem,bgMem;            //用于缓冲作图的内存DC
        CBitmap bmp,Bgbmp;          //内存中承载临时图象的位图
        dcMem.CreateCompatibleDC(&dc); 

        bmp.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height()); // 创建新的位图资源
        CBitmap *pOldBmp = dcMem.SelectObject(&bmp); 
        dcMem.FillSolidRect(rect,dc.GetBkColor());//按原来背景填充客户区,不然会是黑色
        ::SetStretchBltMode(dcMem.m_hDC,HALFTONE); //防止图片缩小失真
        ::SetBrushOrgEx(dcMem.m_hDC,0,0,NULL);   

        realVideoImg.DrawToHDC(dcMem.m_hDC, rect);
        dc.BitBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,SRCCOPY);//将内存DC上的图象拷贝到前台
        realVideoImg.Destroy(); //销毁视频图像对象
        dcMem.SelectObject(pOldBmp);
        dcMem.DeleteDC();      //删除
        bmp.DeleteObject();    //删除位图
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值