vb.net 更改图片框中的不透明度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24817235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:52:01  来源:igfitidea点击:

Change opacity in picturebox

vb.netopacitypicturebox

提问by user3709879

I use this function

我用这个功能

Public Shared Function ChangeOpacity(ByVal img As Image, ByVal opacityvalue As Single) As Bitmap
Dim bmp As New Bitmap(img.Width, img.Height)
  Dim graphics__1 As Graphics = Graphics.FromImage(bmp)
  Dim colormatrix As New colormatrix
        colormatrix.Matrix33 = opacityvalue
        Dim imgAttribute As New ImageAttributes
        imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
        graphics__1.DrawImage(img, New Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, _
         GraphicsUnit.Pixel, imgAttribute)
        graphics__1.Dispose()
    Return bmp
End Function

I use this command

我用这个命令

picturebox.Image = ChangeOpacity(picturebox.Image, 0.3)

picturebox.Image = ChangeOpacity(picturebox.Image, 0.3)

This is work ,The picturebox's opacity is fade out. but when I try this

这是工作,图片框的不透明度淡出。但是当我尝试这个时

picturebox.Image = ChangeOpacity(picturebox.Image, 1.0)

picturebox.Image = ChangeOpacity(picturebox.Image, 1.0)

It is nothing happen , The picture is still fade out I want to return picture into default How to fix this ? Thank you very much

什么都没有发生,图片仍然淡出我想将图片恢复为默认值如何解决这个问题?非常感谢

采纳答案by Sathish

Load the image form file like this

像这样加载图像表单文件

PictureBox1.Image = Image.FromFile("C:\Documents and 
Settings\User3\Desktop\Sathish\image\calendar-icon-reportdate.png")

And use the call opacity like this

并像这样使用通话不透明度

 PictureBox1.Image.Dispose()
 PictureBox1.Image = ChangeOpacity(Image.FromFile("C:\Documents and
 Settings\User3\Desktop\Sathish\image\calendar-icon-reportdate.png"), 0.3)



 PictureBox1.Image.Dispose()
 PictureBox1.Image = ChangeOpacity(Image.FromFile("C:\Documents and
 Settings\User3\Desktop\Sathish\image\calendar-icon-reportdate.png"), 1)

回答by Ahmed

The second parameter in ChangeOpacity()function is the Opacity Percentage or factor of the picture in first parameter.

ChangeOpacity()函数中的第二个参数是第一个参数中图片的不透明度百分比或系数。

So (1.0) will not change the Opacity, it will return the picture without changes.

所以 (1.0) 不会改变不透明度,它会返回没有改变的图片。

You have to multiply the previous opacity value to have (1) and return the picture into default OR reload your image from the source with the default opacity to first parameter.

您必须乘以先前的不透明度值以获得 (1) 并将图片返回到默认值或从具有默认不透明度到第一个参数的源重新加载图像。

In your example The_Current_Opacity * Opacity_Factor = 1

在您的示例中 The_Current_Opacity * Opacity_Factor = 1

0.3 * Opacity_Factor = 1

0.3 * Opacity_Factor = 1

use: picturebox.Image = ChangeOpacity(picturebox.Image, 3.3)

用: picturebox.Image = ChangeOpacity(picturebox.Image, 3.3)

I prefer the division (1/0.3). It's more accurate than ( 3.3).

我更喜欢除法(1/0.3)。它比 更准确( 3.3)

Here is the code and you can do the math

这是代码,你可以做数学

dim OpacityPercentage as Single 
OpacityPercentage = 0.3

'To fade out the picturebox's opacity
picturebox1.Image = ChangeOpacity(picturebox1.Image, OpacityPercentage )

'To return picture into default
picturebox1.Image = ChangeOpacity(picturebox1.Image, 1/OpacityPercentage )