[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
Go Back  Xtreme Visual Basic Talk > > > [VB2010] - about Lock and Unlock bimap


Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2013, 11:15 AM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default [VB2010] - about Lock and Unlock bimap


i can't find a nice tutorial(even completed) about Lock and UnLock Bitmaps
can anyone explain to me how use them?
(what i found don't explain how get the pixels)
Reply With Quote
  #2  
Old 07-15-2013, 08:57 AM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

i found these code and i'm trying put it with my code, but i don't get right results
Code:
Private Sub ChangeColors(ByVal bitmap As Bitmap, ByVal NewColor As Color, ByVal OldColor As Color)
        Dim bmp As New Bitmap(bitmap)

        ' Lock the bitmap's pixels
        Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
        Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, _
                Drawing.Imaging.ImageLockMode.ReadWrite, _
                Imaging.PixelFormat.Format32bppRgb)

        ' Get the address of the first line
        Dim ptr As IntPtr = bmpData.Scan0

        ' Declare an array to hold the bytes of the bitmap
        Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
        Dim rgbValues(bytes - 1) As Byte
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)

        ' Retrieve RGB values
        Dim RedValue As Int32
        Dim GreenValue As Int32
        Dim BlueValue As Int32
        Dim l As Integer = 0
        Dim ActualColor As Color
        For x = 0 To bmp.Width
            For y = 0 To bmp.Height - 1
                l = ((bmp.Width * 3 * y) + (x * 3))
                RedValue = rgbValues(l)
                GreenValue = rgbValues(l + 1)
                BlueValue = rgbValues(l + 2)
                ActualColor = Color.FromArgb(255, RedValue, GreenValue, BlueValue)
                If ActualColor = OldColor Then
                    rgbValues(l) = NewColor.R
                    rgbValues(l + 1) = NewColor.G
                    rgbValues(l + 2) = NewColor.B
                End If
            Next
        Next

        ' Unlock the bitmap
        bmp.UnlockBits(bmpData)
        PictureBox1.Image = bmp
    End Sub
