|
-
Apr 14th, 2012, 02:38 AM
#1
Thread Starter
New Member
How to obtain every pixel from PicturebBx
This program using Visual Studio 2010, WIndows Form App
I want to create a program which need RGB value from an picture in PictureBox
But it has an error, NullReferenceException was not unhandled
The code :
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Dim height As Integer
Dim width As Integer
Dim i As Integer
Dim j As Integer
Dim Color As Color
Dim img As Bitmap = New System.Drawing.Bitmap(PictureBox1.Image)
height = CInt(PictureBox1.Image.Height.ToString)
width = CInt(PictureBox1.Image.Width.ToString)
For i = 0 To height
For j = 0 To width
Color = img.GetPixel(i, j) 'Get the RGB Value at pixel i,j
R(i)(j) = CInt(Color.R) 'Convert Red Value to Integer and put it into R
'G(i)(j) = CInt(Color.G.ToString) 'Convert Green Value to Integer and put it into G
'B(i)(j) = CInt(Color.B.ToString) 'Convert Blue Value to Integer and put it into B
'udah jadi mendapatkan rgb pas picbox diklik, ganti jadi semua pixel di gambar
j += 1
Exit For
Next
i += 1
Exit For
Next
End Sub
R(i)(j) = CInt(Color.R) <-- Here's the error
Please help thanks
-
Apr 14th, 2012, 05:26 AM
#2
Re: How to obtain every pixel from PicturebBx
Hi pandrew,
Welcome to the forum. It's not surprising you get that error because it seems you haven't declared an array called R. Don't confuse that with Color.R, which is a property of the Color type not an array.
The array needs to have the same size as the image, so you have to declare it after Dim img As Bitmap ....
I take it you already know how to declare a two-dimensional array. Remember that the indices start from 0, not from 1.
By the way - posts on this kind of topic often get lots of replies from people telling you there are more efficient ways to do it. There are, but I suggest you ignore them: learn the basics first!
BB
-
Apr 15th, 2012, 12:08 AM
#3
Thread Starter
New Member
Re: How to obtain every pixel from PicturebBx
 Originally Posted by boops boops
Hi pandrew,
Welcome to the forum. It's not surprising you get that error because it seems you haven't declared an array called R. Don't confuse that with Color.R, which is a property of the Color type not an array.
The array needs to have the same size as the image, so you have to declare it after Dim img As Bitmap ....
I take it you already know how to declare a two-dimensional array. Remember that the indices start from 0, not from 1.
By the way - posts on this kind of topic often get lots of replies from people telling you there are mbore efficient ways to do it. There are, but I suggest you ignore them: learn the basics first!
BB
thx boops boops...i agree, basics are the most important hahaha
I've declared R()() as an array, it's global variable
Dim R()() As Integer
Dim G()() As Integer
Dim B()() As Integer
i've tried declare it after Dim img As Bitmap.... but it's no good either
-
Apr 15th, 2012, 03:56 AM
#4
Re: How to obtain every pixel from PicturebBx
Using 2 pairs of brackets doesn't declare a 2-dimensional array but an "array of arrays". That's not the same thing. Here's how you declare a 2-d array:
Code:
Dim R(,) As Integer
But the job isn't done yet. You can't do anything with an array until its dimensions are set. That's why you get the error you got at runtime. If want to make a 20 * 30 array, you could declare it with dimensions like this:
Code:
Dim R(19, 29) As Integer
Note that in VB.Net, you must declare an array with the maximum index, which is 1 less than the size because it starts from 0. You can also declare the array and its dimensions in two steps like this:
Code:
Dim R(,) As Integer
ReDim R(19, 29)
And that's what you need in your code. Dim R(,) at global level as above. Then ReDim it once you know the size of the image in the MouseDown sub:
Code:
ReDim R(width - 1, height - 1)
I hope it's clear now that you must also correct the maximum index in your For statements. Otherwise you will get "index out of range" errors.
BB
-
Apr 29th, 2012, 12:51 AM
#5
Thread Starter
New Member
Re: How to obtain every pixel from PicturebBx
Okay....i got another error
when i run the program, i get IndexOutOfRangeException at redArray(i, j) = CInt(color.R)
this is the code
vb Code:
For i = 0 To img.Height - 1 For j = 0 To img.Width - 1 Dim color As Color = img.GetPixel(j, i) img2.SetPixel(j, i, color) redArray(i, j) = CInt(color.R) j += 1 Next i += 1 Next
i cant put the R value to 2dimensional array, do u know how to do it?
-
Apr 29th, 2012, 02:53 AM
#6
Re: How to obtain every pixel from PicturebBx
Did you use the Redim statement the way I suggested? Maybe I made it look as though they belong one after the other; that's not what I meant.
The Dim statement for the array belongs at Form level (outside any subs). The ReDim statement should go inside a sub, for example in the same sub as the For loop. That way the array will always be resized to match the image, so the code will still work if you change the image.
BB
-
Apr 29th, 2012, 09:19 AM
#7
Thread Starter
New Member
Re: How to obtain every pixel from PicturebBx
i Dim them at Form level, and ReDim them at my button code
here is my button codes
vb Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Dim i, j As Integer
Dim img As Bitmap = New System.Drawing.Bitmap(PictureBox1.Image)
'Dim img2 As Graphics = PictureBox2.CreateGraphics
'Dim pen As Pen
Dim img2 As Bitmap = New Bitmap(img.Width, img.Height)
ReDim redArray(img.Width, img.Height)
ReDim blueArray(img.Width, img.Height)
ReDim greenArray(img.Width, img.Height)
For i = 0 To img.Height - 1
For j = 0 To img.Width - 1
Dim color As Color = img.GetPixel(j, i)
img2.SetPixel(j, i, color)
redArray(i, j) = CInt(color.R)
If (j <= 400) Then
j += 1
End If
Next
i += 1
Next
PictureBox2.Image = img2
End Sub
and this is the error
Last edited by pandrew161; Apr 29th, 2012 at 09:23 AM.
-
Apr 29th, 2012, 09:34 AM
#8
Re: How to obtain every pixel from PicturebBx
You dimmed them backwards. The first element you used was the width, the second element was the height. However, when you perform the loop, the outer loop (i) is the height, while the inner loop (j) is the width. Just swap the i and j and it should work fine.
My usual boring signature: Nothing
 
