16,554
社区成员
发帖
与我相关
我的任务
分享
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
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
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