what i'm doing wrong?
(sorry but i can't find nice tutorials about it... i don't understand because they don't say how catch RGB from values)
Reply With Quote
  #3  
Old 07-15-2013, 09:14 AM
DrPunk's Avatar
DrPunk[VB2010] - about Lock and Unlock bimap DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Some of that code looks like it was taken from the MSDN's LockBits example -> http://msdn.microsoft.com/en-us/libr...code-snippet-2

That clearly states that the code there only works for 24bpp images. You have specified in your code you are using a 32bbp image and so MSDNs example code would not work without changing it.

The bbp is the bits per pixel. There are 8 bits in a byte.

So take a 24bbp image and split it into an array of bytes and every pixel will have 3 entries in the array (24 bits = 3 bytes). Pixel 0 is defined by elements 0, 1 and 2 in the array. Pixel 1 is defined by elements 3, 4 and 5 in the array. And so on.

Take a 32bbp image and split that into an array of bytes and every pixel will have 4 entries in the array (32 bits = 4 bytes). Pixel 0 is defined by elements 0, 1, 2 and 3 in the array. Pixel 1 is defined by elements 4, 5, 6 and 7 in the array. And so on.

Hope that helps. I don't think I can help much more than that.
__________________
There are no computers in heaven!
Reply With Quote
  #4  
Old 07-15-2013, 09:38 AM
DrPunk's Avatar
DrPunk[VB2010] - about Lock and Unlock bimap DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

The MSDN example gives a much better loop over the pixels too. Working from x, y to pixel (((bmp.Width * 3 * y) + (x * 3))) is a bit daft.

The rgbValues array has all the bytes, so just loop over that. The image format's bbp / 8 gives you the step (hence Step 3 in the MSDN example). With that step in, each iteration over the loop will be for a pixel. The MSDN article's loop starts at 2 because it is only setting the 3rd byte of each pixel. Normally you'd want to start at 0.

So, looping over every pixel in your 32bbp image would step 4. The i in the loop will indicate what pixel you are looking at. You can then use that to check each byte of the pixel
Code:
For i = 0 to rgbValues.Length - 1 Step 4
    ' First byte of pixel
    RedValue = rgbValues(i)
    ' Second byte of pixel
    GreenValue = rgbValues(i + 1)
    ' Third byte of pixel
    BlueValue = rgbValues(i + 2)
    ' Fourth byte is alpha, I guess. You could read it if you wanted to

    ActualColor = Color.FromArgb(255, RedValue, GreenValue, BlueValue)

    If ActualColor = OldColor Then
          rgbValues(l) = NewColor.R
          rgbValues(l + 1) = NewColor.G
          rgbValues(l + 2) = NewColor.B
    End If

Next
Might work. Dunno.
__________________
There are no computers in heaven!
Reply With Quote
  #5  
Old 07-15-2013, 09:52 AM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

sorry... but i get the same result.
only changes the backcolor to black
Reply With Quote
  #6  
Old 07-15-2013, 09:55 AM
DrPunk's Avatar
DrPunk[VB2010] - about Lock and Unlock bimap DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Are you sure it's a Imaging.PixelFormat.Format32bppRgb image that you are using?
__________________
There are no computers in heaven!
Reply With Quote
  #7  
Old 07-15-2013, 09:57 AM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

Quote:
Originally Posted by DrPunk View Post
Are you sure it's a Imaging.PixelFormat.Format32bppRgb image that you are using?
yes.. but if you advice, i can use another one
Reply With Quote
  #8  
Old 07-15-2013, 10:07 AM
DrPunk's Avatar
DrPunk[VB2010] - about Lock and Unlock bimap DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I don't suggest using another one, I only suggest that it's very important you use the right pixelformat image for the code.

It's a case of looking at what is happening at each line of code and wondering why it's coming out black, I suppose. Dunno. Over each loop, what it is getting for Red green and blue? Is the ActualColor that it calculates the colour of the pixel?

I have hopefully provided you with the information of what the code is trying to do (which could be wrong, but don't think so). If you understand that then it should help work out why it isn't.
__________________
There are no computers in heaven!
Reply With Quote
  #9  
Old 07-15-2013, 10:14 AM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

Quote:
Originally Posted by DrPunk View Post
I don't suggest using another one, I only suggest that it's very important you use the right pixelformat image for the code.

It's a case of looking at what is happening at each line of code and wondering why it's coming out black, I suppose. Dunno. Over each loop, what it is getting for Red green and blue? Is the ActualColor that it calculates the colour of the pixel?

I have hopefully provided you with the information of what the code is trying to do (which could be wrong, but don't think so). If you understand that then it should help work out why it isn't.
heres the entire code:
Code:
Public Class Form1

    Private Sub ChangeColors(ByVal bitmap As Bitmap, ByVal NewColor As Color, ByVal OldColor As Color)
        Dim bmp As New Bitmap(bitmap)

        ' Lock the bitmap's pixels
        Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
        Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, _
                Drawing.Imaging.ImageLockMode.ReadWrite, _
                Imaging.PixelFormat.Format32bppRgb)

        ' Get the address of the first line
        Dim ptr As IntPtr = bmpData.Scan0

        ' Declare an array to hold the bytes of the bitmap
        Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
        Dim rgbValues(bytes - 1) As Byte
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)

        ' Retrieve RGB values
        Dim RedValue As Int32
        Dim GreenValue As Int32
        Dim BlueValue As Int32
        Dim l As Integer = 0
        Dim ActualColor As Color
        Dim i As Integer

        For i = 0 To rgbValues.Length - 1 Step 4
            ' First byte of pixel
            RedValue = rgbValues(i)
            ' Second byte of pixel
            GreenValue = rgbValues(i + 1)
            ' Third byte of pixel
            BlueValue = rgbValues(i + 2)
            ' Fourth byte is alpha, I guess. You could read it if you wanted to

            ActualColor = Color.FromArgb(rgbValues(i + 3), RedValue, GreenValue, BlueValue)

            If ActualColor = OldColor Then
                rgbValues(l) = NewColor.R
                rgbValues(l + 1) = NewColor.G
                rgbValues(l + 2) = NewColor.B
            End If

        Next

        ' Unlock the bitmap
        bmp.UnlockBits(bmpData)
        PictureBox1.Image = bmp
    End Sub


    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Dim s As Bitmap = PictureBox1.Image.Clone ' < === Use "Clone"

        If e.Button = MouseButtons.Left Then
            PictureBox2.BackColor = s.GetPixel(e.X, e.Y)
        End If
        s.Dispose()
    End Sub

    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        ColorDialog1.ShowDialog()
        PictureBox3.BackColor = ColorDialog1.Color
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ChangeColors(PictureBox1.Image, PictureBox2.BackColor, PictureBox3.BackColor)
    End Sub

End Class
picturebox1 - is the image;
picturebox2 - is the oldcolor(clicked from picturebox1);
picturebox3 - choose the new color(dialog color).
Reply With Quote
  #10  
Old 07-15-2013, 03:28 PM
passel's Avatar
passel[VB2010] - about Lock and Unlock bimap passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Do you try to step through the code in the IDE and look at the values to see if they make sense?

Code:
'Why do you loop with "i"
        For i = 0 To rgbValues.Length - 1 Step 4
'...
'But then index with "l"
            If ActualColor = OldColor Then
                rgbValues(l) = NewColor.R         '<==== should be i
                rgbValues(l + 1) = NewColor.G      '<==== should be i + 1
                rgbValues(l + 2) = NewColor.B      '<==== should be i + 2
            End If
        Next
Code:
'Why do you have them pick an "OldColor"
      PictureBox2.BackColor = s.GetPixel(e.X, e.Y)

'then pass it in the "NewColor" parameter
    ChangeColors(PictureBox1.Image, PictureBox2.BackColor, PictureBox3.BackColor)
Private Sub ChangeColors(ByVal bitmap As Bitmap, ByVal NewColor As Color, ByVal OldColor As Color)
Code:
'Why, when the color order is ARGB (3,2,1,0), do you use ABGR
            RedValue = rgbValues(i)           '<====Should be Blue
            ' Second byte of pixel
            GreenValue = rgbValues(i + 1)
            ' Third byte of pixel
            BlueValue = rgbValues(i + 2)    '<====Should be Red (next to A)
            ' Fourth byte is alpha, I guess. You could read it if you wanted to
'...
                rgbValues(l) = NewColor.R          '<====Should be .B
                rgbValues(l + 1) = NewColor.G
                rgbValues(l + 2) = NewColor.B   '<Should be .R
Code:
'You copy the bitmap bytes to a managed byte array.
 System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)

