How to use timespan axes in bar chart in vb.net? - vb.net

i want to use y axes as timespan and x axes as interger. i used zedgraph control.
Imports ZedGraph
Public Class Form6
Dim frames As Integer
Dim time1 As TimeSpan()
Public Sub New(ByVal no_frame As Integer, ByVal time() As TimeSpan)
InitializeComponent()
frames = no_frame
time1 = time
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateGraph(ZedGraphControl1)
SetSize()
End Sub
Private Sub CreateGraph(ByVal zgc As ZedGraphControl)
Dim myPane As GraphPane = zgc.GraphPane
myPane.Title.Text = "Waste of Bandwidth in Stop and Wait Protocol"
myPane.XAxis.Title.Text = "No of Frames"
myPane.YAxis.Title.Text = "Time"
myPane.YAxis.Type = AxisType.Date
' Make up some data points from the Sine function
Dim list = New PointPairList()
Dim x As Integer
Dim y(100) As TimeSpan
For x = 1 To frames
y(x) = time1(x)
list.Add(x, y(x))
Next x
' Generate a blue curve with circle symbols, and "My Curve 2" in the legend
Dim myCurve As LineItem = myPane.AddCurve("My Curve", list, Color.Blue, SymbolType.Circle)
' Fill the area under the curve with a white-red gradient at 45 degrees
myCurve.Line.Fill = New Fill(Color.White, Color.Red, 45.0F)
' Make the symbols opaque by filling them with white
myCurve.Symbol.Fill = New Fill(Color.White)
' Fill the axis background with a color gradient
myPane.Chart.Fill = New Fill(Color.White, Color.LightGoldenrodYellow, 45.0F)
' Fill the pane background with a color gradient
myPane.Fill = New Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0F)
' Calculate the Axis Scale Ranges
zgc.AxisChange()
End Sub
Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
SetSize()
End Sub
Private Sub SetSize()
ZedGraphControl1.Location = New Point(10, 10)
' Leave a small margin around the outside of the control
ZedGraphControl1.Size = New Size(ClientRectangle.Width - 20, ClientRectangle.Height - 20)
End Sub
End Class
error for the above code
Public Sub Add(x As Double(), y As Double())':
Argument matching parameter 'x' cannot convert from 'Integer' to 'Double()'.
Argument matching parameter 'y' cannot convert from 'TimeSpan' to 'Double()'.
'Public Overrides Sub Add(x As Double, y As Double)':
Argument matching parameter 'y' cannot convert from 'TimeSpan' to 'Double'.
what is the solution for this ?
is there any other graph or bar chart control in vb.net ? if yes then how to use bar chart in vb.net for timespan axes ? (integer) should be x axes and (Timespan) should be y axes. please help me.

i sloved by myself... thank u all..
Imports ZedGraph
Public Class Form6
Dim frames As Integer
Dim time1 As TimeSpan()
Dim str As String
Public Sub New(ByVal no_frame As Integer, ByVal time() As TimeSpan)
InitializeComponent()
frames = no_frame
time1 = time
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateGraph(ZedGraphControl1)
SetSize()
End Sub
Private Sub CreateGraph(ByVal zgc As ZedGraphControl)
Dim myPane As GraphPane = zgc.GraphPane
myPane.Title.Text = "Waste of Bandwidth in Stop and Wait Protocol"
myPane.XAxis.Title.Text = "No of Frames"
myPane.YAxis.Title.Text = "Time"
'myPane.YAxis.Type = AxisType.Date
'myPane.YAxis.Scale.Format = "HH:mm:ss:fff"
' Make up some data points from the Sine function
Dim list = New PointPairList()
Dim x As Double
Dim y(100) As Double
For x = 1 To frames
y(x) = (Convert.ToDouble(time1(x).TotalSeconds))
list.Add(x, y(x))
Next x
' Generate a blue curve with circle symbols, and "My Curve 2" in the legend
Dim myCurve As LineItem = myPane.AddCurve("My Curve", list, Color.Blue, SymbolType.Circle)
' Fill the area under the curve with a white-red gradient at 45 degrees
myCurve.Line.Fill = New Fill(Color.White, Color.Red, 45.0F)
' Make the symbols opaque by filling them with white
myCurve.Symbol.Fill = New Fill(Color.White)
' Fill the axis background with a color gradient
myPane.Chart.Fill = New Fill(Color.White, Color.LightGoldenrodYellow, 45.0F)
' Fill the pane background with a color gradient
myPane.Fill = New Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0F)
' Calculate the Axis Scale Ranges
zgc.AxisChange()
End Sub
Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
SetSize()
End Sub
Private Sub SetSize()
ZedGraphControl1.Location = New Point(10, 10)
' Leave a small margin around the outside of the control
ZedGraphControl1.Size = New Size(ClientRectangle.Width - 20, ClientRectangle.Height - 20)
End Sub
End Class

