Results 1 to 3 of 3

Thread: [RESOLVED] [VB2010] - the Setpixel() method have an error?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,964

    Resolved [RESOLVED] [VB2010] - the Setpixel() method have an error?

    heres my code:
    Code:
    Public Class Form1
        Dim imgImage As Bitmap
        Dim clrOld As Color
        Dim clrNew As Color
        Dim blnChangeColor As Boolean
    
        Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
            ColorDialog1.ShowDialog()
            PictureBox3.BackColor = ColorDialog1.Color
            clrNew = ColorDialog1.Color
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            OpenFileDialog1.ShowDialog()
            imgImage = Bitmap.FromFile(OpenFileDialog1.FileName)
            PictureBox1.Image = imgImage
        End Sub
    
        Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
            If e.Button = Windows.Forms.MouseButtons.Left Then
                PictureBox2.BackColor = imgImage.GetPixel(e.X, e.Y)
                clrOld = PictureBox2.BackColor
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            blnChangeColor = True
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            Dim x As Integer = 0
            Dim y As Integer = 0
            Dim g As Graphics
            g = PictureBox1.CreateGraphics()
    
            If blnChangeColor = True Then
                For y = 0 To imgImage.Height - 1
                    For x = 0 To imgImage.Width - 1
                        If imgImage.GetPixel(x, y) = clrOld Then
                            imgImage.SetPixel(x, y, clrNew)
                        End If
                    Next x
                Next y
                g.DrawImage(imgImage, 0, 0, imgImage.Width, imgImage.Height)
            End If
            PictureBox1.Invalidate()
        End Sub
    
    End Class
    i recive these error: "SetPixel is not supported for images with indexed pixel formats."
    i don't know why these error
    can anyone advice me why?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [VB2010] - the Setpixel() method have an error?


  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,964

    Re: [VB2010] - the Setpixel() method have an error?

    who said that?
    i make the diference(reading the Master Visual Basic 2010 is good).
    i don't understand why i must use in these way... but i did 1 nice function for change the colors:
    Code:
    Private Function ChangeImageColor(ByVal ImgImage As Image, ByVal OldColor As Color, ByVal NewColor As Color) As Image
            Dim x As Integer = 0
            Dim y As Integer = 0
    
            'by some reason i must do these 4 things
            'for use the SetPixel normaly
            Dim Bitmap1 As Bitmap = New Bitmap(ImgImage)
            ImgImage = Bitmap1
            Dim Bitmap2 As Bitmap = New Bitmap(ImgImage)
            Bitmap2 = New Bitmap(ImgImage)
    
            'i use 2 for...next for scrool the entire image
            'i use the if for compare the pixels
            For y = 0 To Bitmap2.Height - 1
                For x = 0 To Bitmap2.Width - 1
                    If Bitmap2.GetPixel(x, y) = OldColor Then
                        Bitmap2.SetPixel(x, y, NewColor)
                    End If
                Next x
            Next y
    
            'now i return the image result
            ChangeImageColor = Bitmap2
        End Function
    these function works fine. and heres how use it:
    Code:
    PictureBox1.Image = ChangeImageColor(PictureBox1.Image, PictureBox2.BackColor, PictureBox3.BackColor)
    it's easy to use isn't?
    PictureBox1.Image - is where you want the final image(changed);
    PictureBox1.Image(the function 1st argument) - is the image that you want to change;
    PictureBox2.BackColor - is the color that you want change;
    PictureBox3.BackColor - is the new color.
    i accept questions and sugestions
    Last edited by joaquim; Dec 2nd, 2011 at 04:40 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

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



Click Here to Expand Forum to Full Width