-
Apr 29th, 2012, 11:05 AM
#9
Thread Starter
New Member
Re: How to obtain every pixel from PicturebBx
-
Apr 29th, 2012, 12:56 PM
#10
Thread Starter
New Member
Re: How to obtain every pixel from PicturebBx
more questions hehehe...
i put code to draw histogram for picturebox in Form2 at Form1's button...then Form2 will show and my histogram will be shown at picturebox in Form2, can it happen?
-
Apr 29th, 2012, 09:00 PM
#11
Re: How to obtain every pixel from PicturebBx
Sure, it's just a question of passing data from one form to a different form.
Here's a whole thread on the subject:
http://www.vbforums.com/showthread.php?t=466253
My usual boring signature: Nothing
 
-
May 1st, 2012, 08:54 AM
#12
Thread Starter
New Member
Re: How to obtain every pixel from PicturebBx
okay, i understand i can use setter and getter, but how to set the value? (although i dont use it now, but i think i should know ^^)
and how to get the value? is it by [public_property_name]([parameter_name]) ??
-
May 1st, 2012, 09:24 AM
#13
Re: How to obtain every pixel from PicturebBx
Just use the name of the "other" form as a reference. For example, suppose you have Form1 and Form2, and Form2 has a public String property called NickName. On Form1 you can write:
Code:
Dim str As String = Form2.NickName
BB
-
May 1st, 2012, 02:53 PM
#14
Re: How to obtain every pixel from PicturebBx
 Originally Posted by Shaggy Hiker
You dimmed them backwards. The first element you used was the width, the second element was the height. However, when you perform the loop, the outer loop (i) is the height, while the inner loop (j) is the width. Just swap the i and j and it should work fine.
As an addition to this, variable naming is important.
While 'i' and 'j' are common looping variables (nothing wrong with that) perhaps using 'x' and 'y' in this case would be better. 'x' would be the width and 'y' the height. The variable names hint at their usage and what they represent.
Meaningful variable names will solve a lot of bugs - by not having them occur in the first place.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
May 1st, 2012, 08:27 PM
#15
Thread Starter
New Member
Re: How to obtain every pixel from PicturebBx
i want to draw a histogram when Form2 is loaded, but i cant draw anything at all even a rectangle
i use the LOAD EVENT as the trigger..
-
May 2nd, 2012, 07:18 AM
#16
Re: How to obtain every pixel from PicturebBx
 Originally Posted by pandrew161
i want to draw a histogram when Form2 is loaded, but i cant draw anything at all even a rectangle
i use the LOAD EVENT as the trigger..
Read some of the threads regarding drawing. Drawing isn't complicated, but there are just a couple of things you need to know.
- Do all your drawing in the Paint (or OnPaint) events;
- Use the passed graphics object to do all your drawing against.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
May 2nd, 2012, 11:38 AM
#17
Thread Starter
New Member
Re: How to obtain every pixel from PicturebBx
 Originally Posted by SJWhiteley
Read some of the threads regarding drawing. Drawing isn't complicated, but there are just a couple of things you need to know.
- Do all your drawing in the Paint (or OnPaint) events;
- Use the passed graphics object to do all your drawing against.
yeah, i already put the code in Paint event, but nothing changed....
this is it
Code:
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim img As Graphics = PictureBox1.CreateGraphics
'draw red histogram
For x = 0 To 255
img.DrawLine(Pens.Red, x - 1, redArray(x - 1), x, redArray(x - 1))
x += 1
Next
End Sub
-
May 2nd, 2012, 11:43 AM
#18
Re: How to obtain every pixel from PicturebBx
You only did part 1 (correct event). You need to do part 2 (graphics object).
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
May 2nd, 2012, 11:53 AM
#19
Thread Starter
New Member
Re: How to obtain every pixel from PicturebBx
actually i dont understand part 2 heheheh....
can u explain it to me?
-
May 3rd, 2012, 06:14 AM
#20
Re: How to obtain every pixel from PicturebBx
Remove the CreateGraphics line; use the supplied Graphics object (e.Graphics).
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|