zoukankan      html  css  js  c++  java
  • CxImage实现9PNG

    CxImage* ScaleImageBy9PNG(CxImage *pRawImage, int nDstWidth,int nDstHeight)
    {
        if(NULL == pRawImage) return NULL;
    
        CDC *pDC = CDC::FromHandle(::GetDC(NULL));
        CDC memDC;
        memDC.CreateCompatibleDC(pDC);
    
        const UINT nBMPWidth = nDstWidth;
        const UINT nBMPHeight= nDstHeight;
        const UINT nRawWidth = pRawImage->GetWidth();
        const UINT nRawHeight = pRawImage->GetHeight();
    
        BITMAPINFO bitmapinfo;
        bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        bitmapinfo.bmiHeader.biBitCount = 32;
        bitmapinfo.bmiHeader.biHeight = nBMPHeight;
        bitmapinfo.bmiHeader.biWidth = nBMPWidth;
        bitmapinfo.bmiHeader.biPlanes = 1;
        bitmapinfo.bmiHeader.biCompression=BI_RGB;
        bitmapinfo.bmiHeader.biXPelsPerMeter=0;
        bitmapinfo.bmiHeader.biYPelsPerMeter=0;
        bitmapinfo.bmiHeader.biClrUsed=0;
        bitmapinfo.bmiHeader.biClrImportant=0;
        bitmapinfo.bmiHeader.biSizeImage = bitmapinfo.bmiHeader.biWidth * bitmapinfo.bmiHeader.biHeight * bitmapinfo.bmiHeader.biBitCount / 8;
        HBITMAP    hBitmap = ::CreateDIBSection (memDC.m_hDC,&bitmapinfo, 0,NULL, 0, 0);
        
    
        CxImage *newImage = new CxImage();
        newImage->CreateFromHBITMAP(hBitmap);
        if (hBitmap != NULL)
        {
            DeleteObject(hBitmap);
        }
        ::ReleaseDC(NULL, pDC->m_hDC);
        if(memDC.m_hDC)        ::DeleteDC(memDC.m_hDC);
    
        int newImageHeight = newImage->GetHeight();
        int newImageWidth = newImage->GetWidth();
        if(FALSE == newImage->AlphaIsValid())
        {
            newImage->AlphaCreate();
        }
        if(newImageHeight > nRawHeight && newImageWidth > nRawWidth)
        {
            for(int iH=0;iH<newImageHeight;++iH)
                for(int iW=0;iW<newImageWidth;++iW)
                {
                    int rawH = iH,rawW = iW;
                    if(rawH >= (newImageHeight-nRawHeight/2))
                    {
                        rawH = rawH - (newImageHeight - nRawHeight);
                    }
                    if(rawW >= (newImageWidth-nRawWidth/2))
                    {
                        rawW = rawW - (newImageWidth - nRawWidth);
                    }
    
                    if((iH < nRawHeight/2 || iH>=(newImageHeight-nRawHeight/2))&&
                        (iW < nRawWidth/2 || iW>= (newImageWidth-nRawWidth/2)))
                    {
                        //4块源
                        RGBQUAD argb = pRawImage->GetPixelColor(rawW,rawH,true);
                        newImage->SetPixelColor(iW,iH,RGB(argb.rgbRed,argb.rgbGreen,argb.rgbBlue));
                        newImage->AlphaSet(iW,iH,argb.rgbReserved);
                    }
                    else
                    {
                        if((iH < nRawHeight/2 || iH>=(newImageHeight-nRawHeight/2))&&iW-1>=0)
                        {
                            RGBQUAD argb = newImage->GetPixelColor(iW-1,iH,true);
                            newImage->SetPixelColor(iW,iH,RGB(argb.rgbRed,argb.rgbGreen,argb.rgbBlue));
                            newImage->AlphaSet(iW,iH,argb.rgbReserved);
                        }
                        else
                        {
                            if(iH - 1 >= 0)
                            {
                                RGBQUAD argb = newImage->GetPixelColor(iW,iH-1,true);
                                newImage->SetPixelColor(iW,iH,RGB(argb.rgbRed,argb.rgbGreen,argb.rgbBlue));
                                newImage->AlphaSet(iW,iH,argb.rgbReserved);
                            }
                        }
                    }
                }
        }
        return newImage;
    }

    9PNG的意思就是绘制时按9块区域绘制,左上左下右上右下是源,其他是拉伸的部分。CxImage主要使用像素点的方式来进行拉伸,并且每个像素都有copy透明信息。

  • 相关阅读:
    [转]P2P原理和常见实现方法
    google naming
    【转】CvArr、Mat、CvMat、IplImage、BYTE转换(总结而来)
    Ubuntu 下 JDK7.0和eclipse安装
    BSP模型简单介绍
    2012最受企业欢迎的开发技能Top10 转
    Social Network Analysis
    python操作mysql
    ASP、ASP.NET、JSP、PHP等网页服务器语言的比较
    mysql 正则表达式 regexp
  • 原文地址:https://www.cnblogs.com/jlyg/p/9809425.html
Copyright © 2011-2022 走看看