我觉得关于这个问题没有必要用算法。如果你不要用算法也可以用的 话,我到是可以提供给你一个方法。
把两个图片的TRANSPARENT设为TRUE;
然后让两个图片的左上角的坐标一致,也就是LEFT和TOP坐标一样。
然后用COPYRECT拷贝那一个区域就可以了。

解决方案 »

  1.   

    我觉得要透明,要图像知道哪个地方该透明才行
    COPYRECT可以吗?
      

  2.   

    Tbitmap有关的属性:
      1.TansparentColor: 透明色;
      2.PixelFormat:指明每一象点颜色所占的二进位数;
                    pf16bit:   点2字节;
                    pf24bit:   3字节;
                    pf32bit:   4字节;
      3.ScanLine[行号];每一行象素缓冲的首址,即数组
                 的首址,每一象点占连续字节;有了上面的3个属性,就可以写出合成2幅图象的算法;   算法:
            for 第0行 到 最后一行
                for 第0列 TO 最后一列
                   begin
                        1.取第一幅一个象点c1;
                        2.取第二幅一个象点c2;
                        3.如果c1是透明色,c3:=c2;
                          如果c2是透明色,c3:=c1;
                        4.送c3
                   end
      
      

  3.   

    我觉得大家是没有完全明白我的意思,我的意思是:
    通过算法设置出象photoshop里一幅图象在另一幅图象上如何透明地显示,
    并且可以设置透明度,如50%的透明度。并不是单幅图象的透明性。
    关键在于象BCB提示的begin语句中第3段如何实现?
    请各位高手指点,谢谢。
      

  4.   

    我有个效率不高(速度很慢),但效果绝对好的算法:
    for 第0行 到 最后一行
        for 第0列 TO 最后一列
          begin
                1.取第一幅一个像点c1的R色 c1R;
                2.取第二幅一个像点c2的R色 c2R;
                3.新像点的R色 cR := (c1R*K+c2R*(1-K));            4.取第一幅一个像点c1的G色 c1G;
                5.取第二幅一个像点c2的G色 c2G;
                6.新像点的G色 cG := (c1G*K+c2G*(1-K));            7.取第一幅一个像点c1的B色 c1B;
                8.取第二幅一个像点c2的B色 c2B;
                9.新像点的B色 cB := (c1B*K+c2B*(1-K));           10.新像点的RGB色 cRGB := RGB(cR,cG,cB);      end
      其中 K 是2个图合并时,第一幅图的全重。改变这个 K 值可以得到一个渐变的效果。
      
      

  5.   

    akuan, 你试过了吗?效果对不对?
    我也希望有更快的算法(我的程序中也有个地方要用这个,现在只好换成另一个效果了),我想,不同算法的结果应该是一样的(类似我给出的算法的结果。呵呵)。
    Windows2000中有个API函数是修改窗体的透明度,好象叫什么α效果(记不太清楚了),速度比较快,PhotoShop中的透明度调整效果也很快。这应该是个标准的图象算法。
      

  6.   

    我的竹叶上有一个VB的范例,你可以简单的转换到Delphi
    http://www.applevb.com/sourcecode/halftran.zip
      

  7.   

    可将下面代码转换为DELPHI代码就可:          CDC               memDC;
    CBitmap &cBitmap=m_bmpDraw;
    CBitmap* pOldMemBmp = NULL;
    COLORREF col,colMask;
    CRect cRect;
    int x, y;
    CRgn wndRgn, rgnTemp; GetWindowRect(&cRect);
    CPoint ptOrg=cRect.TopLeft(); BITMAP bmInfo;
    cBitmap.GetObject(sizeof(bmInfo),&bmInfo);
    CRect rcNewWnd=CRect(ptOrg,CSize(bmInfo.bmWidth,bmInfo.bmHeight)); memDC.CreateCompatibleDC(pDC);
    pOldMemBmp = memDC.SelectObject(&cBitmap);
    colMask=memDC.GetPixel(0,0); wndRgn.CreateRectRgn(0, 0, rcNewWnd.Width(), rcNewWnd.Height());
    for(x=0; x<=rcNewWnd.Width(); x++)
    {
    for(y=0; y<=rcNewWnd.Height(); y++)
    {
    col = memDC.GetPixel(x, y);
    if(col == colMask)
    {
    rgnTemp.CreateRectRgn(x, y, x+1, y+1);
    wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);
    rgnTemp.DeleteObject();
    }
    }
    }
    if (pOldMemBmp) memDC.SelectObject(pOldMemBmp);
    SetWindowRgn((HRGN)wndRgn, TRUE);
    MoveWindow(rcNewWnd);
      

  8.   

    呵呵!看来这些人没明白你的意思呀!我曾经研究过这个问题,不过我是用VC做的,不知道用DELPHI怎么做,不过我可以把算法告诉你:取得两幅为图在每一点的像素值,分离出R,G,B的值,生成位图相应像素的R,G,B由下面公式决定:R=kR1+(1-k)R2   G=kG1+(1-k)G2  B=kB1+(1-k)B2  k为透明度,范围是0%-100% ,R1,R2,G1,G2,B1,B2为混合的两幅位图分离出来的R,G,B值,R,G,B为混合后的相应像素的R,G,B值。
      

  9.   

    使用DDraw吧,这是一个简单的Alpha特效啊。
    nononono的方法是可行的,但是使用了太多的乘法,还要用到浮点,在16位位图游戏编程中,大家通常使用这样的方法来进行Alpha混合:
    1先分色并移位
    2进行混色:(混色深度为32,足够了)
       DestColor= (RscColor-DestColor)*Alpha_Depth〉〉5+DestColor;(R.G.B分别计算)
    3最后合色
    欢迎到我的业余游戏制作主页看看,上面有相关的文章:http://calfsoft.51.net
    我的Email:[email protected]
      

  10.   

    如果你不需要设置透明度的话,最简单的多效果合成方法是Bitblt,设置不同的CopyMode 就可以实现不同的效果.
    当然如果你特别需要实现特定透明度的话,我认为LingWeiTao and APIer的方式是最好的了.
      

  11.   

    akuan虾,找到好的办法之后别忘了给我EMail一份
    [email protected]
      

  12.   

    我不知道你要这样做什么? 做静态的用WIN GDI 接口应该可以了.得到像素值,分离出RGB. 用两幅图的RGB 值做运算, 速度慢但相当灵活.
    如果你要做动画,  速度重要.当然要利用硬件加速了. 通用的MS 平台当然是DIRECT X了.算法一样,但速度不凡. & :-)
      

  13.   

    用MMX汇编优化,可以有100帧左右的速度,当然图片不能太大。
      

  14.   

    akuan,这样试试:分离RGB3色用“位与”、“移位”运算的方法;透明的比例按 32、16分级。
    如:如果是按32级,A图的权重 K = n/32,则B图的权重 = (32-n)/32,
    可以得到这样的算法:       cR := (c1R*n+c2R*(32-n));
           再对cR右移4位。
           
    这样的算法要快很多。
      
      

  15.   

    文章:
    http://www.applevb.com/art/alphablend.txt
    介绍利用AlphaBlend函数实现的透明混合
      

  16.   

    大家太那个了吧?
    这个问题其实很简单:如果你是在Windows98以上系统下工作,那么可以用
    Windows GDI的一个函数实现:AlphaBlend,函数说明就在下面,(请参考最新的MSDN)==========================================================================
      Platform SDK: Windows GDI 
    AlphaBlend
    The AlphaBlend function displays bitmaps that have transparent or semitransparent pixels.BOOL AlphaBlend(
      HDC hdcDest,                 // handle to destination DC
      int nXOriginDest,            // x-coord of upper-left corner
      int nYOriginDest,            // y-coord of upper-left corner
      int nWidthDest,              // destination width
      int nHeightDest,             // destination height
      HDC hdcSrc,                  // handle to source DC
      int nXOriginSrc,             // x-coord of upper-left corner
      int nYOriginSrc,             // y-coord of upper-left corner
      int nWidthSrc,               // source width
      int nHeightSrc,              // source height
      BLENDFUNCTION blendFunction  // alpha-blending function
    );
    Parameter
    hdcDest 
    [in] Handle to the destination device context. 
    nXOriginDest 
    [in] Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle. 
    nYOriginDest 
    [in] Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle. 
    nWidthDest 
    [in] Specifies the width, in logical units, of the destination rectangle. 
    nHeightDest 
    [in] Specifies the height, in logical units, of the destination rectangle. 
    hdcSrc 
    [in] Handle to the source device context. 
    nXOriginSrc 
    [in] Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle. 
    nYOriginSrc 
    [in] Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle. 
    nWidthSrc 
    [in] Specifies the width, in logical units, of the source rectangle. 
    nHeightSrc 
    [in] Specifies the height, in logical units, of the source rectangle. 
    blendFunction 
    [in] Specifies the alpha-blending function for source and destination bitmaps, a global alpha value to be applied to the entire source bitmap, and format information for the source bitmap. The source and destination blend functions are currently limited to AC_SRC_OVER. See the BLENDFUNCTION and EMRALPHABLEND structures. 
    Return Values
    If the function succeeds, the return value is TRUE.If the function fails, the return value is FALSE. Windows NT/ 2000: To get extended error information, call GetLastError. This can return the following value.Value Meaning 
    ERROR_INVALID_PARAMETER One or more of the input parameters is invalid. 
    Res
    If the source rectangle and destination rectangle are not the same size, the source bitmap is stretched to match the destination rectangle. If the SetStretchBltMode function is used, the iStretchMode value is automatically converted to COLORONCOLOR for this function (that is, BLACKONWHITE, WHITEONBLACK, and HALFTONE are changed to COLORONCOLOR). The destination coordinates are transformed by using the transformation currently specified for the destination device context. The source coordinates are transformed by using the transformation currently specified for the source device context. An error occurs (and the function returns FALSE) if the source device context identifies an enhanced metafile device context. If destination and source bitmaps do not have the same color format, AlphaBlend converts the source bitmap to match the destination bitmap. AlphaBlend does not support mirroring. If either the width or height of the source or destination is negative, this call will fail. If the source and destination are the same surface—that is, they are both the screen or the same memory bitmap—and the source and destination rectangles overlap, an error occurs and the function returns FALSE. The source rectangle must lie completely within the source surface, otherwise an error occurs and the function returns FALSE.AlphaBlend fails if the width or height of the source or destination is negative.Note  The SourceConstantaAlpha member of BLENDFUNCTION specifies an alpha transparancy value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values. If SourceConstantAlpha is 0, it is assumed that the image is transparent. Set the SourceConstantAlpha value to 255 (which indicates that the image is opaque) when you only want to use per-pixel alpha values. Requirements 
      Windows NT/2000: Requires Windows 2000.
      Windows 95/98: Requires Windows 98.
      Header: Declared in Wingdi.h; include Windows.h.
      Library: Included as a resource in Msimg32.dll.See Also
    Bitmaps Overview, Bitmap Functions Built on Thursday, May 11, 2000Requirements 
      Windows NT/2000: Requires Windows 2000.
      Windows 95/98: Requires Windows 98.
      Header: Declared in Wingdi.h; include Windows.h.
      Library: Included as a resource in Msimg32.dll.
    See Also
    Bitmaps Overview, Bitmap Functions 
    ====================================================================
      
      Platform SDK: Windows GDI 
    BLENDFUNCTION
    The BLENDFUNCTION structure controls blending by specifying the blending functions for source and destination bitmaps. typedef struct _BLENDFUNCTION {
      BYTE     BlendOp;
      BYTE     BlendFlags;
      BYTE     SourceConstantAlpha;
      BYTE     AlphaFormat;
    }BLENDFUNCTION, *PBLENDFUNCTION, *LPBLENDFUNCTION;
    Members 
    BlendOp 
    Specifies the source blend operation. Currently, the only source and destination blend operation that has been defined is AC_SRC_OVER. For details, see the following Res section. 
    BlendFlags 
    Must be zero. 
    SourceConstantAlpha 
    Specifies an alpha transparency value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values in the source bitmap. If you set SourceConstantAlpha to 0, it is assumed that your image is transparent. Set the SourceConstantAlpha value to 255 (opaque) when you only want to use per-pixel alpha values. 
    AlphaFormat 
    This member controls the way the source and destination bitmaps are interpreted. AlphaFormat has the following value. Value Meaning 
    AC_SRC_ALPHA  This flag is set when the bitmap has an Alpha channel (that is, per-pixel alpha). Note that the APIs use premultiplied alpha, which means that the red, green and blue channel values in the bitmap must be premultiplied with the alpha channel value. For example, if the alpha channel value is x, the red, green and blue channels must be multiplied by x and divided by 0xff prior to the call. 
    Res
    When the AlphaFormat parameter is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, the AlphaBlend function will fail. When the BlendOp parameter is AC_SRC_OVER , the source bitmap is placed over the destination bitmap based on the alpha values of the source pixels. If the source bitmap has no per-pixel alpha value, the blend is based on the SourceConstantAlpha value, as shown in the following table.Dst.Red  
     = Src.Red
     * SourceConstantAlpha + (1 - SourceConstantAlpha) * Dst.Red
     
    Dst.Green  
     = Src.Green  
     * SourceConstantAlpha + (1 - SourceConstantAlpha) * Dst.Green
     
    Dst.Blue  
     = Src.Blue
     * SourceConstantAlpha + (1 - SourceConstantAlpha) * Dst.Blue
     
    If the source bitmap has per-pixel alpha and the SourceConstantAlpha is not used (that is, it equals 0xff), the blend is based on the per-pixel alpha, as shown in the following table.Dst.Red
     = Src.Red
     + (1 - Src.Alpha) * Dst.Red
     
    Dst.Green  
     = Src.Green  
     + (1 - Src.Alpha) * Dst.Green
     
    Dst.Blue
     = Src.Blue
     + (1 - Src.Alpha) * Dst.Blue
     
    If the destination bitmap has an alpha channel, then: Dest.alpha  
     = Src.Alpha  
     + (1 - SrcAlpha) * Dst.Alpha
     
    If the source has per-pixel alpha and the SourceConstantAlpha is used (that is, it is not 0xff), the source is pre-multiplied by the SourceConstantAlpha and then the blend is based on the per-pixel alpha. The following tables show this.Src.Red
     = Src.Red 
     * SourceConstantAlpha; 
     
    Src.Green
     = Src.Green
     * SourceConstantAlpha; 
     
    Src.Blue
     = Src.Blue 
     * SourceConstantAlpha; 
     
    Src.Alpha
     = Src.Alpha
     * SourceConstantAlpha; 
     Dst.Red  
     = Src.Red 
     + (1 - Src.Alpha) * Dst.Red 
     
    Dst.Green 
     = Src.Green
     + (1 - Src.Alpha) * Dst.Green 
     
    Dst.Blue 
     = Src.Blue 
     + (1 - Src.Alpha) * Dst.Blue 
     
    Dst.Alpha
     =  Src.Alpha
     + (1 - Src.Alpha) * Dst.Alpha
     
    Requirements 
      Windows NT/2000: Requires Windows 2000.
      Windows 95/98: Requires Windows 98.
      Header: Declared in Wingdi.h; include Windows.h.See Also
    Bitmaps Overview, Bitmap Structures, AlphaBlend Built on Thursday, May 11, 2000Requirements 
      Windows NT/2000: Requires Windows 2000.
      Windows 95/98: Requires Windows 98.
      Header: Declared in Wingdi.h; include Windows.h.
    See Also
    Bitmaps Overview, Bitmap Structures, AlphaBlend 
      

  17.   

    有个AlphaBlend函数,(win98/NT)查查MSDN就知道了.
      

  18.   

    开玩笑,有这么麻烦吗?????用幕布的copy属性就成了
      

  19.   

    to: tms2000(海天) 那么,请你用copy作个例子看看能不能实现Alpha混全的半透明效果??
      

  20.   

    AlphaBlend函数最简单,就象bitblt一样的用.windows API,不错.我以前没发现,还自己写,现在就.....呵呵
      

  21.   

    隔一个点画一个点就可以,在哪儿都行,dos下也行, 不过你们讲的方法很好!
      

  22.   

    隔一个点画一个点很快,但是效果比较差,(尤其是挑剔的场合,codeguru上有一篇文章讲到这种做法(Alpaha),你可以去看看)