Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRemap a color in an image in VB .NET
DescriptionThis example shows how to remap a color in an image in VB .NET.
Keywordsimage processing, remap color, color, VB .NET
CategoriesGraphics, VB.NET
 
When you clik the Go button, the program uses the values you entered in TextBoxes to makes two colors. It stores the colors in a ColorMap object and then makes an array containing that object. It makes an ImageAttribute object and sets its remap table to this array.

Now the program simply draws the image onto a new Bitmap, specifying the ImageAttribute object. That object automatically performs the color translation.

 
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    Dim cm As New ColorMap
    cm.OldColor = Color.FromArgb(255, _
        Integer.Parse(txtFromR.Text), _
        Integer.Parse(txtFromG.Text), _
        Integer.Parse(txtFromB.Text))
    cm.NewColor = Color.FromArgb(255, _
        Integer.Parse(txtToR.Text), _
        Integer.Parse(txtToG.Text), _
        Integer.Parse(txtToB.Text))
    Dim remap_table() As ColorMap = {cm}
    Dim image_attr As New ImageAttributes
    image_attr.SetRemapTable(remap_table, _
        ColorAdjustType.Bitmap)

    Dim bm_src As Bitmap = picSrc.Image.Clone
    Dim bm As New Bitmap(bm_src.Width, bm_src.Height)
    Dim gr As Graphics = Graphics.FromImage(bm)
    Dim rect As Rectangle = _
        Rectangle.Round(bm.GetBounds(GraphicsUnit.Pixel))

    gr.DrawImage(bm_src, rect, 0, 0, bm.Width, bm.Height, _
        GraphicsUnit.Pixel, image_attr)

    picResult.Image = bm
    gr.Dispose()
    bm_src.Dispose()
    image_attr.Dispose()
End Sub
 
Note that this method only remaps exact color matches. If you change blue pixels with red, green, and blue components (0, 0, 255) to some other color, then a pixel with color (0, 0, 254) will not be changed even though the two colors look the same on the screen.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated