Hi Rod,
Here is my entire code below. Everything is associated with a single form. It appears to be working perfectly through section #4. I think the problem is in #5 or #6. I do not know how to Send you the whole project. If you would tell me, that may make it easier for you to see what I have done.
Gary
Public Class Form1
Private Sub btnSelectPicture_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectPicture.Click
'================================================= ==========
'#1 'Opens Open Dialog Box and Loads Bitmap
'================================================= ==========
'Show the Open Dialog Box.
If ofdSelectPicture.ShowDialog = DialogResult.OK Then
'Load the picture into the picture box.
Dim bm_source As New Bitmap(ofdSelectPicture.FileName)
'================================================= =====
'#2 'CHANGES IMAGE TO BLACK AND WHITE
'================================================= =====
For y As Integer = 0 To bm_source.Height - 1
For x As Integer = 0 To bm_source.Width - 1
Dim clr As Color = bm_source.GetPixel(x, y)
If (CInt(clr.R) + clr.G + clr.B) > 400 Then
bm_source.SetPixel(x, y, Color.FromArgb(255, 255, 255, 255))
Else
bm_source.SetPixel(x, y, Color.FromArgb(255, 0, 0, 0))
End If
Next x
Next y
'================================================= =========
'#3 'Resizes the Bitmap to 640 pixels by 900 pixels
'================================================= =========
'Resize bitmap
Dim bm_dest As New Bitmap(640, 900)
'Make a Graphics objct for the result Bitmap
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
'Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, 640, 900)
'Display the result
picShowPIcture.Image = bm_dest
'Show the name of the file in the form's caption.
Me.Text = "Picture View(" & ofdSelectPicture.FileName & ")"
End If
' txtSaveAs.Enabled = True
btnSelectPicture.Enabled = True
' ================================================== ===================
'#4 'Clones a 10 x 10 pixel area as a New Bitmap Image
'================================================= =====================
'Increase strTop number by ten each cycle
Dim strRow As Integer
strRow = strRow + 10
'Start Cloning
Dim strTop1 As Integer
strTop1 = strRow
Dim strLeft1 As Integer
strLeft1 = 1
Dim strWidth1 As Integer
strWidth1 = 10
Dim strHeight1 As Integer
strHeight1 = 10
'Copy part of the orgininal image
Dim top1 As Integer = Integer.Parse(strTop1)
Dim left1 As Integer = Integer.Parse(strLeft1)
Dim width1 As Integer = Integer.Parse(strWidth1)
Dim Height1 As Integer = Integer.Parse(strHeight1)
'Make a Bitmap to hold the result
Dim bm1 As New Bitmap(width1, Height1)
'Associate a Graphics object with the Bitmap
Using gr As Graphics = Graphics.FromImage(bm1)
'Define source and destination rectangles
Dim src_rect As New Rectangle(left1, top1, width1, Height1)
Dim dst_rect As New Rectangle(0, 0, width1, Height1)
'Copy that part of the image
gr.DrawImage(picShowPIcture.Image, dst_rect, src_rect, GraphicsUnit.Pixel)
End Using
'Display the result
pbClone.Image = bm1
End Sub
'================================================= =========================================
' #5 ' Return the number of matching pixels.
'================================================= =========================================
Private Function CountPixels(ByVal bm As Bitmap, ByVal target_color As Color) As Integer
' Loop through the pixels.
Dim matches As Integer = 0
For y As Integer = 0 To bm.Height - 1
For x As Integer = 0 To bm.Width - 1
If (bm.GetPixel(x, y).Equals(target_color)) Then matches += 1
Next x
Next y
Return matches
End Function
'================================================= ====================================
'Compare number of black pixels to the number of white pixels and if Black Pixels Count is
'#6 ' more than White Pixel Count -- Place a one in txtTextBox1
'================================================= =====================================
Private Sub btnCountPixels_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim bm As New Bitmap(pbClone.Image)
txtBlack.Text = CountPixels(bm, Color.FromArgb(255, 0, 0, 0))
txtWhite.Text = CountPixels(bm, Color.FromArgb(255, 255, 255, 255))
If txtBlack.Text > txtWhite.Text Then
txtTextBox1.Text = 1
Else : txtTextBox1.Text = 0
End If
'================================================= =================================
'Write number from txtTextBox1 to selected text file using Append. In the actual
'#7 'Application this should produce a 64 digit number.
'================================================= ==================================
Dim strSaveAs As String
strSaveAs = txtSaveAs.Text
Dim strTextBox1 As String
strTextBox1 = txtTextBox1.Text
Dim objFile As New System.IO.StreamWriter("C:\WaterWriter\Digital-Images\" & (strSaveAs) & ".txt")
objFile.Write(strTextBox1)
objFile.Close()
End Sub
End Class
|