事前準備
・フォームにPictureBoxを貼り付け、DockプロパティをFilleにする。
Imports System.Drawing.Imaging
Public Class Form1
Private bmp As Bitmap
Private StX As Single, StY As Single
Private EnX As Single, EnY As Single
Private mDown As Boolean = False
Private HBitmapSrc As IntPtr
Private TransparentColor As Color = Color.Tomato
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height, PixelFormat.Format16bppRgb565)
bmpTransparent()
End Sub
Private Sub bmpTransparent()
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillRectangle(New SolidBrush(TransparentColor), New Rectangle(0, 0, bmp.Width, bmp.Height))
g.Dispose()
For i As Integer = 0 To 2
For j As Integer = 0 To 2
bmp.MakeTransparent(bmp.GetPixel(i, j))
Next
Next
End Sub
Private Sub PictureBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
mDown = True
StX = e.X : StY = e.Y
EnX = e.X : EnY = e.Y
End Sub
Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If mDown = False Then Exit Sub
EnX = e.X : EnY = e.Y
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawLine(Pens.Black, StX, StY, EnX, EnY)
StX = e.X : StY = e.Y
g.Dispose()
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
mDown = False
End Sub
Private Sub meclick(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
bmp.Save(“C:\aaa.bmp”)
End If
End Sub
Private Sub Picture_Box(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(bmp, 0, 0)
End Sub
End Class
Format16bppRgb555との違いは、
For i As Integer = 0 To 2
For j As Integer = 0 To 2
bmp.MakeTransparent(bmp.GetPixel(i, j))
Next
Next
の部分くらいです。
実行結果は、Format16bppRgb555とほぼ同じになるので省略します。
相変わらず、この保存方法だとPaintなどの一部アプリケーションでは読めないようです。
関連記事
・PixelFormat.Format16bppRgb555形式の実験用
システム開発のためのVB.NETプログラミング関係一覧に戻る