我正在创建一个在picturebox1中裁剪图像的应用程序。它有两个按钮:-Button1= 捕获桌面并将屏幕截图放入图片框 1。背后有这段代码
Imports System.Net.Mail
Public Class Form1
Private SelectSiz As Size
Private SelectPos As System.Drawing.Point
Private Selecting As Boolean = False
Private Selected As Boolean = False
Private source As Image
Dim XOffs As Integer
Dim YOffs As Integer
Private Function cc() As Bitmap
Dim s As Screen = Screen.PrimaryScreen
Dim img As New Bitmap(s.Bounds.Width, s.Bounds.Height)
Dim gr As Graphics = Graphics.FromImage(img)
gr.CopyFromScreen(s.Bounds.Location, Point.Empty, s.Bounds.Size)
Return img
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.PictureBox1.Image = cc()
End Sub
Button2= that crop slected area which user had made it onto image.
with this code behind it
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Selected Then
Dim Cropped As Image = New Bitmap(Math.Abs(SelectSiz.Width), Math.Abs(SelectSiz.Height))
Dim g As Graphics = Graphics.FromImage(Cropped)
Dim SrcRect As Rectangle = GetSelection(SelectPos, SelectSiz)
SrcRect.X += XOffs
SrcRect.Y += YOffs
g.DrawImage(source, New Rectangle(0, 0, Cropped.Width, Cropped.Height), SrcRect, GraphicsUnit.Pixel)
PictureBox1.Image = Cropped
Button3.Enabled = True
Button4.Enabled = True
Button7.Enabled = True
End If
Selected = False
End Sub
但是现在的问题是,当我选择一个区域将其裁剪到图像上并按 Button2 裁剪它时,它会给我这个错误:-{"值不能为空。参数名称:图像"}
在此处输入图像描述
这是我的应用程序的图片在此处输入图像描述
这就是整个代码
Imports System.Net.Mail
Public Class Form1
Private SelectSiz As Size
Private SelectPos As System.Drawing.Point
Private Selecting As Boolean = False
Private Selected As Boolean = False
Private source As Image
Dim XOffs As Integer
Dim YOffs As Integer
Private Function cc() As Bitmap
Dim s As Screen = Screen.PrimaryScreen
Dim img As New Bitmap(s.Bounds.Width, s.Bounds.Height)
Dim gr As Graphics = Graphics.FromImage(img)
gr.CopyFromScreen(s.Bounds.Location, Point.Empty, s.Bounds.Size)
Return img
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.PictureBox1.Image = cc()
End Sub
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Selected Then
Dim Cropped As Image = New Bitmap(Math.Abs(SelectSiz.Width), Math.Abs(SelectSiz.Height))
Dim g As Graphics = Graphics.FromImage(Cropped)
Dim SrcRect As Rectangle = GetSelection(SelectPos, SelectSiz)
SrcRect.X += XOffs
SrcRect.Y += YOffs
g.DrawImage(source, New Rectangle(0, 0, Cropped.Width, Cropped.Height), SrcRect, GraphicsUnit.Pixel)
PictureBox1.Image = Cropped
Button3.Enabled = True
Button4.Enabled = True
Button7.Enabled = True
End If
Selected = False
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
Selecting = True
SelectPos = New Point(e.Location)
SelectSiz = New Size(1, 1)
Me.Refresh()
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If Selecting Then
SelectSiz = New Size(e.Location.X - SelectPos.X, e.Location.Y - SelectPos.Y)
Selected = True
Me.Refresh()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
Selecting = False
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
If Selected Then
e.Graphics.DrawRectangle(Pens.Black, GetSelection(SelectPos, SelectSiz))
End If
End Sub
Public Function GetSelection(ByVal Start As Point, ByVal Range As Size) As Rectangle
Dim X As Integer = Start.X
Dim X1 As Integer = Range.Width
If X1 < 0 Then
X1 = Math.Abs(X1)
X -= X1
End If
Dim Y As Integer = Start.Y
Dim Y1 As Integer = Range.Height
If Y1 < 0 Then
Y1 = Math.Abs(Y1)
Y -= Y1
End If
Return New Rectangle(X, Y, X1, Y1)
End Function
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim img As Image = Me.PictureBox1.Image
img.RotateFlip(RotateFlipType.Rotate90FlipNone)
Me.PictureBox1.Image = img
End Sub
Private Sub PictureBox1_MouseWhee(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseWheel
If e.Delta <> 0 Then
If e.Delta <= 0 Then
If PictureBox1.Width < 500 Then Exit Sub 'minimum 500?
Else
If PictureBox1.Width > 2000 Then Exit Sub 'maximum 2000?
End If
PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)
PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)
End If
End Sub
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
PictureBox1.Focus()
End Sub
End Class
提前感谢
终于我做到了,谢谢大家这是代码
Me.Opacity = 0%
Me.PictureBox1.Image = cc()
Dim filename As String = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
Dim filePath = (IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filename)))
PictureBox1.Image.Save(filePath)
source = Image.FromFile(filePath)
PictureBox1.Image = source
Me.Opacity = 100%
Catch ex As Exception
MsgBox(ex.Message)
End Try