Public Class Form1 #Region "プロパティ" ''' <summary> ''' マイピクチャーのパス ''' </summary> ''' <returns>パス</returns> Private ReadOnly Property GetPicturePath() Get Dim strImgPath As String strImgPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) Return strImgPath End Get End Property #End Region Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click ' グラフィックス Dim g As Graphics = PictureBox1.CreateGraphics() ' イメージのパス Dim strImgPath As String = String.Concat(GetPicturePath, "\sample.bmp") ' イメージの読み込み Dim img As Image = Image.FromFile(strImgPath) ' 徐々に透過を解く Dim j As Integer = 1 For i = 9 To 0 Step -1 ImageChange(g, img, i * 0.1F) System.Threading.Thread.Sleep(200) ImageChange(g, j * 0.1F) System.Threading.Thread.Sleep(500) j += 1 Next ' リソース解放処理 img.Dispose() g.Dispose() End Sub ''' <summary> ''' (ポリモフィズム)イメージを切り替える。 ''' </summary> ''' <param name="g">グラフィックスメソッド</param> ''' <param name="alpha">アルファ値</param> Private Sub ImageChange(ByVal g As Graphics, ByVal alpha As Single) ' イメージのパス Dim strImgPath As String = Me.GetPicturePath + "\sample2.bmp" ' 読み込むイメージ Dim img As Image = Image.FromFile(strImgPath) ImageChange(g, img, alpha) End Sub ''' <summary> ''' イメージを切り替える。 ''' </summary> ''' <param name="g">グラフィックス</param> ''' <param name="img">イメージ</param> ''' <param name="alpha">アルファ値</param> ''' <remarks></remarks> Private Sub ImageChange(ByVal g As Graphics, _ ByVal img As Image, ByVal alpha As Single) ' 読み込む画像 Dim back As New Bitmap(img.Width, img.Height) ' グラフィックス Dim bg As Graphics = Graphics.FromImage(back) ' 透過で変更する値 Dim cm As New System.Drawing.Imaging.ColorMatrix cm.Matrix00 = 1 cm.Matrix11 = 1 cm.Matrix22 = 1 cm.Matrix33 = alpha cm.Matrix44 = 1 ' cmの値をカラーマトリックスとして設定 Dim attr As New System.Drawing.Imaging.ImageAttributes attr.SetColorMatrix(cm) bg.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), _ 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attr) g.DrawImage(back, 0, 0) ' リソース解放 bg.Dispose() back.Dispose() End Sub End Class