这个代码有什么问题,可以';在vb.net2005的pictureboxx1中裁剪图像?

这个代码有什么问题,可以';在vb.net2005的pictureboxx1中裁剪图像?,vb.net,image,Vb.net,Image,我正在创建一个在picturebox1中裁剪图像的应用程序。 它有两个按钮:- Button1=捕获桌面并将屏幕截图放入picturebox1。 有了这个代码 Imports System.Net.Mail Public Class Form1 Private SelectSiz As Size Private SelectPos As System.Drawing.Point Private Selecting As Boolean = False Priva

我正在创建一个在picturebox1中裁剪图像的应用程序。 它有两个按钮:- Button1=捕获桌面并将屏幕截图放入picturebox1。 有了这个代码

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
但现在的问题是,当我选择一个区域将其裁剪到图像上,然后按按钮2进行裁剪时,会出现以下错误:- {“值不能为空。参数名称:image”}

这是我的应用程序的图片

这就是全部代码

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
导入System.Net.Mail
公开课表格1
私人选择Siz作为大小
私人选择POS作为System.Drawing.Point
私有选择为布尔值=False
选择私有为布尔值=False
作为映像的私有源
作为整数的Dim XOffs
作为整数的Dim YOffs
私有函数cc()作为位图
变暗s为屏幕=Screen.PrimaryScreen
将img设置为新位图(s.Bounds.Width、s.Bounds.Height)
作为图形的Dim gr=图形。来自图像(img)
gr.CopyFromScreen(s.Bounds.Location、Point.Empty、s.Bounds.Size)
返回img
端函数
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
Me.PictureBox1.Image=cc()
端接头
私有子按钮2\u单击\u 1(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮6。单击
如果选中,则
将尺寸裁剪为图像=新位图(Math.Abs(SelectSiz.Width)、Math.Abs(SelectSiz.Height))
尺寸g为Graphics=Graphics.FromImage(裁剪)
Dim SrcRect As Rectangle=GetSelection(SelectPos,SelectSiz)
srrect.X+=XOffs
srrect.Y+=YOffs
g、 DrawImage(源,新矩形(0,0,裁剪.宽度,裁剪.高度),SrcRect,GraphicsUnit.Pixel)
PictureBox1.Image=裁剪
按钮3.Enabled=True
按钮4.Enabled=True
按钮7.Enabled=True
如果结束
所选=错误
端接头
私有子PictureBox1\u MouseDown(ByVal sender作为对象,ByVal e作为MouseEventArgs)处理PictureBox1.MouseDown
选择=真
选择POS=新点(如位置)
SelectSiz=新尺寸(1,1)
我
端接头
私有子PictureBox1\u MouseMove(ByVal sender作为对象,ByVal e作为MouseEventArgs)处理PictureBox1.MouseMove
如果选择则
SelectSiz=新尺寸(e.Location.X-SelectPos.X,e.Location.Y-SelectPos.Y)
所选=真
我
如果结束
端接头
私有子PictureBox1\u MouseUp(ByVal sender作为对象,ByVal e作为MouseEventArgs)处理PictureBox1.MouseUp
选择=假
端接头
私有子PictureBox1_Paint(ByVal sender作为对象,ByVal e作为PaintEventArgs)处理PictureBox1.Paint
如果选中,则
e、 图形.绘图矩形(画笔.黑色,GetSelection(SelectPos,SelectSiz))
如果结束
端接头
公共函数GetSelection(ByVal起点为点,ByVal范围为大小)为矩形
尺寸X为整数=开始.X
尺寸X1为整数=范围宽度
如果X1<0,则
X1=数学绝对值(X1)
X-=X1
如果结束
将Y调整为整数=开始.Y
尺寸Y1为整数=范围高度
如果Y1<0,则
Y1=数学绝对值(Y1)
Y-=Y1
如果结束
返回新矩形(X,Y,X1,Y1)
端函数
私有子按钮4\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮4。单击
将img调暗为Image=Me.PictureBox1.Image
img.RotateFlip(RotateFlipType.Rotate90FlipNone)
Me.PictureBox1.Image=img
端接头
私有子PictureBox1\u鼠标Whee(ByVal发送方作为System.Object,ByVal e作为MouseEventArgs)处理PictureBox1.MouseWheel
如果e.Delta为0,则
如果e.Delta我终于做到了,
谢谢大家
这是密码

 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

“源”从来没有分配给任何东西,所以它总是
什么都没有
。那么如何分配它呢?因为我是vb.net的初学者