Wrox Programmer Forums
BOOK: Visual Basic 2010 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2010 Programmer's Reference by Rod Stephens; ISBN: 9780470499832
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2010 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 16th, 2012, 07:06 PM
Authorized User
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts

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.


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)

'================================================= =====
'================================================= =====

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))
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")

End Sub
End Class
Old February 17th, 2012, 12:24 PM
Rod Stephens's Avatar
Wrox Author
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts

Offhand I don't see anything wrong but it'll be easier to figure out in the debugger. To send the whole project:

- Delete the bin and obj directories. VB will recreate them before it executes the program and they're fairly big so deleting them will save space.

- Go to the directory above the project's directory. Right-click the project directory and select Send To > Compressed (zipped) Folder.

Email me the zip file at RodStephens@vb-helper.com and I'll take a look.

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Page Color darkestangel1980 CSS Cascading Style Sheets 6 February 6th, 2008 12:40 PM
color changing windows casemaker Oracle 0 May 28th, 2006 08:29 PM
changing pixel color in if/then construct rharris VB.NET 2002/2003 Basics 1 June 19th, 2005 09:12 AM
Changing color conny Visual C++ 0 April 14th, 2005 07:57 PM
Need help changing color on text, please larry Javascript How-To 1 December 23rd, 2003 10:24 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.