Related

vb.net avoid zoom in picturebox when drawing line(using XNA) in maximized form

I'm drawing lines in a picturebox inside a form, when I maximize the form the picturebox change height and width automatically because of anchor bounds.
Problem is that the lines are rendered in wrong way on the maximized window,and lines that should be 1 pixel width seem bigger. I'm missing some zoom proprety in picturebox control?
There is a way to avoid that?
I'm using XNA 4.0, here the basic code where pbGame is my picturebox.
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Private quit As Boolean = False
Public grafix As GraphicsDevice
Private Function initialize(ByRef surface As PictureBox) As Boolean
Try
Dim pparam As New PresentationParameters
pparam.DeviceWindowHandle = surface.Handle
pparam.IsFullScreen = False
Dim grafixAdapt As GraphicsAdapter = GraphicsAdapter.DefaultAdapter
grafix = New GraphicsDevice(grafixAdapt, GraphicsProfile.HiDef, pparam)
initialize = True
Catch ex As Exception
initialize = False
End Try
End Function
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set up the initialize function found above
If InitializeGraphics(pbGame) AndAlso InitializeEffect(grafix) Then
BackgroundWorker1.RunWorkerAsync()
Else
MessageBox.Show("There was a problem initializing XNA.")
Me.Close()
End If
End Sub
Private effect As BasicEffect
Private Function InitializeEffect(ByVal graphics As GraphicsDevice) As Boolean
effect = New BasicEffect(graphics)
Try
effect.VertexColorEnabled = True
effect.Projection = Matrix.CreateOrthographicOffCenter(0, graphics.Viewport.Width, graphics.Viewport.Height, 0, 0, 1)
InitializeEffect = True
Catch ex As Exception
InitializeEffect = False
End Try
End Function
Private Function Set2dLine(ByVal x1 As Integer, ByVal y1 As Integer, ByVal z1 As Integer, _
ByVal x2 As Integer, ByVal y2 As Integer, ByVal z2 As Integer, _
ByVal color As Color) As VertexPositionColor()
Dim vertices1, vertices2 As New VertexPositionColor
vertices1.Position = New Vector3(x1, y1, z1)
vertices1.Color = color
vertices2.Position = New Vector3(x2, y2, z2)
vertices2.Color = color
Return {vertices1, vertices2}
End Function
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Do Until quit = True
grafix.Clear(Color.CornflowerBlue)
effect.CurrentTechnique.Passes(0).Apply()
Dim newline() As VertexPositionColor = Set2dLine(50, 10, 0, 150, 10, 0, Color.Black)
grafix.DrawUserPrimitives(PrimitiveType.LineList, newline, 0, 1)
grafix.Present()
Loop
End Sub

Type Text Directly On A Bitmap Image at Mouse Position

