Back
  1.     Shared Function ByteSignalImage(b As Byte) As Image
  2.         Dim values As String = Convert.ToString(b, 2).PadLeft(8, "0"c)
  3.         Dim imageWidth As Integer = 400
  4.         Dim bitSize As Integer = imageWidth / 10
  5.         Dim imageHeight As Integer = bitSize * 3
  6.         Dim bm As New System.Drawing.Bitmap(imageWidth, imageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
  7.         Dim middleleft As New Point(0, (imageHeight / 2))
  8.         Dim middleright As New Point(imageWidth, (imageHeight / 2))
  9.         Dim place As Integer = 1
  10.         For i As Integer = 1 To values.Count - 1
  11.             place = place * 2
  12.         Next
  13.         Using g As Graphics = Graphics.FromImage(bm)
  14.             g.Clear(Color.FromArgb(255, 179, 255, 240))
  15.             g.DrawLine(Pens.Black, middleleft, middleright)
  16.             Dim lastX As Integer = bitSize
  17.             Dim count As Integer = 0
  18.             Dim y As Integer = 22
  19.             Dim val As Integer = 0
  20.             Dim lastval As Integer = 0
  21.             Dim redpen As New Pen(New SolidBrush(Color.Red), 2)
  22.             Dim greenPen As New Pen(New SolidBrush(Color.Green), 1) With {.DashPattern = {2.0!, 2.0!}}
  23.             Dim y1 As Integer = 0
  24.             Dim y2 As Integer = imageHeight
  25.             For x As Integer = 0 To imageWidth Step bitSize
  26.                 g.DrawLine(greenPen, New Point(x, y1), New Point(x, y2))
  27.             Next
  28.             g.DrawLine(greenPen, New Point(imageWidth - 1, y1), New Point(imageWidth - 1, y2))
  29.             Dim x1 As Integer = 0
  30.             Dim x2 As Integer = imageWidth
  31.             For y = 0 To imageHeight Step bitSize
  32.                 g.DrawLine(greenPen, New Point(x1, y), New PointF(x2, y))
  33.             Next
  34.             g.DrawLine(greenPen, New Point(x1, imageHeight - 1), New Point(x2, imageHeight - 1))
  35.  
  36.             For x As Integer = (bitSize * 2) To imageWidth - bitSize Step bitSize
  37.                 Dim prevValue As Integer = 0
  38.                 Try
  39.                     If CInt(CStr(values(count))) = 0 Then
  40.                         y = (imageHeight / 2)
  41.                         lastval = val
  42.                         val = 0
  43.                     Else
  44.                         y = (imageHeight / 2) - bitSize
  45.                         lastval = val
  46.                         val = 1
  47.                     End If
  48.                 Catch ex As Exception
  49.                 End Try
  50.                 Dim pt1 As New Point(lastX, y)
  51.                 Dim pt2 As New Point(x, y)
  52.                 Dim pt3 As New Point(lastX, (imageHeight / 2) - bitSize)
  53.                 Dim pt4 As New Point(lastX, (imageHeight / 2))
  54.                 Dim ctr As Integer = lastX + ((x - lastX) / 2)
  55.                 Dim fontsize As Integer = (bitSize / 4)
  56.                 Dim font As New Font("consolas", fontsize, FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
  57.                 Dim textsizef As SizeF = g.MeasureString(val.ToString(), font)
  58.                 Dim textsize As New Size(CInt(textsizef.Width), CInt(textsizef.Height))
  59.                 Dim textLeft As Integer = ctr - (textsize.Width / 2)
  60.                 Dim textTop As Integer = y - textsize.Height - 2
  61.                 Dim placeString As String = place.ToString & "'s"
  62.                 Dim placesizef As SizeF = g.MeasureString(placeString.ToString(), font)
  63.                 Dim placesize As New Size(CInt(placesizef.Width), CInt(placesizef.Height))
  64.                 Dim placeLeft As Integer = ctr - (placesize.Width / 2)
  65.                 Dim placeTop As Integer = (imageHeight / 2) + 5
  66.                 g.DrawLine(redpen, pt1, pt2)
  67.                 g.DrawString(val.ToString, font, Brushes.Black, New PointF(textLeft, textTop))
  68.                 g.DrawString(placeString.ToString, font, Brushes.Black, New PointF(placeLeft, placeTop))
  69.                 If Not (val = lastval) Then
  70.                     g.DrawLine(redpen, pt3, pt4)
  71.                 End If
  72.                 If count = 7 Then
  73.                     If val = 1 Then
  74.                         Dim pt5 As New Point(x, (imageHeight / 2) - bitSize)
  75.                         Dim pt6 As New Point(x, (imageHeight / 2))
  76.                         g.DrawLine(redpen, pt5, pt6)
  77.                     End If
  78.                 End If
  79.                 count = count + 1
  80.                 lastX = x
  81.                 place = place / 2
  82.             Next
  83.         End Using
  84.         Return bm
  85.     End Function