Why is this not showing any results in PictureBox? - vb.net

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.

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.

(VB) Spawning multiple enemies

I'm currently making an arcade shooting game in Visual Basic which spawns enemies at the top of the form which move vertically downward toward the player. My current code spawns the enemy, but any attempt to add another 'enemyShip' to the 'enemyShips' array fails and hence, only a single enemy is spawned. Any help as to how to spawn multiple enemies would be appreciated. My current code is below:
Dim enemySize As Integer = 32
Dim enemySpawn As New Point(150, 0)
Dim enemyShip As New Rectangle(150, 0, enemySize, enemySize)
Dim enemyLoc As New Point(enemyShip.Location)
Dim enemySpr As Image = My.Resources.sprEnemy32x32
Dim enemySpeed As Integer = 5
Dim enemyShips(-1) As Rectangle
Dim intCount As Integer = 0
Dim g, bbg As Graphics
Dim backBuff As Bitmap
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
g = Me.CreateGraphics
backBuff = New Bitmap(300, 300, Imaging.PixelFormat.Format32bppPArgb)
bbg = Graphics.FromImage(backBuff)
tmrSpawn.Enabled = True
tmrRender.Enabled = True
End Sub
Private Sub tmrSpawn_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSpawn.Tick
SpawnEnemy()
End Sub
Private Sub SpawnEnemy()
'Add enemyShip to index in array enemyShips
'Add 1 to enemyShip's index so new rectangle is stored in the next index
ReDim Preserve enemyShips(intCount)
enemyShips(intCount) = enemyShip
intCount += 1
'Move newly created enemyShip vertically downward on the form
For Each Me.enemyShip In enemyShips
enemyLoc = New Point(enemyShip.Location.X, enemyShip.Location.Y + enemySpeed)
enemyShip.Location = enemyLoc
Next
End Sub
Private Sub tmrRender_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRender.Tick
bbg.DrawImage(enemySpr, enemyShip)
g.DrawImage(backBuff, 0, 0)
bbg.Clear(Color.Gray)
End Sub

How to use timespan axes in bar chart in 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

How to draw points when user clicks on a map

Dim HaveToDraw As New Boolean
Dim xMouse As Integer
Dim yMouse As Integer
Private Sub foo(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If HaveToDraw = True Then
e.Graphics.FillEllipse(Brushes.Green, xMouse, yMouse, 10, 10)
End If
HaveToDraw = False
End Sub
Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
If RadioButton1.Checked = True Then
xMouse = e.X
yMouse = e.Y
HaveToDraw = True
PictureBox1.Refresh()
End If
End Sub
This code lets the user draw an elipse when he clicks on any point on the map, but there are 2 problems with it: 1 - The user is able to draw only one elipse; 2 - The user is not able to erase a previously created elipse.
So, how can I do that solving these two problems?
As #Idle_Mind suggested, you could use a list to store your points, and a right-click event to remove the points:
Dim radius as Integer = 5
Private points As New List(Of Point)()
Private Sub pictureBox1_MouseClick(sender As Object, e As MouseEventArgs)
If e.Button = System.Windows.Forms.MouseButtons.Left Then
points.Add(e.Location) ' add point on left click
ElseIf e.Button = System.Windows.Forms.MouseButtons.Right Then
For i As Integer = 0 To points.Count - 1 ' remove points on right-click
If distance(points(i).X, points(i).Y, e.Location) < radius Then
points.RemoveAt(i)
End If
Next
End If
pictureBox1.Refresh()
End Sub
'helper function
Private Function distance(x__1 As Integer, y__2 As Integer, mousep As Point) As Integer
Dim X__3 As Integer = CInt(Math.Pow(CDbl(x__1 - mousep.X), 2))
Dim Y__4 As Integer = CInt(Math.Pow(CDbl(y__2 - mousep.Y), 2))
Return CInt(Math.Sqrt(CDbl(X__3 + Y__4)))
End Function
Private Sub pictureBox1_Paint(sender As Object, e As PaintEventArgs)
For i As Integer = 0 To points.Count - 1
e.Graphics.FillEllipse(Brushes.Green, points(i).X - radius, points(i).Y - radius, radius * 2, radius * 2)
Next
End Sub
I also changed the paint code to draw the circles so that they are centered under the mouse-click.