Welcome to ADO.NET Access 2003—your ultimate hub for VB.NET and ADO.NET programming excellence. Discover in-depth tutorials, practical code samples, and expert troubleshooting guides covering a broad range of topics—from building robust WinForms applications and seamless MS Access integration to working with SQL Server, MySQL, and advanced tools like WebView2 and Crystal Reports. Whether you're a beginner or a seasoned developer, our step-by-step articles are designed to empower you to optimize.

Looking for MS Access Developer❓❓

Application developer

Post Page Advertisement [Top]

🌟 Fade Images in VB .NET: A Comprehensive Guide

Fade Image in VB.NET 2010

Adding a fade effect to images is a subtle yet powerful enhancement in desktop applications. In this tutorial, you'll learn how to create a smooth fade-out animation on an image using VB.NET 2010.

🛠️ Project Setup

  • Form Name: Form1
  • Form Text: Fade Image in VB 2010

Controls used:

  1. PictureBox (PicToFade)
    • BackColor: Transparent
    • Dock: Fill
    • SizeMode: StretchImage
  2. Timer (Timer1)
    • Interval: 100 ms
  3. OpenFileDialog (OpenFileDialog1)

🧩 How It Works

  1. Image Selection: Clicking the PictureBox opens a dialog to choose an image.
  2. Image Display: The selected image is shown in the PictureBox.
  3. Fade-Out Effect: The Timer gradually decreases the image's opacity.

💡 Code Implementation

1. Variables and Initialization


Imports System.Drawing.Imaging

Public Class Form1
  Private fadeAlpha As Single = 1.0F
  Private fadeStep As Single = 0.05F
  Private originalImage As Image
  Private imageAttributes As New ImageAttributes()

2. Image Selection and Display


Private Sub PicToFade_Click(sender As Object, e As EventArgs) Handles PicToFade.Click
  OpenFileDialog1.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp"
  If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    originalImage = Image.FromFile(OpenFileDialog1.FileName)
    PicToFade.Image = originalImage
    fadeAlpha = 1.0F
    Timer1.Start()
  End If
End Sub

3. Fade-Out Animation


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  If fadeAlpha > 0 Then
    fadeAlpha -= fadeStep
    If fadeAlpha < 0 Then fadeAlpha = 0

    Dim cm As New ColorMatrix()
    cm.Matrix33 = fadeAlpha
    imageAttributes.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)

    Dim bmp As New Bitmap(originalImage.Width, originalImage.Height)
    Using g As Graphics = Graphics.FromImage(bmp)
      g.DrawImage(originalImage, New Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel, imageAttributes)
    End Using

    PicToFade.Image = bmp
  Else
    Timer1.Stop()
  End If
End Sub

🎯 Customization Tips

  • Fade Speed: Lower fadeStep for a slower fade effect.
  • Fade-In: Initialize fadeAlpha = 0.0F and increment it in the Timer.
  • Loop Fade: Reverse fadeStep direction at min/max opacity.

🔁 Bonus Gist (Alternative Code Sample)

See another variation of the fade logic on GitHub:

🔗 View VB.NET Fade Gist

 Here are some online Visual Basic lessons and courses:

Bottom Ad [Post Page]