ヘッダー

VB.NET2002対応 VB.NET2003対応 VB2005対応

 

画像の明るさを設定する

以下の関数Brightenを使用すると、画像を明るくしたり暗くしたりすることができる。使用例はすぐ下にある。

なお、画像の色彩を調節するの使用例2ではより高度な明るさ設定が可能。ただし遅い。

VB.NET2002対応 VB.NET2003対応 VB2005対応

'■Brighten
''' <summary>画像の明るさを設定する。</summary>
''' <param name="Source">対象の画像</param>
''' <param name="Alpha">明るさ。-255~の範囲で指定。</param>
''' <returns>明るさが設定された画像</returns>
Private Function Brighten(ByVal Source As Image, ByVal Alpha As Integer) As Bitmap

    '▼引数のチェック
   
If IsNothing(Source) Then
       
Throw New NullReferenceException("Sourceに値が設定されていません。")
    End
If

    If Alpha < -255 OrElse Alpha > 255 Then
       
Throw New ArgumentException("Alphaは-255 から255 の範囲で指定してください。")
    End
If

    '▼Sourceのイメージをそのまま描画
   
Dim g As Graphics
    Dim SourceImage As New Bitmap(Source.Width, Source.Height)

    g = Graphics.FromImage(SourceImage)
    g.DrawImage(Source,
New Point(0, 0))

    '▼Sourceのイメージの上に白い(黒い)長方形を重ねる
   
Dim MyBrush As SolidBrush

    If Alpha > 0 Then
       
'白い長方形を作成
       
MyBrush = New SolidBrush(Color.FromArgb(Alpha, 255, 255, 255))
        g.FillRectangle(MyBrush, Source.GetBounds(GraphicsUnit.Pixel))
   
Else
       
'黒い長方形を作成
       
MyBrush = New SolidBrush(Color.FromArgb(-Alpha, 0, 0, 0))
        g.FillRectangle(MyBrush, Source.GetBounds(GraphicsUnit.Pixel))
    End
If

    Return SourceImage

End Function

 

使用例1:PictureBoxの画像を少し明るくする

VB.NET2002対応 VB.NET2003対応 VB2005対応

PictureBox1.Image = Brighten(PictureBox1.Image, 50)
PictureBox1.Refresh()

メモ:PictureBox1にはあらかじめ画像を読み込んでおくなどしてImageプロパティに値をセットしておく必要があります。

 

使用例2:PictureBoxの画像を少し暗くする

VB.NET2002対応 VB.NET2003対応 VB2005対応

PictureBox1.Image = Brighten(PictureBox1.Image, -50)
PictureBox1.Refresh()

メモ:PictureBox1にはあらかじめ画像を読み込んでおくなどしてImageプロパティに値をセットしておく必要があります。

 


VB6対応  VB6ではAPIやDirectX等を使用しますがかなり困難です。