I'm hoping some kind soul can help me with this difficult problem. I'm using VB.Net, Visual Studio 2008 making a Windows Forms application. I have a FOR NEXT loop (actually one nexted inside the other) which scans across a bitmap image and retrieves data from the image, pixel by pixel. This data then needs to be stored into a series of arrays. At this stage I have got it all working using a structure section, but am trying to replace that. I also need the array data to be available publicly across the project.
I have removed the Structure section and instead declared the arrays as public arrays the size that there are pixels. Now, I cannot seem to place data into the array slots. Can anyone help with highlighting the error? I suspected it was because the loop might be going from pixel 1 to 9...
Secondly, It would be extremely helpful if somebody could show me how to adapt the code so that I can save to a 2-dimensional array (rows,columns).
Thirdly, I need to display the contents of the array on the form and allow the user to change values, instantly updating the array. Can I do this with a datagrid?
Also any obvious glitches please let me know.
Code follows below:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Bitmap
Imports System.Drawing.Imaging.Metafile
Imports Microsoft.VisualBasic
Public Class Imagereader
Public strFileName As String
Public image1 As Bitmap
Public DidWork, pixels As Integer
Public pixelColor As Color
Protected pixa(pixels - 1), pixr(), pixg(), pixb(), pixgrey(), lux(), Y(), U(), V(), pixcount As Integer
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFD.InitialDirectory = "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\Hex_Lux Calculator\RGB tests"
OpenFD.Title = "Open a Bitmap File"
OpenFD.FileName = ""
OpenFD.Filter = "Bitmap Files|*.bmp"
DidWork = OpenFD.ShowDialog()
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = OpenFD.FileName
' Retrieve the image.
image1 = New Bitmap(strFileName, True)
PictureBox1.Image = image1
' Display the pixel format in Label1.
Label2.Text = "Pixel format: " + image1.PixelFormat.ToString()
' Loop through the images pixels to reset color.
pixels = image1.Width * image1.Height
pixcount = 0
MsgBox("number of pixels in the image: " & (pixels))
For col = 1 To (image1.Height)
For row = 1 To (image1.Width)
' count status - used for checking through the 2 loops
' MsgBox("y: " & (y) & " x: " & (x))
pixelColor = image1.GetPixel(row, col)
pixa(pixcount) = pixelColor.A
'pixr(pixcount) = pixelColor.R
'pixg(pixcount) = pixelColor.G
'pixb(pixcount) = pixelColor.B
'pixgrey(pixcount) = ((Int(pixelColor.R) + Int(pixelColor.G) + Int(pixelColor.B)) / 3)
'Y(pixcount) = (0.299 * pixr(pixcount) + (0.587 * pixg(pixcount)) + (0.114 * pixb(pixcount)))
'U(pixcount) = Math.Round(0.492 * (pixb(pixcount) - Y(pixcount)))
'V(pixcount) = Math.Round(0.877 * (pixr(pixcount) - Y(pixcount)))
'lux(pixcount) = ((pixgrey(pixcount) / 255) * 100)
'image1.SetPixel(row, col, Color.FromArgb(pixa(pixcount), pixr(pixcount), pixg(pixcount), pixb(pixcount)))
'set conditions - find the red pixel and colour it white
'If image1.GetPixel(x, y) = Color.FromArgb(255, 255, 0, 0) Then
' image1.SetPixel(x, y, Color.White)
'End If
pixcount = pixcount + 1
Next
Next
'MsgBox("Pixel count: " & (pixcount))
PictureBox2.Image = image1
End If
End Sub
-------------
Best regards,
Matthew Bennett
Btw the commented out sections are old. The loop variables used to be 'x' and 'y', but are now 'row' and 'col'.
Edited by Vswe, 07 March 2010 - 02:43 PM.
Don't double post, edit instead. Also use code tags for codes


Sign In
Create Account

Back to top









