Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Created June 11, 2021 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj1711572002/9594e6922e402261dbd7d160f4412dcb to your computer and use it in GitHub Desktop.
Save dj1711572002/9594e6922e402261dbd7d160f4412dcb to your computer and use it in GitHub Desktop.
VB.NET DrawImage to Transforma a square into a parallelogram.
Public Class Form1
Dim totalPlot As Bitmap
Dim tg As Graphics
Dim ulx, uly, urx, ury, llx, lly As Integer
Private Sub plotSlider(ByVal startNo As Integer)
'画像の一部を切り取って(トリミングして)表示する
Debug.Print(startNo)
Dim timeScale As Integer = 3
Dim canvas As New Bitmap(410, 210) 'W=400,H=240
Dim g As Graphics = Graphics.FromImage(canvas)
Dim canvas3 As New Bitmap(600, 400) 'W=400,H=240
Dim g3 As Graphics = Graphics.FromImage(canvas3)
Dim srcRect As New Rectangle(startNo, 0, 400, 210)
'描画する部分の範囲を決定する。ここでは、位置(0,0)、大きさ100x100で描画する
Dim desRect As New Rectangle(0, 0, srcRect.Width, srcRect.Height)
'画像の一部を描画する
g.DrawImage(totalPlot, desRect, srcRect, GraphicsUnit.Pixel)
g3.DrawImage(totalPlot, desRect, srcRect, GraphicsUnit.Pixel)
Dim p As New Pen(Color.Red, 1)
'位置(10, 20)に100x80の長方形を描く
g.DrawRectangle(p, 0, 0, 399, 199)
g3.DrawRectangle(p, 0, 0, 399, 199)
''======四角画像を変形表示させる位置を指定する=======
''元の画像の左上の位置を(ulx, uly)にする
''元の画像の右上の位置を(urx, ury)にする
''元の画像の左下の位置を(llx, lly)にする
Dim destinationPoints() As Point
destinationPoints = New Point() {New Point(ulx, uly),
New Point(urx, ury),
New Point(llx, lly)}
'canvasを変形して傾いた画像をg3(canvas3)へ描画する
g3.DrawImage(canvas, destinationPoints)
'Graphicsオブジェクトのリソースを解放する
g.Dispose()
g3.Dispose()
'PictureBox1に表示する
PictureBox2.Image = canvas 'gで大きなtotalPlotを切り取った画像canvasをPictureBox2へ書き込む
PictureBox3.Image = canvas3 'g3で作っグラフィックcanvas3をPictureBox3へ書き込む
End Sub
Private Sub HScrollBar1_ValueChanged(sender As Object, e As EventArgs) Handles HScrollBar1.ValueChanged
'maximum=1200 minimum=0
Dim slideVal As Integer = HScrollBar1.Value
plotSlider(slideVal)
TextBox1.Text = CStr(slideVal)
End Sub
'元グラフを作成する
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
totalPlot = New Bitmap(PictureBox1.Width * 3, PictureBox1.Height)
tg = Graphics.FromImage(totalPlot)
Dim i As Integer
For i = 1 To PictureBox1.Width * 3
Dim px As Integer = i
Dim py As Integer = Math.Sin(Math.PI / 30 * i) * 100 * (i / 1200) + 100
tg.FillEllipse(Brushes.Red, px, py, 4, 4)
Next i
PictureBox1.Image = totalPlot
End Sub
Private Sub PictureBox3_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox3.MouseClick
'画面座標でマウスポインタの位置を取得する
Dim sp As System.Drawing.Point = System.Windows.Forms.Cursor.Position
'画面座標をクライアント座標に変換する
Dim cp As System.Drawing.Point = Me.PointToClient(sp)
'X座標を取得する
Dim x As Integer = cp.X
'Y座標を取得する
Dim y As Integer = cp.Y
If RadioButton1.Checked = True Then
ulx = x - PictureBox3.Left
TextBox2.Text = x - PictureBox2.Left
uly = y - PictureBox3.Top
TextBox3.Text = y - PictureBox2.Top
End If
If RadioButton2.Checked = True Then
urx = x - PictureBox3.Left
TextBox4.Text = x - PictureBox2.Left
ury = y - PictureBox3.Top
TextBox5.Text = y - PictureBox2.Top
End If
If RadioButton3.Checked = True Then
llx = x - PictureBox3.Left
TextBox6.Text = x - PictureBox2.Left
lly = y - PictureBox3.Top
TextBox7.Text = y - PictureBox2.Top
End If
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment