does anyone know how to fade in and fade out a button in vb? i know how to do it with a form but not with a button.
Please help.
fade in components in visual basic
Page 1 of 111 Replies - 1552 Views - Last Post: 11 February 2012 - 09:58 PM
Replies To: fade in components in visual basic
#2
Re: fade in components in visual basic
Posted 11 February 2012 - 05:47 AM
button doesn't have opacity property. But you can create button from picture box. Then you can set image opacity.
look in:
look in:
System.Drawing.Imaging System.Drawing.Graphics
#3
Re: fade in components in visual basic
Posted 11 February 2012 - 06:20 AM
#4
Re: fade in components in visual basic
Posted 11 February 2012 - 07:06 AM
i will show you how to set image opacity. Then you can make fade in/out with loop.
First must create new graphic object from image.
http://msdn.microsof...y/5y289054.aspx
Then create new image attributes.
http://msdn.microsof...attributes.aspx
After creating new image attributes ,you will find method SetColorMatrix
http://msdn.microsof...y/c78zcz45.aspx
to set color matrix you must create new color matrix. Matrix33 property of new color matrix is what you looking for.
after that use graphics to draw new image from your image with this attributes.
If you stuck somewhere just ask.
First must create new graphic object from image.
http://msdn.microsof...y/5y289054.aspx
Then create new image attributes.
http://msdn.microsof...attributes.aspx
After creating new image attributes ,you will find method SetColorMatrix
http://msdn.microsof...y/c78zcz45.aspx
to set color matrix you must create new color matrix. Matrix33 property of new color matrix is what you looking for.
Dim cmxPic As New ColorMatrix() cmxPic.Matrix33 = imgOpac '(as double) your image opacity setting (min=0, max=1) imgAtt.SetColorMatrix(cmxPic, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
after that use graphics to draw new image from your image with this attributes.
If you stuck somewhere just ask.
#5
Re: fade in components in visual basic
Posted 11 February 2012 - 07:45 AM
its a little bit confusing. This is how it looks like. You can put your button picture in your resources. Then change image opacity with this function.
example:
Public Function SetImageOpacity(ByVal imgPic As Image, ByVal imgOpac As Double) As Image
Dim bmpPic As New Bitmap(imgPic.Width, imgPic.Height)
Dim grPic As Graphics = Graphics.FromImage(bmpPic)
Dim imgAtt As New ImageAttributes()
Dim cmxPic As New ColorMatrix()
cmxPic.Matrix33 = imgOpac
imgAtt.SetColorMatrix(cmxPic, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
grPic.DrawImage(imgPic, New Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, imgAtt)
grPic.Dispose()
imgAtt.Dispose()
Return bmpPic
End Function
example:
PictureBox1.image=SetImageOpacity(my.resources.buttonPic,0.5)
#6
Re: fade in components in visual basic
Posted 11 February 2012 - 08:12 AM
sela007, on 11 February 2012 - 07:45 AM, said:
its a little bit confusing. This is how it looks like. You can put your button picture in your resources. Then change image opacity with this function.
example:
Public Function SetImageOpacity(ByVal imgPic As Image, ByVal imgOpac As Double) As Image
Dim bmpPic As New Bitmap(imgPic.Width, imgPic.Height)
Dim grPic As Graphics = Graphics.FromImage(bmpPic)
Dim imgAtt As New ImageAttributes()
Dim cmxPic As New ColorMatrix()
cmxPic.Matrix33 = imgOpac
imgAtt.SetColorMatrix(cmxPic, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
grPic.DrawImage(imgPic, New Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, imgAtt)
grPic.Dispose()
imgAtt.Dispose()
Return bmpPic
End Function
example:
PictureBox1.image=SetImageOpacity(my.resources.buttonPic,0.5)
How do i make the opacity increase gradually with a loop? and i need the image in the picturebox to fade in,in 1 sec and when fading out it should fade out in 1 sec.should i use a timer? and what should the loop look like?
I am very confused.Isnt there any video tutorials i could watch to learn how to do this?
This post has been edited by Bluezap: 11 February 2012 - 08:14 AM
#7
Re: fade in components in visual basic
Posted 11 February 2012 - 08:36 AM
What's wrong with experimentation? Better learning than - please spoon feed me even if it a video.
#8
Re: fade in components in visual basic
Posted 11 February 2012 - 08:54 AM
#9
Re: fade in components in visual basic
Posted 11 February 2012 - 10:07 AM
If you have been experimenting show us. This forum's concept is for you to try to learn it yourself and come here with problems in code or concepts. sela007 did a great job of supplying some good links and an example.
As far as the loop - I think a simple Do/Loop Until <condition> would work fine, but it will be blisteringly fast.
As far as the loop - I think a simple Do/Loop Until <condition> would work fine, but it will be blisteringly fast.
#10
Re: fade in components in visual basic
Posted 11 February 2012 - 11:59 AM
where is the problem? you don't know how to use timer? or you don't know how to implement function? or something else?
My advice is to use timer. First of all you must create variable inside form class which will represent current picture opacity. Assume you want to fade picture from invisible to 100% visible, then first set current picture opacity to 0. If you want to picture fade in in 1 second, that means you must increase picture opacity by 0.01 every 10 milliseconds. That means you must set timer interval=10.
My advice is to use timer. First of all you must create variable inside form class which will represent current picture opacity. Assume you want to fade picture from invisible to 100% visible, then first set current picture opacity to 0. If you want to picture fade in in 1 second, that means you must increase picture opacity by 0.01 every 10 milliseconds. That means you must set timer interval=10.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick ' increase every 10 milliseconds CurrentPictureOpacity+=0.01 ' note: check if CurrentPictureOpacity is less or equal 1. If not then stop timer or else you will get error. PictureBox1.Image = SetImageOpacity(samplePicture,CurentPictureOpacity) End Sub
#11
Re: fade in components in visual basic
Posted 11 February 2012 - 09:10 PM
sela007, on 11 February 2012 - 11:59 AM, said:
where is the problem? you don't know how to use timer? or you don't know how to implement function? or something else?
My advice is to use timer. First of all you must create variable inside form class which will represent current picture opacity. Assume you want to fade picture from invisible to 100% visible, then first set current picture opacity to 0. If you want to picture fade in in 1 second, that means you must increase picture opacity by 0.01 every 10 milliseconds. That means you must set timer interval=10.
My advice is to use timer. First of all you must create variable inside form class which will represent current picture opacity. Assume you want to fade picture from invisible to 100% visible, then first set current picture opacity to 0. If you want to picture fade in in 1 second, that means you must increase picture opacity by 0.01 every 10 milliseconds. That means you must set timer interval=10.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick ' increase every 10 milliseconds CurrentPictureOpacity+=0.01 ' note: check if CurrentPictureOpacity is less or equal 1. If not then stop timer or else you will get error. PictureBox1.Image = SetImageOpacity(samplePicture,CurentPictureOpacity) End Sub
oh ok thanks for all the help.I'll mess around with this and let you know if i run into a problem
#12
Re: fade in components in visual basic
Posted 11 February 2012 - 09:58 PM
Did a bit of research and found a easy solution.Thanks for all the help with this.
Public Class Form1
Private _fadeOpacity As Single = 0
Private oldImage As Bitmap
Private newImage As Bitmap
Private Function FadeBitmap(ByVal bmp As Bitmap, ByVal opacity As Single) As Bitmap
Dim bmp2 As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb)
opacity = Math.Max(0, Math.Min(opacity, 1.0F))
Using ia As New Imaging.ImageAttributes
Dim cm As New Imaging.ColorMatrix
cm.Matrix33 = opacity
ia.SetColorMatrix(cm)
Dim destpoints() As PointF = {New Point(0, 0), New Point(bmp.Width, 0), New Point(0, bmp.Height)}
Using g As Graphics = Graphics.FromImage(bmp2)
g.DrawImage(bmp, destpoints, _
New RectangleF(Point.Empty, bmp.Size), GraphicsUnit.Pixel, ia)
End Using
End Using
Return bmp2
End Function
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If _fadeOpacity < 100 AndAlso oldImage IsNot Nothing Then
e.Graphics.DrawImageUnscaled(oldImage, Point.Empty)
End If
If _fadeOpacity > 0 AndAlso newImage IsNot Nothing Then
Using fadedImage As Bitmap = FadeBitmap(newImage, _fadeOpacity)
e.Graphics.DrawImageUnscaled(fadedImage, Point.Empty)
End Using
End If
End Sub
Private Sub Form1_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles Me.Layout
oldImage = New Bitmap(Image.FromFile("C:\Users\PC1\Desktop\Icons\button2.bmp"))
newImage = New Bitmap(Image.FromFile("C:\Users\PC1\Desktop\Icons\button3.bmp"))
PictureBox1.Refresh()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer2.Start()
Timer1.Stop()
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
_fadeOpacity += 0.02
If _fadeOpacity >= 1 Then
_fadeOpacity = 1
Timer2.Stop()
End If
PictureBox1.Invalidate()
End Sub
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|