11 Replies - 1552 Views - Last Post: 11 February 2012 - 09:58 PM Rate Topic: -----

#1 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

fade in components in visual basic

Posted 11 February 2012 - 02:47 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: fade in components in visual basic

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

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:
System.Drawing.Imaging
System.Drawing.Graphics


Was This Post Helpful? 0
  • +
  • -

#3 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: fade in components in visual basic

Posted 11 February 2012 - 06:20 AM

View Postsela007, on 11 February 2012 - 05:47 AM, said:

button doesn't have opacity property. But you can create button from picture box. Then you can set image opacity.
look in:
System.Drawing.Imaging
System.Drawing.Graphics


so how exactly do you fade it in and out?
Was This Post Helpful? 0
  • +
  • -

#4 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

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.
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.
Was This Post Helpful? 0
  • +
  • -

#5 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

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.
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)


Was This Post Helpful? 0
  • +
  • -

#6 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: fade in components in visual basic

Posted 11 February 2012 - 08:12 AM

View Postsela007, 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.
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

Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 957
  • View blog
  • Posts: 3,680
  • Joined: 02-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#8 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: fade in components in visual basic

Posted 11 February 2012 - 08:54 AM

View Post_HAWK_, on 11 February 2012 - 08:36 AM, said:

What's wrong with experimentation? Better learning than - please spoon feed me even if it a video.

I have been experimenting on this for a while now but had no luck.Now im desperate lol.
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 957
  • View blog
  • Posts: 3,680
  • Joined: 02-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#10 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

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.
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


Was This Post Helpful? 1
  • +
  • -

#11 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 12

Re: fade in components in visual basic

Posted 11 February 2012 - 09:10 PM

View Postsela007, 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.
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 :)
Was This Post Helpful? 0
  • +
  • -

#12 Bluezap  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 212
  • Joined: 19-January 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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1