4,103 questions
Hi,
You can refer to the following code.
Imports System.Drawing.Drawing2D
Public Class Form1
Private NewImg As Img1
Private Imgs As New List(Of Img1)
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
NewImg = New Img1("C:\Users\Administrator\Desktop\Cat.jfif", e.Location, e.Location)
Imgs.Add(NewImg)
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If NewImg Is Nothing Then Return
NewImg.Point2 = e.Location
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
For Each Img As Img1 In Imgs
Img.Draw(e.Graphics)
Next
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
NewImg = Nothing
End Sub
Private Sub PictureBox1_SizeChanged(sender As Object, e As EventArgs) Handles PictureBox1.SizeChanged
PictureBox1.Invalidate()
End Sub
End Class
Class Img1
Private pt1, pt2 As Point
Private img As Image
Public Sub New(imageloc As String, startPoint As Point, endPoint As Point)
img = Image.FromFile(imageloc)
Point1 = startPoint
Point2 = endPoint
End Sub
Public Property Point1 As Point
Get
Return pt1
End Get
Set(value As Point)
pt1 = value
End Set
End Property
Public Property Point2 As Point
Get
Return pt2
End Get
Set(value As Point)
pt2 = value
End Set
End Property
Public Sub Draw(gr As Graphics)
Dim newsize As New Size(Point2.X - Point1.X, Point2.Y - Point1.Y)
Dim newpt As New Point(Math.Min(pt1.X, pt2.X), Math.Min(pt1.Y, pt2.Y))
gr.DrawImage(resizeImage(img, newsize), newpt)
End Sub
Private Shared Function resizeImage(ByVal imgToResize As Image, ByVal size As Size) As Image
Dim sourceWidth As Integer = imgToResize.Width
Dim sourceHeight As Integer = imgToResize.Height
Dim nPercent As Single = 0
Dim nPercentW As Single = 0
Dim nPercentH As Single = 0
nPercentW = (CSng(size.Width) / CSng(sourceWidth))
nPercentH = (CSng(size.Height) / CSng(sourceHeight))
Dim destWidth As Integer = CInt((sourceWidth * nPercentW))
Dim destHeight As Integer = CInt((sourceHeight * nPercentH))
If destHeight = 0 OrElse destWidth = 0 Then
Dim b As Bitmap = New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(CType(b, Image))
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(imgToResize, 0, 0, Math.Abs(destWidth), Math.Abs(destHeight))
g.Dispose()
Return CType(b, Image)
ElseIf destHeight > 0 AndAlso destWidth > 0 Then
Console.WriteLine(destHeight & "," & destWidth)
Dim b As Bitmap = New Bitmap(Math.Abs(destWidth), Math.Abs(destHeight))
Dim g As Graphics = Graphics.FromImage(CType(b, System.Drawing.Image))
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(imgToResize, 0, 0, Math.Abs(destWidth), Math.Abs(destHeight))
g.Dispose()
Return CType(b, Image)
ElseIf destHeight > 0 AndAlso destWidth < 0 Then
Dim b As Bitmap = New Bitmap(Math.Abs(destWidth), Math.Abs(destHeight))
Dim g As Graphics = Graphics.FromImage(CType(b, Image))
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(imgToResize, 0, 0, Math.Abs(destWidth), Math.Abs(destHeight))
g.Dispose()
b.RotateFlip(RotateFlipType.RotateNoneFlipX)
Return CType(b, Image)
ElseIf destHeight < 0 AndAlso destWidth > 0 Then
Dim b As Bitmap = New Bitmap(Math.Abs(destWidth), Math.Abs(destHeight))
Dim g As Graphics = Graphics.FromImage(CType(b, Image))
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(imgToResize, 0, 0, Math.Abs(destWidth), Math.Abs(destHeight))
g.Dispose()
b.RotateFlip(RotateFlipType.RotateNoneFlipY)
Return CType(b, Image)
Else
Dim b As Bitmap = New Bitmap(Math.Abs(destWidth), Math.Abs(destHeight))
Dim g As Graphics = Graphics.FromImage(CType(b, Image))
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(imgToResize, 0, 0, Math.Abs(destWidth), Math.Abs(destHeight))
g.Dispose()
b.RotateFlip(RotateFlipType.RotateNoneFlipXY)
Return CType(b, Image)
End If
End Function
End Class
Best Regards.
Jiachen Li
----------
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.