Results 1 to 20 of 20

Thread: How to obtain every pixel from PicturebBx

Hybrid View

  1. #1
    New Member
    Join Date
    Apr 12
    Posts
    13

    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

  2. #2
    Frenzied Member boops boops's Avatar
    Join Date
    Nov 08
    Location
    Holland/France
    Posts
    1,980

    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

  3. #3
    New Member
    Join Date
    Apr 12
    Posts
    13

    Re: How to obtain every pixel from PicturebBx

    Quote Originally Posted by boops boops View Post
    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

  4. #4
    Frenzied Member boops boops's Avatar
    Join Date
    Nov 08
    Location
    Holland/France
    Posts
    1,980

    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

  5. #5
    New Member
    Join Date
    Apr 12
    Posts
    13

    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:
    1. For i = 0 To img.Height - 1
    2.             For j = 0 To img.Width - 1
    3.                 Dim color As Color = img.GetPixel(j, i)
    4.                 img2.SetPixel(j, i, color)
    5.  
    6.                 redArray(i, j) = CInt(color.R)
    7.                 j += 1
    8.             Next
    9.             i += 1
    10.         Next

    i cant put the R value to 2dimensional array, do u know how to do it?

  6. #6
    Frenzied Member boops boops's Avatar
    Join Date
    Nov 08
    Location
    Holland/France
    Posts
    1,980

    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

  7. #7
    New Member
    Join Date
    Apr 12
    Posts
    13

    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:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    2.  
    3.         Dim i, j As Integer
    4.         Dim img As Bitmap = New System.Drawing.Bitmap(PictureBox1.Image)
    5.         'Dim img2 As Graphics = PictureBox2.CreateGraphics
    6.         'Dim pen As Pen
    7.         Dim img2 As Bitmap = New Bitmap(img.Width, img.Height)
    8.         ReDim redArray(img.Width, img.Height)
    9.         ReDim blueArray(img.Width, img.Height)
    10.         ReDim greenArray(img.Width, img.Height)
    11.  
    12.  
    13.  
    14.         For i = 0 To img.Height - 1
    15.             For j = 0 To img.Width - 1
    16.                 Dim color As Color = img.GetPixel(j, i)
    17.                 img2.SetPixel(j, i, color)
    18.  
    19.                 redArray(i, j) = CInt(color.R)
    20.                 If (j <= 400) Then
    21.                     j += 1
    22.                 End If
    23.             Next
    24.             i += 1
    25.  
    26.         Next
    27.  
    28.         PictureBox2.Image = img2
    29.     End Sub

    and this is the error
    Last edited by pandrew161; Apr 29th, 2012 at 09:23 AM.

  8. #8
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,392

    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

  9. #9
    Fanatic Member SJWhiteley's Avatar
    Join Date
    Feb 09
    Location
    South of the Mason-Dixon Line
    Posts
    885

    Re: How to obtain every pixel from PicturebBx

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  10. #10
    New Member
    Join Date
    Apr 12
    Posts
    13

    Re: How to obtain every pixel from PicturebBx

    oh, right
    thanks all >,<

  11. #11
    New Member
    Join Date
    Apr 12
    Posts
    13

    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?

  12. #12
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,392

    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

  13. #13
    New Member
    Join Date
    Apr 12
    Posts
    13

    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]) ??

  14. #14
    Frenzied Member boops boops's Avatar
    Join Date
    Nov 08
    Location
    Holland/France
    Posts
    1,980

    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

  15. #15
    New Member
    Join Date
    Apr 12
    Posts
    13

    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..

  16. #16
    Fanatic Member SJWhiteley's Avatar
    Join Date
    Feb 09
    Location
    South of the Mason-Dixon Line
    Posts
    885

    Re: How to obtain every pixel from PicturebBx

    Quote Originally Posted by pandrew161 View Post
    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.

  17. #17
    New Member
    Join Date
    Apr 12
    Posts
    13

    Re: How to obtain every pixel from PicturebBx

    Quote Originally Posted by SJWhiteley View Post
    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

  18. #18
    Fanatic Member SJWhiteley's Avatar
    Join Date
    Feb 09
    Location
    South of the Mason-Dixon Line
    Posts
    885

    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.

  19. #19
    New Member
    Join Date
    Apr 12
    Posts
    13

    Re: How to obtain every pixel from PicturebBx

    actually i dont understand part 2 heheheh....
    can u explain it to me?

  20. #20
    Fanatic Member SJWhiteley's Avatar
    Join Date
    Feb 09
    Location
    South of the Mason-Dixon Line
    Posts
    885

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •