2011-02-10 92 views
2
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

您可能希望将标题更改为更具体的问题,网站上的每个问题都可能出现在您当前的标题下 – 2011-02-10 11:28:31

+0

您预期会发生什么? bmpPic.Image是一个实际的,实例化的对象吗? – 2011-02-10 11:30:22

回答

2

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

喜欢的东西

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

我相信,彩色矩阵值的范围从01,所以你应该使用0.5而不是50