Results 1 to 3 of 3

Thread: DrawImage and defining color to copy instead of mask.

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    6

    DrawImage and defining color to copy instead of mask.

    I am using VB 2008 & drawimage function to copy certain colored parts of image A to Image B. My problem is image A has many different colored patches on it and I only want to copy the ones that have a certain RGB value. I was going to set the masking color to the RGB value but remembered that defines the color to ignore - I’m interested in the opposite.

    Basically I'm looking for a "Copy only pixels of certain color" technique.

    I know I could loop through all the pixels and check it’s color, but it’s way slow and I’m hoping there’s something better.

    Thanks in advance!

    P.S. As a side note, after getting the copy function working, my next requirements will be to change all the colors on a image to be the same, then overlay that resulting image onto a second image, perferalby with about 50% transparency. I think I have that figured out though...
    Last edited by tscorpio; Feb 12th, 2012 at 10:32 AM.

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: DrawImage and defining color to copy instead of mask.

    Quote Originally Posted by tscorpio View Post
    I am using VB 2008 & drawimage function to copy certain colored parts of image A to Image B. My problem is image A has many different colored patches on it and I only want to copy the ones that have a certain RGB value. I was going to set the masking color to the RGB value but remembered that defines the color to ignore - I’m interested in the opposite.

    Basically I'm looking for a "Copy only pixels of certain color" technique.

    I know I could loop through all the pixels and check it’s color, but it’s way slow and I’m hoping there’s something better.

    Thanks in advance!

    P.S. As a side note, after getting the copy function working, my next requirements will be to change all the colors on a image to be the same, then overlay that resulting image onto a second image, perferalby with about 50% transparency. I think I have that figured out though...
    Hi tscorpio,
    I can's see any alternative to looping through the pixels. Since you say looping through the pixels is slow, perhaps you are not yet familiar with the LockBits method which allows you to treat the image as a byte or integer array. The integer version could easily provide speed gains of over 100x over GetPixel/SetPixel for large images. The speed gain depends partly on how efficient you make your inner loop.

    I posted a LockBits wrapper class (FastPix) to the CodeBank which makes it simpler to apply the LockBits method. See the "rapid pixel processing" link in my signature below. Assuming you have downloaded the FastPix class and added it to your project, and includeColor is the color you want to keep, you could code what you want to do optimally like this:

    vb Code:
    1. Dim colorValue As Integer = includeColor.ToARGB
    2. Dim alphaMaskValue As Integer = &HFFFFFF
    3. Dim alphaValue As Integer = &H80000000
    4. Dim bmpA As New Bitmap(ImageA)
    5.  
    6. Using fp As New FastPix(bmpA)
    7.    Dim pixels() As Integer = fp.PixelArray
    8.    For i As Integer = 0 To pixels.Length - 1
    9.        Dim pixelValue As Integer = pixels(i)
    10.        If pixelValue = colorValue Then
    11.             pixels(i) = pixelValue And alphaMaskValue Or alphaValue
    12.       Else
    13.             pixels(i) = 0
    14.       End If
    15.    Next
    16. End Using
    The effect of this is to set the pixels of the wanted color to 50% opacity and all other pixels to 0% opacity. You can then paint bmpA onto ImageB using Graphics.DrawImage (which GDI+ does pretty efficiently).

    BB

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    6

    Re: DrawImage and defining color to copy instead of mask.

    Thank you for the reply.
    I haven't had a chance to get back to this, but I really appriciate your idea of lock bits. I was not aware of them, but I think that approach will give me the speed I'd like.

    I will post again after I've coded some stuff.

    Thanks again!!!

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