I am trying to use the WM5 AlphaBlend() function to add transparency to an image. I found this link that contains a great example in C#.

When I download and run the C# example it works fine. I converted it to VB , but now the image does not display. I don't get any errors returned, but nothing is drawn.

Here is my code. Any help would be appreciated.

Imports System.Reflection

Public Class Form4

'Dim constantAlphaImage As Bitmap

Dim constantAlphaImage As Image

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

' Load the image to use with the AlphaBlend API.

Dim path As String = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)

constantAlphaImage = New Bitmap(path + "\blendme.bmp")

' AlphaBlend takes two HDC's - one source and one destination. Here's the source.

Using gxSrc As Graphics = Graphics.FromImage(constantAlphaImage)

Dim hdcDst As IntPtr = e.Graphics.GetHdc()

Dim hdcSrc As IntPtr = gxSrc.GetHdc()

Dim blendFunction As New BlendFunction()

blendFunction.BlendOp = CByte(BlendOperation.AC_SRC_OVER)

' Only supported blend operation

blendFunction.BlendFlags = CByte(BlendFlags.Zero)

' Documentation says put 0 here

blendFunction.SourceConstantAlpha = CByte(128)

' Constant alpha factor

blendFunction.AlphaFormat = CByte(0)

' Don't look for per pixel alpha

PlatformAPIs.AlphaBlend(hdcDst, Left, Top, Width, Height, hdcSrc, _

0, 0, Width, Height, blendFunction)


' Required cleanup to GetHdc()

' Required cleanup to GetHdc()


End Using

End Sub

End Class

Imports System

Imports System.Collections.Generic

Imports System.Text

Imports System.Runtime.InteropServices

Public Structure BlendFunction

Public BlendOp As Byte

Public BlendFlags As Byte

Public SourceConstantAlpha As Byte

Public AlphaFormat As Byte

End Structure

Public Enum BlendOperation As Byte


End Enum

Public Enum BlendFlags As Byte

Zero = 0

End Enum

Public Enum SourceConstantAlpha As Byte

Transparent = 0

Opaque = 255

End Enum

Public Enum AlphaFormat As Byte


End Enum

Public Class PlatformAPIs

<DllImport("coredll.dll")> _

Public Shared Function AlphaBlend(ByVal hdcDest As IntPtr, ByVal xDest As Int32, ByVal yDest As Int32, ByVal cxDest As Int32, ByVal cyDest As Int32, ByVal hdcSrc As IntPtr, _

ByVal xSrc As Int32, ByVal ySrc As Int32, ByVal cxSrc As Int32, ByVal cySrc As Int32, ByVal blendFunction As BlendFunction) As Int32

End Function

End Class

Re: Smart Devices General VB.NET WM 5 Alpha Blend function won't draw

Guang-Ming Bian - MSFT

hi smaggi

I have tried the c# code , it works fine. your vb code seem fine too. I am just confused about your code:

Code Snippet

PlatformAPIs.AlphaBlend(hdcDst, Left, Top, Width, Height, hdcSrc, _

0, 0, Width, Height, blendFunction)

You don't set value to Left,Top,Width,Height.

I hope it helpful to you.