事前準備
・フォームに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.Format16bppRgb555)
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()
bmp.MakeTransparent(bmp.GetPixel(0, 0))
bmp.MakeTransparent(bmp.GetPixel(1, 1))
bmp.MakeTransparent(bmp.GetPixel(0, 1))
bmp.MakeTransparent(bmp.GetPixel(1, 0))
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
マウスの左ボタンでムーブ時に、フリーハンドの直線を描き、
右クリックにて、Bitmapファイルを保存します。
保存は自動で任せてしまっています。
これで保存すると、Paintなどの一部アプリケーションで読み込めません。
また、
bmp.MakeTransparent(bmp.GetPixel(0, 0))
bmp.MakeTransparent(bmp.GetPixel(1, 1))
bmp.MakeTransparent(bmp.GetPixel(0, 1))
bmp.MakeTransparent(bmp.GetPixel(1, 0))
の部分を、
bmp.MakeTransparent(bmp.GetPixel(0, 0))
bmp.MakeTransparent(bmp.GetPixel(1, 1))
だけにすると、
こんな感じになります。
関連記事
・PixelFormat.Format16bppRgb565形式