I am trying to write (type) directly onto a bitmap. I need to be able to type at the mouse position, so where ever on the screen i click the mouse, I can start typing text with the keyboard.
Here is a working VS 2017 VB Win Form code that will print "Hello World" at the mousedown position. But it only works with predetermined text. I would like to be able to just type at that spot. I feel I am so close, just can't get it to work.
Imports System.IO
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Dim WithEvents Chart1 As New Chart
Private Structure TextPoints
Dim MPos As Point
Dim Txt As String
End Structure
Private TextList As New List(Of TextPoints)
Private TempPoint As Point
Private FirstPoint As Point
Dim xcnt As Integer = -1
Dim ycnt As Integer = -1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Size = New Size(1100, 700)
Me.Location = New Point(10, 10)
MakeBackImage()
With Chart1
.Name = "Chart1"
.Location = New System.Drawing.Point(40, 40)
.Size = New System.Drawing.Size(1010, 610)
.BackImage = "BackImg.jpg"
.Parent = Me
End With
End Sub
Private Sub Chart1_MouseDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Chart1.MouseDown
FirstPoint = New Point(e.X, e.Y)
TempPoint = New Point(e.X, e.Y)
Me.Refresh()
End Sub
Private Sub Chart1_MouseUp(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Chart1.MouseUp
Dim T As New TextPoints With {
.MPos = TempPoint,
.Txt = "Hello World"}
TextList.Add(T)
Me.Refresh()
End Sub
Private Sub MakeBackImage()
Dim x, y As Integer
Dim img As Image = New Bitmap(1020, 620)
Dim graphics As Graphics = Graphics.FromImage(img)
graphics.Clear(Drawing.Color.White)
For x = 0 To 1000 Step 20
graphics.DrawLine(Pens.Black, x, 0, x, 600)
xcnt += 1
Next
For y = 0 To 600 Step 20
ycnt += 1
graphics.DrawLine(Pens.Black, 0, y, 1000, y)
Next
img.Save("BackImg.jpg", Imaging.ImageFormat.Jpeg)
End Sub
Private Sub Chart1_Paint(ByVal sender As Object,
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Chart1.Paint
Dim drawString As String = "Hello World"
Dim drawFont As New Font("Arial", 14)
Dim drawBrush As New SolidBrush(Color.Black)
For Each t As TextPoints In TextList
e.Graphics.DrawString(t.Txt, drawFont,
drawBrush, t.MPos.X, t.MPos.Y)
Next
End Sub
End Class
This is a simplified code. Actually, the background image is only created once, but I added code to dynamically create it here to make the demo better.

Need help randomizing a list and posting it as a position on my form

I'm a high school student trying to learn VB.Net programming. I've been trying to get a whack-a-mole game going. I have been trying to make a list of positions for a button, and somehow retrieving the integers and posting it as a position on my app. I've been experimenting with random number generators but I can't catch how to retrieve numbers off a list and exchanging them into positions.
Here's the code:
Public Class Form1
Dim red, blue, green, yellow
Dim ListX As New List(Of Integer)
Dim ListY As New List(Of Integer)(New Integer() {400, 350})
Private Sub btnstart_Click(sender As Object, e As EventArgs) Handles btnstart.Click
For pos = 1 To 7
PictureBox3.Location = New Point(73, 398)
Next pos
End Sub
Sub Main()
Dim ListX As New List(Of Integer)
Dim ListY As New List(Of Integer)(New Integer() {400, 350})
End Sub
Private Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click
End Sub
Subaz's code simplified:
Private Function GetLocation() As Point
Dim rn As New Random
Return New Point(rn.Next(400), rn.Next(350))
End Function
This is the code I wrote:
Public Class Form37
Private Sub btnstart_Click(sender As System.Object, e As System.EventArgs) Handles btnstart.Click
'set the location of picturebox randomly
PictureBox3.Location = GetLocation()
End Sub
Private Function GetLocation() As Point
'get a random value between 0 to 400 for x and 0 to 350 for y
Dim x_ As Integer
Dim y_ As Integer
Randomize()
x_ = CInt(Int((400 * Rnd()) + 1))
y_ = CInt(Int((350 * Rnd()) + 1))
'set those x and y as point
Dim Loc As Point = New Point(x_, y_)
'return location from this function
Return Loc
End Function
Private Sub PictureBox3_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox3.Click
'// when cliked on picturebox,
'// increase score or whatever you want to do
'// show the picturebox in another place now
PictureBox3.Location = GetLocation()
End Sub
End Class
Explanation:
On clicking the button, the picturebox3 will relocate to a random point in the form.
On clicking the picturebox3, you can do coding where you can increase score or whatever and then the picturebox will again relocate to another random point.
Hope this helps

A Graphics object cannot be created from an image that has an indexed pixel format

I have a program that when you click on picturebox1, it transfers the image inside it to picturebox2. Then I have an interpolation code to modify it to a nearestneighbor pixel rendering. It also draws a pixel grid on picturebox2 to line up around the pixels. I have picturebox2 set to stretch image. I tried 2 different methods of drawing on the picturebox2. I am having a problem converting the image back to the right size to transfer it back to picturebox1 after its been edited with the paintbrush.
CODE:
Imports System.Windows.Forms
Imports System.Drawing
Imports System
Imports System.IO
Public Class Form1
Dim Brush = Brushes.Black
Dim COLOR1 As Color
Dim BMP As Bitmap
Dim Draw As Boolean
CODE: when you click on the picturebox1(topleft) it transfers its image to picturebox2(canvaseditor) does the grid draw, interpolate mode etc.
Private Sub topleft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles topleft.Click
tiledcanvas.BackgroundImage = topleft.Image
Label1.Text = "Top-Left"
'CANVAS PIXEL GRID CODE
If topleft.Image Is Nothing Then
Else
canvaseditor.Image = Nothing
canvaseditor.Image = topleft.Image
canvaseditor.Width = topleft.Width * 8 + 1
canvaseditor.Height = topleft.Height * 8 + 1
'load and draw the image(s) once
BackgroundImage1 = New Bitmap(topleft.Image)
bmpNew = New Bitmap(canvaseditor.Width * scaleFactor, canvaseditor.Height * scaleFactor)
Using g As Graphics = Graphics.FromImage(bmpNew)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half
g.DrawImage(BackgroundImage1, 0, 0, bmpNew.Width, bmpNew.Height)
End Using
canvaseditor.Focus()
GroupBox13.Focus()
End If
End Sub
CODE: the paintbrush code, the mousedown, mousemove, and mouseup events on picturebox2(canvaseditor)
Private Sub canvaseditor_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles canvaseditor.MouseMove
If Draw = True Then
PaintBrush(e.X, e.Y)
Else
End If
'If down = True Then
'canvaseditor.CreateGraphics.FillRectangle(Brush, e.X, e.Y, 8, 8)
'End If
' LocalMousePosition = canvaseditor.PointToClient(Cursor.Position)
'Dim X As Integer
'Dim Y As Integer
'If LocalMousePosition.X > 0 And LocalMousePosition.X < 9 Then
'X = 1
' ElseIf LocalMousePosition.X > 8 And LocalMousePosition.X < 17 Then
'X = 2
'ElseIf LocalMousePosition.X > 16 And LocalMousePosition.X < 25 Then
'X = 3
'End If
'Label6.Text = (X & ", " & Y)
End Sub
Private Sub PaintBrush(ByVal X As Integer, ByVal Y As Integer)
Using g As Graphics = Graphics.FromImage(canvaseditor.Image)
g.FillRectangle(New SolidBrush(Color.Black), New Rectangle(X, Y, 6, 6))
End Using
canvaseditor.Refresh()
End Sub
Private Sub canvaseditor_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles canvaseditor.MouseDown
'down = True
'If down = True Then
'Dim NEWBMP As New Bitmap(topleft.Width, topleft.Height)
'Dim graph As Graphics = Graphics.FromImage(NEWBMP)
' graph.FillRectangle(Brush, e.X, e.Y, 8, 8)
'topleft.Image = NEWBMP
'End If
'down = True
'If down = True Then
'canvaseditor.CreateGraphics.FillRectangle(Brush, e.X, e.Y, 8, 8)
'End If
Draw = True
PaintBrush(e.X, e.Y)
End Sub
Private Sub canvaseditor_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles canvaseditor.MouseUp
Draw = False
End Sub
and heres the first paint sub i made:
Private Sub canvaseditor_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles canvaseditor.Paint
If Not bmpNew Is Nothing Then
e.Graphics.DrawImage(bmpNew, 0, 0)
End If
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.DimGray) '~~~ color of the lines
Dim x As Integer
Dim y As Integer
Dim intSpacing As Integer = 8 '~~~ spacing between adjacent lines
'~~~ Draw the horizontal lines
x = canvaseditor.Width
For y = 0 To canvaseditor.Height Step intSpacing
g.DrawLine(pn, New Point(0, y), New Point(x, y))
Next
'~~~ Draw the vertical lines
y = canvaseditor.Height
For x = 0 To canvaseditor.Width Step intSpacing
g.DrawLine(pn, New Point(x, 0), New Point(x, y))
Next
End Sub
hoping this is understandable so someone can point me in the right direction. thanks.

Why is this not showing any results in PictureBox?

I'm trying to change an image to black and white on a variable threshold for use in an ocr program. My problem is that I'm not seeing any results in the image that is supposedly processed. I do experience a small wait when rendering, so i am to assume that it is actually doing something.
Imports System.Object
Imports System.Drawing.Bitmap
Public Class Form1
Dim x As Integer, y As Integer
Dim imgx As Integer, imgy As Integer
Dim img As Bitmap
Dim thresh As Bitmap
Dim pixelColor As Color
Dim threshcolor As Color
Dim tempcolor As Color
Public Function getpixel(ByRef x As Integer, ByRef y As Integer) As Color
End Function
Public Sub find_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find_img.Click
open_img.ShowDialog()
img_dsp.Text = open_img.FileName()
img_loc.Text = open_img.FileName
img_dsp.ImageLocation = img_dsp.Text
End Sub
Public Sub find_img_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles find_img.LostFocus
img = (img_dsp.Image)
img_dsp.Refresh()
img_dsp.Text = open_img.FileName()
img_dsp.ImageLocation = img_dsp.Text
img = (img_dsp.Image)
img_dsp.Refresh()
End Sub
Public Sub render_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles render.Click
Dim myx As Integer
Dim myy As Integer
img_threshold.Image = img
thresh = img_threshold.Image
For myy = 1 To (img.Height - 1)
For myx = 1 To (img.Width - 1)
tempcolor = img.GetPixel(myx, myy)
If tempcolor.GetBrightness < NumericUpDown1.Value Then
thresh.SetPixel(x, y, Color.Black)
End If
If tempcolor.GetBrightness > NumericUpDown1.Value Then
thresh.SetPixel(x, y, Color.White)
End If
Next myx
Next myy
img_threshold.Image = thresh
img_threshold.Refresh()
End Sub
End Class
Do you know what a reference is? writing A = B, and changing A and writing B = A?
if you specify that A and B point the same object (a reference to the same object) changing one changes the other too, they occupy the same storage in memory! Teach yourself basic, before writing programs, please.