none
[VB]Change color image in vb net

    Question

  • Good evening and thank you for your time and help
    This is what I would like:
    In my program I charge small image 100 pixel by 100 pixel;
    From there I would like to change the color of each pixel with a palette that I created so I load these colors in a list 

    So from there I would like my program to compare each pixel and replace it with the color closest to my palette

    This is what I have already tried, but I have an overflow error.
    Here is the code:

    For i = 0 To monImage.Width - 1
                    For j = 0 To monImage.Height - 1
                        LaCouleur = monImage.GetPixel(i, j)
                        Dim lememe As Integer = 0
                        Dim oldmeme As Integer = 0
                    Dim oldval As Double = 0
                    Dim val As Double= Math.Sqrt((LaCouleur.A - MaPalette(0).A) * (LaCouleur.A - MaPalette(0).A) + (LaCouleur.G - MaPalette(0).G) * (LaCouleur.G - MaPalette(0).G) + (LaCouleur.B - MaPalette(0).B) * (LaCouleur.B - MaPalette(0).B) + (LaCouleur.R - MaPalette(0).R) * (LaCouleur.R - MaPalette(0).R))
                    For idx As Integer = 0 To MaPalette.Count - 1
                        Dim val2 As Double = Math.Sqrt((LaCouleur.A - MaPalette(idx).A) * (LaCouleur.A - MaPalette(idx).A) + (LaCouleur.G - MaPalette(idx).G) * (LaCouleur.G - MaPalette(idx).G) + (LaCouleur.B - MaPalette(idx).B) * (LaCouleur.B - MaPalette(idx).B) + (LaCouleur.R - MaPalette(idx).R) * (LaCouleur.R - MaPalette(idx).R))
                        If val2 <= val Then
                                oldval = val
                                val = val2
                                oldmeme= meme
                                meme = idx
                            End If
                        Next idx
                        monimage2.SetPixel(i, j, MaPalette(meme))
                    Next j
                Next 
    That's from there I'm stuck, and I do not know why I have an overflow
    I thank you in advance for the help given and the advice given



    Sunday, December 31, 2017 9:29 AM

Answers

  • I have overflow errors on variables val and val2; So I broke them down and when I debug, I have the same error override Thank you for your help

    When you broke those expressions down into their sub-expressions, and your debugger stopped at an error, what was the line of code, and what were the values of the two variables referred to in that line of code?

    • Marked as answer by roro6970 Tuesday, January 02, 2018 10:28 AM
    Tuesday, January 02, 2018 8:38 AM

