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 :)
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
Re: How to obtain every pixel from PicturebBx
Quote:
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
Quote:
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
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
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?
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
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
http://a3.sphotos.ak.fbcdn.net/hphot...67199090_n.jpg
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.
Re: How to obtain every pixel from PicturebBx
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?
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
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]) ??
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
Re: How to obtain every pixel from PicturebBx
Quote:
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.
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..
Re: How to obtain every pixel from PicturebBx
Quote:
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.
Re: How to obtain every pixel from PicturebBx
Quote:
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
Re: How to obtain every pixel from PicturebBx
You only did part 1 (correct event). You need to do part 2 (graphics object).
Re: How to obtain every pixel from PicturebBx
actually i dont understand part 2 heheheh....
can u explain it to me?
Re: How to obtain every pixel from PicturebBx
Remove the CreateGraphics line; use the supplied Graphics object (e.Graphics).