AlphaBlend函数问题

水猿兵团五哥 2010-02-10 02:04:48
快过年了,都呆不住了吧,找个问题活跃下气氛,顺便散粉。
进入正题:我在学习用AlphaBlend函数来处理图片的透明度,查了很多资料,
(发现这个函数的资料真少)终于在语法上通过了,可是效果却没有出现。
上代码:

Public Class Alpha
Private Declare Function AlphaBlend Lib "msimg32.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 = &H0
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
Public Class Form1
Dim alphavalue As Integer

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Dim grDest As Bitmap = New Bitmap(My.Resources._9d54f6eff87b5adfb3fb9564)
Dim grSrc As Bitmap = New Bitmap(My.Resources._9f12060e34a540e97bcbe1ce)
alphavalue = TrackBar1.Value
Alpha.Blend(grDest, grSrc, alphavalue)
'Button1.Font = New Font(Button1.Font.Size, alphavalue)
PictureBox1.Image = grDest
PictureBox2.Image = grSrc
PictureBox1.Refresh()
PictureBox2.Refresh()
End Sub
End Class


...全文
313 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
水猿兵团五哥 2010-03-08
  • 打赏
  • 举报
回复
最近没时间了,慢慢研究吧,先结贴了,谢谢各位!
水猿兵团五哥 2010-03-04
  • 打赏
  • 举报
回复
feixuyue 2010-03-04
  • 打赏
  • 举报
回复
dim btp as new bitmap("text.jpg")
dim x,y as integer
for x=0 to btp.width-1
for y-0 to btp.height-1
btp.setpixel(x,y,color.fromargb(alpha,btp.getpixel(x,y))) 'alpha 设置透明值
next
next
feixuyue 2010-03-04
  • 打赏
  • 举报
回复
.net有实现.
下面是MSDN相关的代码,希望对楼主有帮助!
当图像位于背景的白色部分之上时,图像就与白色相混合。在图像与黑色线条的相交处,图像与黑色相混合。

Visual Basic 复制代码
' Create the Bitmap object and load it with the texture image.
Dim bitmap As New Bitmap("Texture.jpg")

' Initialize the color matrix.
' Note the value 0.8 in row 4, column 4.
Dim matrixItems As Single()() = { _
New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 0.8F, 0}, _
New Single() {0, 0, 0, 0, 1}}

Dim colorMatrix As New ColorMatrix(matrixItems)

' Create an ImageAttributes object and set its color matrix.
Dim imageAtt As New ImageAttributes()
imageAtt.SetColorMatrix( _
colorMatrix, _
ColorMatrixFlag.Default, _
ColorAdjustType.Bitmap)

' First draw a wide black line.
e.Graphics.DrawLine( _
New Pen(Color.Black, 25), _
New Point(10, 35), _
New Point(200, 35))

' Now draw the semitransparent bitmap image.
Dim iWidth As Integer = bitmap.Width
Dim iHeight As Integer = bitmap.Height

' Pass in the destination rectangle (2nd argument) and the x _
' coordinate (3rd argument), x coordinate (4th argument), width _
' (5th argument), and height (6th argument) of the source rectangle.
e.Graphics.DrawImage( _
bitmap, _
New Rectangle(30, 0, iWidth, iHeight), _
0.0F, _
0.0F, _
iWidth, _
iHeight, _
GraphicsUnit.Pixel, _
imageAtt)


a854468521 2010-02-16
  • 打赏
  • 举报
回复
春节顶贴+接分。
讨厌设置回复字数限制。
古今多少事 2010-02-15
  • 打赏
  • 举报
回复
很多人需要解决又没答案的问题……UP!
poul000000 2010-02-12
  • 打赏
  • 举报
回复
just support ...Foucing
水猿兵团五哥 2010-02-11
  • 打赏
  • 举报
回复
引用 9 楼 lost_painting 的回复:
狂晕,行,反正闲着没事,我帮你解决这个问题,你150分都归我了...

引用 7 楼 machong8183 的回复:正式回4楼,我测试了,没效果,连漆黑黑都不见了不知道什么意思


分没问题,只要你能解决问题,如果能加分我再加
oo渣渣oo 2010-02-11
  • 打赏
  • 举报
回复
呵呵,我是进来学习的……顺便专业性的飘过……
wzuomin 2010-02-11
  • 打赏
  • 举报
回复
引用 13 楼 modest 的回复:
http://www.codeproject.com/KB/GDI-plus/alphafx.aspx


How to implement Alpha blending

Mark
水猿兵团五哥 2010-02-11
  • 打赏
  • 举报
回复
感谢老魏,这个方法我正在研究,不过我想把这个方法用内存法来使用,类似下面的代码,琢磨中。。。。

Try
Dim Bmp As Bitmap = CType(Me.PictureBox1.Image, Bitmap)
Dim Height As Integer = Bmp.Height
Dim Width As Integer = Bmp.Width

Dim pxf As Imaging.PixelFormat = Imaging.PixelFormat.Format24bppRgb

Dim rect As Rectangle = New Rectangle(0, 0, Bmp.Width, Bmp.Height)

Dim newbmpData As Imaging.BitmapData = Bmp.LockBits(rect, _
Imaging.ImageLockMode.ReadWrite, pxf)
Dim numBytes As Integer = newbmpData.Stride * Bmp.Height
Dim newBytes(numBytes) As Byte
Dim ptr As IntPtr = newbmpData.Scan0
' Copy the RGB values into the array.
Marshal.Copy(ptr, newBytes, 0, newBytes.Length)
Dim index As Integer = 0
For x As Integer = 0 To newbmpData.Height - 1
index = x * newbmpData.Stride
For y As Integer = 0 To newbmpData.Width - 1
Dim Resultcolor As Byte = 0
'加权平均值法
Resultcolor = CType(0.587 * newBytes(index + 1), Byte) + _
CType(0.114 * newBytes(index + 1), Byte) + CType(0.299 * newBytes(index), Byte)
newBytes(index) = Resultcolor
newBytes(index + 1) = Resultcolor
newBytes(index + 2) = Resultcolor
index = index + 3
Next
Next

Marshal.Copy(newBytes, 0, newbmpData.Scan0, newBytes.Length)

Bmp.UnlockBits(newbmpData)

Me.PictureBox1.Image = Bmp
鸭梨山大帝 2010-02-10
  • 打赏
  • 举报
回复
晕,我是WIN7 X64,拿楼主的代码连变化都看不到.
看来应该是API声明或者调用的时候有问题
鸭梨山大帝 2010-02-10
  • 打赏
  • 举报
回复
狂晕,行,反正闲着没事,我帮你解决这个问题,你150分都归我了...

引用 7 楼 machong8183 的回复:
正式回4楼,我测试了,没效果,连漆黑黑都不见了
不知道什么意思
水猿兵团五哥 2010-02-10
  • 打赏
  • 举报
回复
看来过年了真的没多少人在上班了?
水猿兵团五哥 2010-02-10
  • 打赏
  • 举报
回复
正式回4楼,我测试了,没效果,连漆黑黑都不见了
不知道什么意思
水猿兵团五哥 2010-02-10
  • 打赏
  • 举报
回复
引用 4 楼 lost_painting 的回复:
VB6 API声明:
VB code
DeclareFunction AlphaBlend Lib"msimg32.dll" ( _
ByVal hdcDestAsLong, _
ByVal xDestAsLong, _
ByVal yDestAsLong, _
ByVal WidthDestAsLong, _
ByVal HeightDestAsLong, _
ByVal hdcSrcAsLong, _
ByVal xSrcAsLong, _
ByVal ySrcAsLong, _
ByVal WidthSrcAsLong, _
ByVal HeightSrcAsLong, _
ByRef BlendfuncAsAs BLENDFUNCTION'ByRef here)AsLong
将 Byval 修改为 ByRef, VB6中的long在VB.NET中用Integer替代


骗我分哦,明显没看我的内容,气!!!
鸭梨山大帝 2010-02-10
  • 打赏
  • 举报
回复
AlphaBlend:
http://wenwen.soso.com/z/q169212230.htm

AlphaBlend参数BLENDFUNCTION :
http://blog.csdn.net/Vonger/archive/2009/08/24/4476654.aspx

鸭梨山大帝 2010-02-10
  • 打赏
  • 举报
回复
VB6 API声明:

Declare Function AlphaBlend Lib "msimg32.dll" ( _
ByVal hdcDest As Long, _
ByVal xDest As Long, _
ByVal yDest As Long, _
ByVal WidthDest As Long, _
ByVal HeightDest As Long, _
ByVal hdcSrc As Long, _
ByVal xSrc As Long, _
ByVal ySrc As Long, _
ByVal WidthSrc As Long, _
ByVal HeightSrc As Long, _
ByRef Blendfunc As As BLENDFUNCTION 'ByRef here
) As Long

将 Byval 修改为 ByRef, VB6中的long在VB.NET中用Integer替代
加载更多回复(3)

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