'But you never copy the managed byte array back to the bitmap.
' Should copy the array back to your bitmap, before unlocking the bitmap
    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
    ' Unlock the bitmap
    bmp.UnlockBits(bmpData)

Updated code, with a few other changes, like getting rid of the cloning of the bitmap, and passing the bitmap by reference so don't need to create a new bitmap to reassign to the picturebox, just modify the one passed.
Code:
Public Class Form1
  Private Sub ChangeColors(ByRef bitmap As Bitmap, ByVal NewColor As Color, ByVal OldColor As Color)

    ' Lock the bitmap's pixels
    Dim rect As New Rectangle(0, 0, bitmap.Width, bitmap.Height)
    Dim bmpData As System.Drawing.Imaging.BitmapData = bitmap.LockBits(rect, _
            Drawing.Imaging.ImageLockMode.ReadWrite, _
            Imaging.PixelFormat.Format32bppRgb)

    ' Get the address of the first line
    Dim ptr As IntPtr = bmpData.Scan0

    ' Declare an array to hold the bytes of the bitmap
    Dim bytes As Integer = Math.Abs(bmpData.Stride) * bitmap.Height
    Dim rgbValues(bytes - 1) As Byte
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)

    ' Retrieve RGB values
    Dim ActualColor As Color
    Dim i As Integer

    For i = 0 To rgbValues.Length - 1 Step 4
      ActualColor = Color.FromArgb(BitConverter.ToInt32(rgbValues, i)) 'convert 4 bytes to integer then to Color

      If ActualColor = OldColor Then
        rgbValues(i) = NewColor.B
        rgbValues(i + 1) = NewColor.G
        rgbValues(i + 2) = NewColor.R
      End If

    Next

    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
    ' Unlock the bitmap
    bitmap.UnlockBits(bmpData)
  End Sub

  Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    PictureBox2.BackColor = CType(PictureBox1.Image, Bitmap).GetPixel(e.X, e.Y)
  End Sub

  Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
    ColorDialog1.ShowDialog()
    PictureBox3.BackColor = ColorDialog1.Color
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ChangeColors(PictureBox1.Image, PictureBox3.BackColor, PictureBox2.BackColor)
  End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 07-15-2013 at 03:51 PM.
Reply With Quote
  #11  
Old 07-15-2013, 03:39 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

Quote:
Originally Posted by passel View Post
Do you try to step through the code in the IDE and look at the values to see if they make sense?

Code:
'Why do you loop with "i"
        For i = 0 To rgbValues.Length - 1 Step 4
'...
'But then index with "l"
            If ActualColor = OldColor Then
                rgbValues(l) = NewColor.R         '<==== should be i
                rgbValues(l + 1) = NewColor.G      '<==== should be i + 1
                rgbValues(l + 2) = NewColor.B      '<==== should be i + 2
            End If
        Next
Code:
'Why do you have them pick an "OldColor"
      PictureBox2.BackColor = s.GetPixel(e.X, e.Y)

'then pass it in the "NewColor" parameter
    ChangeColors(PictureBox1.Image, PictureBox2.BackColor, PictureBox3.BackColor)
Private Sub ChangeColors(ByVal bitmap As Bitmap, ByVal NewColor As Color, ByVal OldColor As Color)
Code:
'Why, when the color order is ARGB (3,2,1,0), do you use ABGR
            RedValue = rgbValues(i)           '<====Should be Blue
            ' Second byte of pixel
            GreenValue = rgbValues(i + 1)
            ' Third byte of pixel
            BlueValue = rgbValues(i + 2)    '<====Should be Red (next to A)
            ' Fourth byte is alpha, I guess. You could read it if you wanted to
'...
                rgbValues(l) = NewColor.R          '<====Should be .B
                rgbValues(l + 1) = NewColor.G
                rgbValues(l + 2) = NewColor.B   '<Should be .R
Code:
'You copy the bitmap bytes to a managed byte array.
 System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)

'But you never copy the managed byte array back to the bitmap.
' Should copy the array back to your bitmap, before unlocking the bitmap
    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
    ' Unlock the bitmap
    bmp.UnlockBits(bmpData)
please try give me a nice tutorial link for i learn more about it
i can't find i nice tutorial that explains how use ARGB values
i found some only for invert the colors, but i don't understand how they see the ARGB values
please try give me a nice tutorial about it
seems easy but i'm lost in these part
Reply With Quote
  #12  
Old 07-15-2013, 03:45 PM
AtmaWeapon's Avatar
AtmaWeapon[VB2010] - about Lock and Unlock bimap AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

There is no one-stop tutorial for this. You are manipulating the raw bits of a bitmap. That means you have to understand, for every format you want to support, exactly how individual bytes of the file translate into the color of a pixel. That involves a lot of study and calculation. I haven't found a good reference for /one/ image type, let alone a tutorial on the entire thing. It's like you're asking for a detailed, practical tutorial on "replacing the head gasket in every car ever".

Maybe there's someone else less cynical that can dig one up, but right now the only advice I can offer is, "When you can write a program that spits out a bitmap with a checkerboard pattern from raw bytes, the answer to your question will likely become clear after a debugging session." I'm not there, and can't help with it.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #13  
Old 07-15-2013, 03:59 PM
passel's Avatar
passel[VB2010] - about Lock and Unlock bimap passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

If you put in the changes for the questions I asked then two things were suppose to occur,
1. Your code would work.
2. You might understand why it didn't work and why it now works.

Hopefully you'll re-read the questions as a mini-tutorial, at least for the code given.
I also added a slightly modified version of your code as a completed working version at the end (while you guys were posting, I guess).
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #14  
Old 07-15-2013, 04:05 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

thanks to all.
i will go back to that code that invert the colors.. maybe study more i can found my solution. thanks to all
Reply With Quote
  #15  
Old 07-15-2013, 04:55 PM
passel's Avatar
passel[VB2010] - about Lock and Unlock bimap passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

This link also is an example that essentially does what your code was trying to do, swap one color with another. Redundant at this point, but shows that searching on VB.Net LockBits can find a few working examples.
And this link looks like it might be a good basic tutorial from a brief scroll through, but I haven't read it.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #16  
Old 07-16-2013, 03:19 AM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

Quote:
Originally Posted by passel View Post
This link also is an example that essentially does what your code was trying to do, swap one color with another. Redundant at this point, but shows that searching on VB.Net LockBits can find a few working examples.
And this link looks like it might be a good basic tutorial from a brief scroll through, but I haven't read it.
thanks for all
thanks..

i'm read it and seems great
maybe i can find my problem in code
thanks
Reply With Quote
  #17  
Old 07-16-2013, 03:50 AM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

i belive that i can change these funtion for what i need:
Code:
Public Shared Function SetPixel(ByRef OriginalBitmap As Bitmap, _
                               ByRef PixelLocation As Point, _
                                ByRef PixelColor As Color) As Bitmap
        Const px As System.Drawing.GraphicsUnit = GraphicsUnit.Pixel
        Const fmtArgb As Imaging.PixelFormat = Imaging.PixelFormat.Format32bppArgb
        Dim boundsF As RectangleF = OriginalBitmap.GetBounds(px)
        Dim bounds As New Rectangle
        bounds.Location = New Point(CInt(boundsF.X), CInt(boundsF.Y))
        bounds.Size = New Size(CInt(boundsF.Width), CInt(boundsF.Height))
        Dim bmClone As Bitmap = OriginalBitmap.Clone(bounds, fmtArgb)
        Dim bmData As System.Drawing.Imaging.BitmapData = bmClone.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, bmClone.PixelFormat)
        Dim offsetToFirstPixel As IntPtr = bmData.Scan0
        Dim byteCount As Integer = Math.Abs(bmData.Stride) * bmClone.Height
        Dim bitmapBytes(byteCount - 1) As Byte
        System.Runtime.InteropServices.Marshal.Copy(offsetToFirstPixel, bitmapBytes, 0, byteCount)
        Dim alpha As Byte = PixelColor.A
        Dim red As Byte = PixelColor.R
        Dim green As Byte = PixelColor.G
        Dim blue As Byte = PixelColor.B

        'locations
        Dim baseOffset As Integer = bmData.Stride * PixelLocation.Y
        Dim addOffset As Integer = PixelLocation.X * 4
        Dim offset As Integer = baseOffset + addOffset

        'setting the BRGA values(in memory they work in these order)
        bitmapBytes(offset) = blue
        bitmapBytes(offset + 1) = green
        bitmapBytes(offset + 2) = red
        bitmapBytes(offset + 3) = alpha

        'save the work
        System.Runtime.InteropServices.Marshal.Copy(bitmapBytes, 0, offsetToFirstPixel, byteCount)
        bmClone.UnlockBits(bmData)
        Return (bmClone)
    End Function
these functions fine
(i have tested and the red pixel is showed)
and i put that comments for divide the code
thanks
let me ask anotherthing: if i change the Alpha value, i do Opacy with pixels, right?
Reply With Quote
  #18  
Old 07-16-2013, 04:10 AM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default

yes... yes.. thanks
Code:
Public Function ChangeCoolors(ByRef OriginalBitmap As Bitmap, _
                               ByRef OldColor As Color, _
                                ByRef NewColor As Color) As Bitmap
        Const px As System.Drawing.GraphicsUnit = GraphicsUnit.Pixel
        Const fmtArgb As Imaging.PixelFormat = Imaging.PixelFormat.Format32bppArgb
        Dim boundsF As RectangleF = OriginalBitmap.GetBounds(px)
        Dim bounds As New Rectangle
        bounds.Location = New Point(CInt(boundsF.X), CInt(boundsF.Y))
        bounds.Size = New Size(CInt(boundsF.Width), CInt(boundsF.Height))
        Dim bmClone As Bitmap = OriginalBitmap.Clone(bounds, fmtArgb)
        Dim bmData As System.Drawing.Imaging.BitmapData = bmClone.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, bmClone.PixelFormat)
        Dim offsetToFirstPixel As IntPtr = bmData.Scan0
        Dim byteCount As Integer = Math.Abs(bmData.Stride) * bmClone.Height
        Dim bitmapBytes(byteCount - 1) As Byte
        System.Runtime.InteropServices.Marshal.Copy(offsetToFirstPixel, bitmapBytes, 0, byteCount)
        Dim alpha As Byte
        Dim red As Byte
        Dim green As Byte
        Dim blue As Byte
        Dim ActualColor As Color

        'locations
        For Y As Integer = 0 To OriginalBitmap.Height - 1
            For X As Integer = 0 To OriginalBitmap.Width - 1
                Dim baseOffset As Integer = bmData.Stride * Y
                Dim addOffset As Integer = X * 4
                Dim offset As Integer = baseOffset + addOffset
                blue = bitmapBytes(offset)
                green = bitmapBytes(offset + 1)
                red = bitmapBytes(offset + 2)
                alpha = bitmapBytes(offset + 3)
                ActualColor = Color.FromArgb(alpha, red, green, blue)
                If ActualColor = OldColor Then
                    bitmapBytes(offset) = NewColor.B
                    bitmapBytes(offset + 1) = NewColor.G
                    bitmapBytes(offset + 2) = NewColor.R
                    bitmapBytes(offset + 3) = NewColor.A
                End If
            Next
        Next
        'save the work
        System.Runtime.InteropServices.Marshal.Copy(bitmapBytes, 0, offsetToFirstPixel, byteCount)
        bmClone.UnlockBits(bmData)
        Return (bmClone)
    End Function
i don't understand, for now, the entire code. but, at least, i can do my changes hehehehe
thanks for all
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap [VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
 
[VB2010] - about Lock and Unlock bimap
[VB2010] - about Lock and Unlock bimap
 
-->