【VB】画像を半透明で重ねる/透明色を指定する - VB備忘録 | 八八八

【VB】画像を半透明で重ねる/透明色を指定する

■ファイルから読み込んで半透明で重ねる

以下の例では既に画像が表示されているPictureBox1に、
新たな画像をファイルから読み込んで半透明で重ねて表示する。

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

Dim FileName As String = "C:\Sample1.bmp"
Dim SourceImage As Image = Image.FromFile(FileName)
Dim DestImage As Image = PictureBox1.Image
Dim g As Graphics = Graphics.FromImage(DestImage)
Dim Attr As New Imaging.ImageAttributes
Dim M As New Imaging.ColorMatrix
M.Matrix00 = 1.0F
M.Matrix11 = 1.0F
M.Matrix22 = 1.0F
M.Matrix33 = 0.5F
M.Matrix44 = 1.0F
Attr.SetColorMatrix(M)

g.DrawImage(SourceImage, New Rectangle(New Point, SourceImage.Size), 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, Attr)

PictureBox1.Refresh()

SourceImage.Dispose()


'*****************************************************

■PictureBox2の画像を半透明にしてPicutreBox1に重ねる

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

Dim SourceImage As Image = PictureBox2.Image
Dim DestImage As Image = PictureBox1.Image
Dim g As Graphics = Graphics.FromImage(DestImage)
Dim Attr As New Imaging.ImageAttributes
Dim M As New Imaging.ColorMatrix
M.Matrix00 = 1.0F
M.Matrix11 = 1.0F
M.Matrix22 = 1.0F
M.Matrix33 = 0.5F
M.Matrix44 = 1.0F
Attr.SetColorMatrix(M)

g.DrawImage(SourceImage, New Rectangle(New Point, SourceImage.Size), 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, Attr)

PictureBox1.Refresh()



'/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_

■指定した色を透明する

以下の例では既に画像が表示されているPictureBox1に、
PictureBox2の画像の白い部分を透明にして重ねる。

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

メモ:完全に白い部分だけが透明になります。人間の目には
   白く見えても白と少しでも違う色は透明になりません。

Dim SourceImage As Image = PictureBox2.Image
Dim DestImage As Image = PictureBox1.Image
Dim g As Graphics = Graphics.FromImage(DestImage)
Dim Attr As New Imaging.ImageAttributes
Attr.SetColorKey(Color.White, Color.White) '白を透明色に指定

g.DrawImage(SourceImage, New Rectangle(New Point, SourceImage.Size), 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, Attr)

PictureBox1.Refresh()


'*****************************************************

■指定した色の範囲を透明にする

以下の例では既に画像が表示されているPictureBox1に、
PictureBox2の画像の薄い灰色の部分から白い部分を
透明にして重ねる。

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

Dim SourceImage As Image = PictureBox2.Image
Dim DestImage As Image = PictureBox1.Image
Dim g As Graphics = Graphics.FromImage(DestImage)
Dim Attr As New Imaging.ImageAttributes
Dim HighColor As Color = Color.FromArgb(255, 255, 255) '白
Dim LowColor As Color = Color.FromArgb(150, 150, 150) '薄い灰色
Attr.SetColorKey(LowColor, HighColor) '薄い灰色から白の範囲の色を透明色に指定

g.DrawImage(SourceImage, New Rectangle(New Point, SourceImage.Size), 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, Attr)

PictureBox1.Refresh()

 
 






読者の皆様、日々の開発業務、どうもお疲れ様です!
いろいろ試してみましたが、疲れたときにはやっぱりこれが一番元気出るんですよね。
カフェイン多めで眠気スッキリ。味も美味しいですし♪
アサヒのMONSTER飲んで、今日も乗り切りましょう!

×

この広告は1年以上新しい記事の投稿がないブログに表示されております。