Alpha 函数使用

来源:互联网 发布:网络创业平台 编辑:程序博客网 时间:2024/06/06 04:40

 

为了研究图片半透明,找了很多alpha相关资料,没能实现自己的功能。先把这段代码留着,以后有时间再来看。

 

 这段代码好像是codeproject上的,有点忘记了,反正不是我写的,呵呵!

Public Class Alpha
    Private Declare Function AlphaBlend Lib "coredll.dll" _
    (ByVal hdcDest As IntPtr, ByVal xDest As Integer, ByVal yDest As Integer, ByVal cxDest As Integer, _
     ByVal cyDest As Integer, ByVal hdcSrc As IntPtr, ByVal xSrc As Integer, ByVal ySrc As Integer, _
     ByVal cxSrc As Integer, ByVal cySrc As Integer, ByVal bf As BlendFunction) As Integer

  <Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _
  Public Structure BlendFunction
    Dim BlendOp As Byte
    Dim BlendFlags As Byte
    Dim SourceConstantAlpha As Byte
    Dim AlphaFormat As Byte
  End Structure

  Public Enum BlendOperation As Byte
    AC_SRC_OVER = 0
  End Enum

  Public Enum BlendFlags As Byte
    Zero = 0
  End Enum

  Public Enum SourceConstantAlpha As Byte
    Transparent = &H0
    Opaque = &HFF
  End Enum

  Public Enum AlphaFormat As Byte
    AC_NONE = 0
    AC_SRC_ALPHA = 1
  End Enum

 

  Public Shared Sub Blend(ByVal dest As Bitmap, ByVal src As Bitmap, ByVal alpha As Byte)
    Using grDest As Graphics = Graphics.FromImage(dest)
      Using grSrc As Graphics = Graphics.FromImage(src)
        Dim hdcDest As IntPtr = grDest.GetHdc
        Dim hdcSrc As IntPtr = grSrc.GetHdc

        Dim bf As BlendFunction
        bf.AlphaFormat = AlphaFormat.AC_NONE
        bf.BlendFlags = BlendFlags.Zero
        bf.BlendOp = BlendOperation.AC_SRC_OVER
        bf.SourceConstantAlpha = alpha

        AlphaBlend(hdcDest, 0, 0, dest.Width, dest.Height, hdcSrc, 0, 0, src.Width, src.Height, bf)

        grSrc.ReleaseHdc(hdcSrc)
        grDest.ReleaseHdc(hdcDest)
      End Using
    End Using
  End Sub

End Class

原创粉丝点击