无法更改图像不透明度

时间:2011-02-10 11:24:39

标签: vb.net

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

当我点击按钮时没有任何反应。我在这里做错了什么?

2 个答案:

答案 0 :(得分:3)

我认为颜色矩阵值的范围从01,因此您应该使用0.5代替50

答案 1 :(得分:2)

您可能希望将bmpPic.Image设置为函数返回的值。

这样的东西
Dim a As Image = bmpPic.Image         
bmpPic.Image = SetImgOpacity(a, 50)