当前位置: 代码迷 >> VB Dotnet >> 画图并显示到picturebox中出有关问题了
  详细解决方案

画图并显示到picturebox中出有关问题了

热度:153   发布时间:2016-04-25 02:19:16.0
画图并显示到picturebox中出问题了
本帖最后由 xiaolinyouni 于 2014-02-14 11:01:14 编辑
   Sub validatecode(ByVal vnum)
        Dim img As System.drawing.Bitmap
        Dim g As Graphics
        Dim r As Random = New Random
        Dim gheight As Integer = Int(Len(vnum) * 13)
        'gheight为图片宽度, 根据字符长度自动更改图片宽度

        img = New Bitmap(gheight, 20)
        g = Graphics.FromImage(img)
        g.DrawString(vnum, New System.Drawing.Font("Arial", 10), New System.Drawing.SolidBrush(Color.Blue), 3, 3)
        '新增,修改、画图片的背景噪音线

        For i As Integer = 0 To 10
            Dim x1 As Integer
            x1 = r.Next(img.Width)
            Dim x2 As Integer =r.Next(img.Width)
            Dim y1 As Integer =r.Next(img.Height)
            Dim y2 As Integer =r.Next(img.Height)
            g.DrawLine(New Pen(Color.Silver),x1, y1, x2, y2)
        Next i
        Dim font As Font
        font = New System.Drawing.Font("Arial", 12)
        Dim brush As System.Drawing.
        Drawing2D.LinearGradientBrush
        brush = New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, True)
        g.DrawString(vnum, font, brush, 2, 2)
        'VB.NET生成验证码中画图片的前景噪音点()

        For ii As Integer = 0 To 100
            Dim x As Integer = r.Next(img.Width)
            Dim y As Integer = r.Next(img.Height)
            img.SetPixel(x, y, Color.FromArgb(r.Next()))
        Next
        '画图片的边框线()

        g.DrawRectangle(New Pen(Color.Silver),0, 0, img.Width - 1,img.Height - 1)
        '在矩形内绘制字串(字串, 字体, 画笔颜色, 左上x.左上y)

        PictureBox1.Image = img
        PictureBox1.Refresh()
        g.Dispose()
        img.Dispose()

    End Sub


在最后那个picturebox1.image=img时报错,去掉这行,则通过.

未处理 System.ArgumentException
  Message=参数无效。
  Source=System.Drawing
  StackTrace:
       在 System.Drawing.Image.get_Width()
       在 System.Drawing.Image.get_Size()
       在 System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
       在 System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
       在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
       在 System.Windows.Forms.Control.WmPaint(Message& m)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)