开发者

Having trouble in changing the image opacity

开发者 https://www.devze.com 2023-02-09 22:18 出处:网络
Imports System.Drawing.Imaging Public Class Form1 Public Shared Function SetImgOpacity(ByVal imgPic As Image, ByVal imgOpac As Single) As Image
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

When I click the button nothing happens. What am I doing wrong here ?


I believe that the color matrix values range from 0 to 1, so you should probably use 0.5 instead of 50.


You might want to set the bmpPic.Image to the value returned from your function.

Something like

Dim a As Image = bmpPic.Image         
bmpPic.Image = SetImgOpacity(a, 50) 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号