Imports System.IO
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Public Class Form1
Private sourceImage As Bitmap
Private isDrawing As Boolean
Private lastPoint As Point
Private sourceImage1 As Bitmap
Private sourceImage2 As Bitmap
Private isDrawingText As Boolean
Private lastTextPoint As Point
'加一个集合来存储每次输入文字的位置和文本
Private textPositionsAndTexts As New List(Of Tuple(Of Point, String))
Private isSelecting As Boolean
Private selectionStartPoint As Point
Private selectionEndPoint As Point
Private isMovingText As Boolean '标识是否正在移动文字
Private movingTextIndex As Integer '正在移动的文字在集合中的索引
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
InitializeTrackBar()
End Sub
Private Sub InitializeTrackBar()
'置滑动条的属性
TrackBar1.Minimum = -255
TrackBar1.Maximum = 255
TrackBar1.Value = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.gif;*.bmp"
openFileDialog1.Title = "选择图片"
If openFileDialog1.ShowDialog() = DialogResult.OK Then
sourceImage = New Bitmap(openFileDialog1.FileName)
PictureBox1.Image = sourceImage
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button2.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "JPEG 图像|*.jpg"
saveFileDialog1.Title = "保存图片"
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Try
Dim bitmap As New Bitmap(sourceImage)
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Jpeg)
Catch ex As Exception
MessageBox.Show("保存图片时出错: " & ex.Message)
End Try
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button3.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "JPEG 图像|*.jpg|PNG 图像|*.png|BMP 图像|*.bmp"
saveFileDialog1.Title = "保存图片"
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Try
Dim bitmap As New Bitmap(sourceImage)
Select Case saveFileDialog1.FilterIndex
Case 1 'JPEG
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Jpeg)
Case 2 'PNG
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Png)
Case 3 'BMP
bitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp)
End Select
Catch ex As Exception
MessageBox.Show("保存图片时出错: " & ex.Message)
End Try
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button4.Click
If sourceImage IsNot Nothing Then
Dim startX As Integer = 10 '裁剪起始的 X 坐标
Dim startY As Integer = 10 '裁剪起始的 Y 坐标
Dim cropWidth As Integer = 100 '裁剪的宽度
Dim cropHeight As Integer = 100 '裁剪的高度
Dim croppedImage As Bitmap = New Bitmap(cropWidth, cropHeight)
Using graphics As Graphics = Graphics.FromImage(croppedImage)
graphics.DrawImage(sourceImage, New Rectangle(0, 0, cropWidth, cropHeight), startX, startY, cropWidth, cropHeight, GraphicsUnit.Pixel)
End Using
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "JPEG 图像|*.jpg|PNG 图像|*.png|BMP 图像|*.bmp"
saveFileDialog1.Title = "保存裁剪后的图片"
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Try
Select Case saveFileDialog1.FilterIndex
Case 1 'JPEG
croppedImage.Save(saveFileDialog1.FileName, ImageFormat.Jpeg)
Case 2 'PNG
croppedImage.Save(saveFileDialog1.FileName, ImageFormat.Png)
Case 3 'BMP
croppedImage.Save(saveFileDialog1.FileName, ImageFormat.Bmp)
End Select
Catch ex As Exception
MessageBox.Show("保存图片时出错: " & ex.Message)
End Try
End If
Else
MessageBox.Show("请先打开图片再进行裁剪操作")
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button5.Click
If sourceImage IsNot Nothing Then
Dim flippedHorizontallyImage As Bitmap = FlipImageHorizontally(sourceImage)
PictureBox1.Image = flippedHorizontallyImage
Else
MessageBox.Show("请先打开图片再进行水平翻转操作")
End If
End Sub
Private Function FlipImageHorizontally(ByVal image As Bitmap) As Bitmap
Dim flippedImage As New Bitmap(image.Width, image.Height)
For y As Integer = 0 To image.Height - 1
For x As Integer = 0 To image.Width - 1
flippedImage.SetPixel(image.Width - 1 - x, y, image.GetPixel(x, y))
Next
Next
Return flippedImage
End Function
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button6.Click
If sourceImage IsNot Nothing Then
Dim flippedVerticallyImage As Bitmap = FlipImageVertically(sourceImage)
PictureBox1.Image = flippedVerticallyImage
Else
MessageBox.Show("请先打开图片再进行垂直翻转操作")
End If
End Sub
Private Function FlipImageVertically(ByVal image As Bitmap) As Bitmap
Dim flippedImage As New Bitmap(image.Width, image.Height)
For y As Integer = 0 To image.Height - 1
For x As Integer = 0 To image.Width - 1
flippedImage.SetPixel(x, image.Height - 1 - y, image.GetPixel(x, y))
Next
Next
Return flippedImage
End Function
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button7.Click
If sourceImage IsNot Nothing Then
Using graphics As Graphics = Graphics.FromImage(sourceImage)
Dim font As New Font("Arial", 20, FontStyle.Bold)
Dim brush As New SolidBrush(Color.Red)
graphics.SmoothingMode = SmoothingMode.AntiAlias
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
graphics.DrawString("这是按钮输入的文字", font, brush, 50, 50)
End Using
PictureBox1.Image = sourceImage
Else
MessageBox.Show("请先打开图片再添加文字")
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
isSelecting = True
selectionStartPoint = e.Location
End If
If e.Button = MouseButtons.Right Then
If isDrawingText Then
'开始输入文字时,记录初始位置
lastTextPoint = e.Location
ElseIf textPositionsAndTexts.Count > 0 Then
'检查是否点击了已输入的文字区域
For i As Integer = 0 To textPositionsAndTexts.Count - 1
Dim textData As Tuple(Of Point, String) = textPositionsAndTexts(i)
Dim font As New Font("Arial", 20, FontStyle.Bold)
Using graphics As Graphics = PictureBox1.CreateGraphics()
Dim size As SizeF = graphics.MeasureString(textData.Item2, font)
End Using
Dim rect As New RectangleF(textData.Item1, Size)
If rect.Contains(e.Location) Then
isMovingText = True
movingTextIndex = i
lastTextPoint = e.Location
Exit For
End If
Next
End If
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)Handles PictureBox1.MouseMove
If isSelecting Then
selectionEndPoint = e.Location
PictureBox1.Invalidate() ' 触发重绘
End If
If isMovingText Then
Dim offsetX As Integer = e.Location.X - lastTextPoint.X
Dim offsetY As Integer = e.Location.Y - lastTextPoint.Y
Dim newPoint As Point = textPositionsAndTexts(movingTextIndex).Item1
newPoint.Offset(offsetX, offsetY)
textPositionsAndTexts(movingTextIndex) = New Tuple(Of Point, String)(newPoint, textPositionsAndTexts(movingTextIndex).Item2)
lastTextPoint = e.Location
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)Handles PictureBox1.MouseUp
isSelecting = False
CropSelectedArea()
If isMovingText Then
isMovingText = False
End If
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
If sourceImage IsNot Nothing AndAlso isSelecting Then
Using pen As New Pen(Color.Red, 2)
e.Graphics.DrawRectangle(pen, GetSelectionRectangle())
End Using
End If
If sourceImage IsNot Nothing Then
For Each item In textPositionsAndTexts
Dim font As New Font("Arial", 20, FontStyle.Bold)
Dim brush As New SolidBrush(Color.Red)
e.Graphics.DrawString(item.Item2, font, brush, item.Item1)
Next
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
If sourceImage IsNot Nothing And isDrawingText Then
'输入新文字时,添加新的位置和文本到集合
textPositionsAndTexts.Add(New Tuple(Of Point, String)(lastTextPoint, TextBox1.Text))
PictureBox1.Invalidate()
End If
End Sub
Private Function GetSelectionRectangle() As Rectangle
Dim left As Integer = Math.Min(selectionStartPoint.X, selectionEndPoint.X)
Dim top As Integer = Math.Min(selectionStartPoint.Y, selectionEndPoint.Y)
Dim width As Integer = Math.Abs(selectionStartPoint.X - selectionEndPoint.X)
Dim height As Integer = Math.Abs(selectionStartPoint.Y - selectionEndPoint.Y)
Return New Rectangle(left, top, width, height)
End Function
Private Sub CropSelectedArea()
Dim selectionRect As Rectangle = GetSelectionRectangle()
If selectionRect.Width > 0 AndAlso selectionRect.Height > 0 Then
Dim croppedImage As Bitmap = New Bitmap(selectionRect.Width, selectionRect.Height)
Using graphics As Graphics = Graphics.FromImage(croppedImage)
graphics.DrawImage(sourceImage, New Rectangle(0, 0, selectionRect.Width, selectionRect.Height), selectionRect, GraphicsUnit.Pixel)
End Using
PictureBox1.Image = croppedImage
End If
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
If sourceImage IsNot Nothing Then
Dim brightnessValue As Integer = Integer.Parse(InputBox("请输入亮度值(-255 到 255)", "调整亮度"))
Dim adjustedImage As Bitmap = AdjustBrightness(sourceImage, brightnessValue)
PictureBox1.Image = adjustedImage
Else
MessageBox.Show("请先打开图片再调整亮度")
End If
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
If sourceImage IsNot Nothing Then
Dim newWidth As Integer = Integer.Parse(InputBox("请输入新的宽度", "调整大小"))
Dim newHeight As Integer = Integer.Parse(InputBox("请输入新的高度", "调整大小"))
Dim resizedImage As Bitmap = ResizeImage(sourceImage, newWidth, newHeight)
PictureBox1.Image = resizedImage
Else
MessageBox.Show("请先打开图片再调整大小")
End If
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If sourceImage IsNot Nothing Then
Dim rotationAngle As Long = CSng(Integer.Parse(InputBox("请输入旋转角度(0 到 360)", "旋转图片")))
Dim rotatedImage As Bitmap = RotateImage(sourceImage, rotationAngle)
PictureBox1.Image = rotatedImage
Else
MessageBox.Show("请先打开图片再旋转")
End If
End Sub
Private Function RotateImage(ByVal image As Bitmap, ByVal angle As Single) As Bitmap
Dim rotatedImage As New Bitmap(image.Width, image.Height)
Using graphics As Graphics = Graphics.FromImage(rotatedImage)
graphics.TranslateTransform(image.Width / 2, image.Height / 2)
graphics.RotateTransform(angle)
graphics.TranslateTransform(-image.Width / 2, -image.Height / 2)
graphics.DrawImage(image, 0, 0)
End Using
Return rotatedImage
End Function
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.gif;*.bmp"
openFileDialog1.Title = "选择第一张图片"
If openFileDialog1.ShowDialog() = DialogResult.OK Then
sourceImage1 = New Bitmap(openFileDialog1.FileName)
End If
End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
Dim openFileDialog2 As New OpenFileDialog()
openFileDialog2.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.gif;*.bmp"
openFileDialog2.Title = "选择第二张图片"
If openFileDialog2.ShowDialog() = DialogResult.OK Then
sourceImage2 = New Bitmap(openFileDialog2.FileName)
End If
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
If sourceImage1 IsNot Nothing AndAlso sourceImage2 IsNot Nothing Then
Dim stitchedImage As Bitmap = StitchImages(sourceImage1, sourceImage2)
PictureBox1.Image = stitchedImage
Else
MessageBox.Show("请先选择两张图片")
End If
End Sub
Private Function StitchImages(ByVal image1 As Bitmap, ByVal image2 As Bitmap) As Bitmap
Dim maxWidth As Integer = Math.Max(image1.Width, image2.Width)
Dim totalHeight As Integer = image1.Height + image2.Height
Dim stitchedImage As New Bitmap(maxWidth, totalHeight)
Using graphics As Graphics = Graphics.FromImage(stitchedImage)
graphics.DrawImage(image1, 0, 0)
graphics.DrawImage(image2, 0, image1.Height)
End Using
Return stitchedImage
End Function
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
If sourceImage1 IsNot Nothing AndAlso sourceImage2 IsNot Nothing Then
Dim composedImage As Bitmap = C(sourceImage1, sourceImage2)
PictureBox1.Image = composedImage
Else
MessageBox.Show("请先选择两张图片")
End If
End Sub
Private Function C(ByVal image1 As Bitmap, ByVal image2 As Bitmap) As Bitmap
Dim composedImage As New Bitmap(image1.Width, image1.Height)
Using graphics As Graphics = graphics.FromImage(composedImage)
graphics.DrawImage(image1, 0, 0)
'合成方式为半透明叠加
graphics.CompositingMode = CompositingMode.SourceOver
graphics.CompositingQuality = CompositingQuality.HighQuality
graphics.DrawImage(image2, New Rectangle(0, 0, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, New ImageAttributes())
End Using
Return composedImage
End Function
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
If sourceImage1 IsNot Nothing AndAlso sourceImage2 IsNot Nothing Then
Dim stitchedImage As Bitmap = StitchImages(sourceImage1, sourceImage2)
PictureBox1.Image = stitchedImage
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Title = "保存拼接后的图片"
saveFileDialog.Filter = "JPEG 图像|*.jpg|PNG 图像|*.png|BMP 图像|*.bmp"
If saveFileDialog.ShowDialog() = DialogResult.OK Then
Select Case saveFileDialog.FilterIndex
Case 1 'JPEG
stitchedImage.Save(saveFileDialog.FileName, ImageFormat.Jpeg)
Case 2 'PNG
stitchedImage.Save(saveFileDialog.FileName, ImageFormat.Png)
Case 3 'BMP
stitchedImage.Save(saveFileDialog.FileName, ImageFormat.Bmp)
End Select
End If
Else
MessageBox.Show("请先选择两张图片")
End If
End Sub
Private Function s(ByVal image1 As Bitmap, ByVal image2 As Bitmap) As Bitmap
Dim maxWidth As Integer = Math.Max(image1.Width, image2.Width)
Dim totalHeight As Integer = image1.Height + image2.Height
Dim stitchedImage As New Bitmap(maxWidth, totalHeight)
Using graphics As Graphics = graphics.FromImage(stitchedImage)
graphics.DrawImage(image1, 0, 0)
graphics.DrawImage(image2, 0, image1.Height)
End Using
Return stitchedImage
End Function
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
If sourceImage1 IsNot Nothing AndAlso sourceImage2 IsNot Nothing Then
Dim composedImage As Bitmap = C(sourceImage1, sourceImage2)
PictureBox1.Image = composedImage
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Title = "保存合成后的图片"
saveFileDialog.Filter = "JPEG 图像|*.jpg|PNG 图像|*.png|BMP 图像|*.bmp"
If saveFileDialog.ShowDialog() = DialogResult.OK Then
Select Case saveFileDialog.FilterIndex
Case 1 'JPEG
composedImage.Save(saveFileDialog.FileName, ImageFormat.Jpeg)
Case 2 'PNG
composedImage.Save(saveFileDialog.FileName, ImageFormat.Png)
Case 3 'BMP
composedImage.Save(saveFileDialog.FileName, ImageFormat.Bmp)
End Select
End If
Else
MessageBox.Show("请先选择两张图片")
End If
End Sub
Private Function n(ByVal image1 As Bitmap, ByVal image2 As Bitmap) As Bitmap
Dim composedImage As New Bitmap(image1.Width, image1.Height)
Using graphics As Graphics = graphics.FromImage(composedImage)
graphics.DrawImage(image1, 0, 0)
'合成方式为半透明叠加
graphics.CompositingMode = CompositingMode.SourceOver
graphics.CompositingQuality = CompositingQuality.HighQuality
graphics.DrawImage(image2, New Rectangle(0, 0, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, New ImageAttributes())
End Using
Return composedImage
End Function
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
If sourceImage IsNot Nothing Then
Dim brightnessValue As Integer = TrackBar1.Value
Dim adjustedImage As Bitmap = AdjustBrightness(sourceImage, brightnessValue)
PictureBox1.Image = adjustedImage
End If
End Sub
Private Function AdjustBrightness(ByVal image As Bitmap, ByVal value As Integer) As Bitmap
Dim adjustedImage As New Bitmap(image.Width, image.Height)
For y As Integer = 0 To image.Height - 1
For x As Integer = 0 To image.Width - 1
Dim originalColor As Color = image.GetPixel(x, y)
Dim r As Integer = Clamp(originalColor.R + value, 0, 255)
Dim g As Integer = Clamp(originalColor.G + value, 0, 255)
Dim b As Integer = Clamp(originalColor.B + value, 0, 255)
adjustedImage.SetPixel(x, y, Color.FromArgb(r, g, b))
Next
Next
Return adjustedImage
End Function
Private Function Clamp(ByVal value As Integer, ByVal min As Integer, ByVal max As Integer) As Integer
If value < min Then
Return min
ElseIf value > max Then
Return max
Else
Return value
End If
End Function
Private Function ResizeImage(ByVal image As Bitmap, ByVal newWidth As Integer, ByVal newHeight As Integer) As Bitmap
Dim resizedImage As New Bitmap(newWidth, newHeight)
Using graphics As Graphics = Graphics.FromImage(resizedImage)
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
graphics.DrawImage(image, 0, 0, newWidth, newHeight)
End Using
Return resizedImage
End Function
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
Cursor = Cursors.Default
ZoomInImage()
End Sub
Sub ZoomInImage()
FullImage()
PictureBox1.Width = CInt(PictureBox1.Width * 1.25)
PictureBox1.Height = CInt(PictureBox1.Height * 1.25)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Sub FullImage()
If PictureBox1.Image.Width < PictureBox1.Width And PictureBox1.Image.Height < PictureBox1.Height Then
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End If
CalculateAspectRatioAndSetDimensions()
End Sub
Private Function CalculateAspectRatioAndSetDimensions() As Double
' Calculate the proper aspect ratio and set the image's dimensions.
Dim ratio As Double
If PictureBox1.Image.Width > PictureBox1.Image.Height Then
ratio = PictureBox1.Image.Width / _
PictureBox1.Image.Height
PictureBox1.Height = CInt(CDbl(PictureBox1.Width) / ratio)
Else
ratio = PictureBox1.Image.Height / _
PictureBox1.Image.Width
PictureBox1.Width = CInt(CDbl(PictureBox1.Height) / ratio)
End If
Return ratio
End Function
Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
PictureBox1.Refresh()
Cursor = Cursors.Default
ZoomOutImage()
End Sub
Sub ZoomOutImage()
FullImage()
PictureBox1.Width = CInt(PictureBox1.Width / 1.25)
PictureBox1.Height = CInt(PictureBox1.Height / 1.25)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
TextBox1.Text = ""
If sourceImage IsNot Nothing Then
TextBox1.Visible = True
TextBox1.Focus()
isDrawingText = True
Else
MessageBox.Show("请先打开图片再添加文字")
End If
End Sub
Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
PictureBox1.Image = Nothing
End Sub
End Class