All replies

  • Hi roro6970,

    Welcome to the Developing Universal Windows apps forum! Please utilize tagging when posting to this forum, thanks!

    Are you developing a UWP app? Your above code seems to use a Bitmap class to get and set a pixel, could you provide more info about your issue?

    To get a pixel in UWP app, you can use the BitmapDecoder class and use the GetPixelDataAsync() method to request the frame pixel data then call the PixelDataProvider.DetachPixelData() to get the internally-stored pixel data. You can get more details from Create, edit, and save bitmap images.

    Best regards,

    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 01, 2018 8:15 AM
  • Hello Thank you for your reply, I'm sorry; but it's for a program winform application. Have you way to move my topic. Thanks for your help and your time spent
    Monday, January 01, 2018 10:35 AM
  • Hello roro6970,

    Ok, I will move this thread to the corresponding forum for a better support. 

    Best regards,

    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 02, 2018 2:35 AM
  • Hi roro6970,

    You said that you have an overflow error from your code, where you have this error?

    And can you please provide the entire code that can produce this issue here? Because I don't get MaPalette class and I also don't know what  meme's value is.

    About change the color of the each pixel, I get this info, and I find some error in your code.

    https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.setpixel(v=vs.110).aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 02, 2018 6:31 AM
    Moderator
  • That's from there I'm stuck, and I do not know why I have an overflow

    You have two lines of code that are too long and complex to try and debug: 

       Dim val As Double= Math.Sqrt((LaCouleur.A - MaPalette(0).A) * (LaCouleur.A - MaPalette(0).A) + (LaCouleur.G - MaPalette(0).G) * (LaCouleur.G - MaPalette(0).G) + (LaCouleur.B - MaPalette(0).B) * (LaCouleur.B - MaPalette(0).B) + (LaCouleur.R - MaPalette(0).R) * (LaCouleur.R - MaPalette(0).R))
    ...
    Dim val2 As Double = Math.Sqrt((LaCouleur.A - MaPalette(idx).A) * (LaCouleur.A - MaPalette(idx).A) + (LaCouleur.G - MaPalette(idx).G) * (LaCouleur.G - MaPalette(idx).G) + (LaCouleur.B - MaPalette(idx).B) * (LaCouleur.B - MaPalette(idx).B) + (LaCouleur.R - MaPalette(idx).R) * (LaCouleur.R - MaPalette(idx).R))

    Dim a new local variable for each sub-expression and do the calculation using as many lines of code as possible, so each line has one single simple expression.  Then insert a breakpoint before the first line, run the program and step through from the breakpoint one line at a time, looking at the intermediate values, until you find where the problem occurs.

    Tuesday, January 02, 2018 7:12 AM
  • Hello, thank you for your answers and the time

    Here is my code a little better:

    'Here my palette:
        Private MesCouleurs As New List(Of Color) From {Color.FromArgb(236, 237, 237), Color.FromArgb(240, 232, 185), Color.FromArgb(240, 185, 1), Color.FromArgb(230, 79, 39), Color.FromArgb(182, 49, 54), Color.FromArgb(225, 136, 159), Color.FromArgb(105, 74, 130), Color.FromArgb(44, 70, 144), Color.FromArgb(48, 92, 176),
            Color.FromArgb(37, 104, 71), Color.FromArgb(73, 174, 137), Color.FromArgb(83, 65, 55), Color.FromArgb(192, 36, 53), Color.FromArgb(55, 184, 118), Color.FromArgb(131, 136, 138), Color.FromArgb(46, 47, 49), Color.FromArgb(216, 210, 206), Color.FromArgb(127, 51, 42), Color.FromArgb(165, 105, 63), Color.FromArgb(165, 45, 54),
            Color.FromArgb(104, 62, 154), Color.FromArgb(135, 89, 61), Color.FromArgb(222, 155, 144), Color.FromArgb(222, 180, 139), Color.FromArgb(54, 63, 56), Color.FromArgb(185, 57, 94), Color.FromArgb(89, 47, 56), Color.FromArgb(103, 151, 174), Color.FromArgb(255, 32, 141), Color.FromArgb(255, 57, 86), Color.FromArgb(229, 239, 19)}
    'Here filter image
     Private Sub Fitre_Click(sender As Object, e As EventArgs) Handles Button5.Click
            Dim LaCouleur As Color
            Dim imgor As Bitmap = New Bitmap(Pic1.Image)
            Dim image2 As Bitmap = New Bitmap(img.Width, img.Height)
            MessageBox.Show(MesCouleurs(4).ToString)
            Try
                For i = 0 To imgor.Width - 1
                    For j = 0 To imgor.Height - 1
                        LaCouleur = imgor.GetPixel(i, j)
                        Dim lememe As Integer = 0
                        Dim oldmeme As Integer = 0
                        Dim oldval As Double = 0
                        Dim val As Double = Math.Sqrt((LaCouleur.A - MesCouleurs(0).A) * (LaCouleur.A - MesCouleurs(0).A) +
                                                  (LaCouleur.G - MesCouleurs(0).G) * (LaCouleur.G - MesCouleurs(0).G) +
                                                  (LaCouleur.B - MesCouleurs(0).B) * (LaCouleur.B - MesCouleurs(0).B) +
                                                  (LaCouleur.R - MesCouleurs(0).R) * (LaCouleur.R - MesCouleurs(0).R))
                        For idx As Integer = 0 To MesCouleurs.Count - 1
                            Dim val2 As Double = Math.Sqrt((LaCouleur.A - MesCouleurs(idx).A) * (LaCouleur.A - MesCouleurs(idx).A) +
                                                       (LaCouleur.G - MesCouleurs(idx).G) * (LaCouleur.G - MesCouleurs(idx).G) +
                                                       (LaCouleur.B - MesCouleurs(idx).B) * (LaCouleur.B - MesCouleurs(idx).B) +
                                                       (LaCouleur.R - MesCouleurs(idx).R) * (LaCouleur.R - MesCouleurs(idx).R))
                            If val2 <= val Then
                                oldval = val
                                val = val2
                                oldmeme = lememe
                                lememe = idx
                            End If
                        Next idx
                        image2.SetPixel(i, j, MesCouleurs(lememe))
                    Next j
                Next i
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            Finally
                ZPic3.Image = image2
            End Try
        End Sub


    I have overflow errors on variables val and val2; So I broke them down and when I debug, I have the same error override Thank you for your help

    • Edited by roro6970 Tuesday, January 02, 2018 8:00 AM
    Tuesday, January 02, 2018 7:59 AM
  • I have overflow errors on variables val and val2; So I broke them down and when I debug, I have the same error override Thank you for your help

    When you broke those expressions down into their sub-expressions, and your debugger stopped at an error, what was the line of code, and what were the values of the two variables referred to in that line of code?

    • Marked as answer by roro6970 Tuesday, January 02, 2018 10:28 AM
    Tuesday, January 02, 2018 8:38 AM
  • Hello thank you very much ;
    It would seem that the return of the root squared created safety overruns;
    so here is my corrected code, it's a bit long run but at least it works

     Public Function BonneCouleur(ByVal imgg As Bitmap) As Bitmap
            Dim i As Integer = 0
            Do While i < imgg.Height
                Dim j As Integer = 0
                Do While j < imgg.Width
                    Dim bestindex As Integer = 0
                    Dim val As Integer = 255 * 255 + 255 * 255 + 255 * 255 + 1
                    For k As Integer = 0 To MesCouleurs.Count - 1
                        Dim mycolor As Color = imgg.GetPixel(j, i)
                        Dim Rdiff As Integer = mycolor.R
                        Dim Gdiff As Integer = mycolor.G
                        Dim Bdiff As Integer = mycolor.B
    
                        If Rdiff < MesCouleurs(k).R Then
                            Rdiff = MesCouleurs(k).R - Rdiff
                        Else
                            Rdiff = Rdiff - MesCouleurs(k).R
                        End If
    
                        If Gdiff < MesCouleurs(k).G Then
                            Gdiff = MesCouleurs(k).G - Gdiff
                        Else
                            Gdiff = Gdiff - MesCouleurs(k).G
                        End If
    
                        If Bdiff < MesCouleurs(k).B Then
                            Bdiff = MesCouleurs(k).B - Bdiff
                        Else
                            Bdiff = Bdiff - MesCouleurs(k).B
                        End If
    
                        Dim val1 As Integer = Rdiff * Rdiff + Gdiff * Gdiff + Bdiff * Bdiff
    
                        If val1 < val Then
                            val = val1
                            bestindex = k
                        End If
                    Next k
                    imgg.SetPixel(j, i, MesCouleurs(bestindex))
                    j += 1
                Loop
                i += 1
            Loop
            Return imgg
        End Function
    Thank you for your help and the time
    Ps: Where can i put solved
    Tuesday, January 02, 2018 10:28 AM
  • It would seem that the return of the root squared created safety overruns;
    so here is my corrected code, it's a bit long run but at least it works

    I think you need the ABS function: https://msdn.microsoft.com/en-us/library/f2yzeea2(v=vs.110).aspx

    But in any case at least you can now see where the problem is.  If you mark the responses that assisted you to find a solution as Answers, then that indicates the problem is resolved.

    Tuesday, January 02, 2018 11:33 AM