Imports System.Drawing.Imaging
Public Class Form1
Public Shared Function SetImgOpacity(ByVal imgPic As Image, ByVal imgOpac As Single) As Image
Dim bmpPic As New Bitmap(imgPic.Width, imgPic.Height)
Dim gfxPic As Graphics = Graphics.FromImage(bmpPic)
Dim cmxPic As New ColorMatrix()
Dim iaPic As New ImageAttributes()
cmxPic.Matrix33 = imgOpac
iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
gfxPic.DrawImage(imgPic, New Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic)
gfxPic.Dispose()
iaPic.Dispose()
Return bmpPic
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Image = bmpPic.Image
SetImgOpacity(a, 50)
End Sub
End Class
当我点击按钮时没有任何反应。我在这里做错了什么?
答案 0 :(得分:3)
我认为颜色矩阵值的范围从0
到1
,因此您应该使用0.5
代替50
。
答案 1 :(得分:2)
您可能希望将bmpPic.Image
设置为函数返回的值。
像
这样的东西Dim a As Image = bmpPic.Image
bmpPic.Image = SetImgOpacity(a, 50)