Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
Go Back  Xtreme Visual Basic Talk > > > Changing Color of Simple Bitmaps


Reply
 
Thread Tools Display Modes
  #1  
Old 04-17-2013, 05:00 PM
Ninth Wave Ninth Wave is offline
Freshman
 
Join Date: Nov 2005
Location: Australia
Posts: 35
Smile Changing Color of Simple Bitmaps


If I load a bitmap let's say it has only two colors, white pixels on black pixels.

Code:
Dim myBitmap As Bitmap
myBitmap = Bitmap.FromFile("file.bmp")
http://i.imgur.com/9CjAXuW.png


Could I change the foreground white pixels to another color at runtime?
Reply With Quote
  #2  
Old 04-17-2013, 06:22 PM
passel's Avatar
passelChanging Color of Simple Bitmaps passel is offline
Sinecure Expert

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

There are a number of ways it could be done.
One way is to use a colorMatrix to adjust the red,green and blue levels when you draw the bitmap to another place, eg. the form, another control, or another bitmap.

This example code uses a colorMatrix to turn the white X into another color set by using three trackbars to adjust the level of Red,Green,Blue (color matrix expects range 0.0 to 1.0)
Assumes you have the picture in the application directory.
Code:
Imports System.Drawing.Imaging

Public Class Form1
  Dim myBitmap As New Bitmap("file.bmp")
  Dim redScale As Single = 1.0F
  Dim greenScale As Single = 1.0F
  Dim bluescale As Single = 1.0F

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    TrackBar1.Maximum = 255 : TrackBar1.TickFrequency = 16
    TrackBar2.Maximum = 255 : TrackBar2.TickFrequency = 16
    TrackBar3.Maximum = 255 : TrackBar3.TickFrequency = 16
  End Sub

  Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    Dim cm As New ColorMatrix
    cm.Matrix00 = redScale
    cm.Matrix11 = greenScale
    cm.Matrix22 = bluescale
    Dim imageAtt As New ImageAttributes()
    imageAtt.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
    Dim iWidth As Integer = myBitmap.Width
    Dim iHeight As Integer = myBitmap.Height

    e.Graphics.DrawImage(myBitmap,
       New Rectangle(0, 0, iWidth, iHeight),
       0.0F, 0.0F,
       iWidth, iHeight, GraphicsUnit.Pixel, imageAtt)
    imageAtt.Dispose()
  End Sub

  Private Sub TrackBar1_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBar1.Scroll
    redScale = TrackBar1.Value / 255.0F
    PictureBox1.Refresh()
  End Sub

  Private Sub TrackBar2_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBar2.Scroll
    greenScale = TrackBar2.Value / 255.0F
    PictureBox1.Refresh()
  End Sub

  Private Sub TrackBar3_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBar3.Scroll
    bluescale = TrackBar3.Value / 255.0F
    PictureBox1.Refresh()
  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.
Reply With Quote
  #3  
Old 04-21-2013, 09:42 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

You never cease to amaze me passel... I had to add this to find the bmp

Code:
        Dim ofdFile As New OpenFileDialog With {.Filter = "Bitmap |*.bmp"}
        If (ofdFile.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            myBitmap = New Bitmap(ofdFile.FileName)
        End If
But I can't see where you are specifying it is the "X" in the bmp you want to work on. Is there a specific line of code you can point out?
Reply With Quote
  #4  
Old 04-22-2013, 10:53 AM
passel's Avatar
passelChanging Color of Simple Bitmaps passel is offline
Sinecure Expert

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

The color matrix is technically modifying the whole bitmap, but since the bitmap is made up of Black and White, you only see the White X being affected.

What the color matrix is doing scaling the red, green and blue portions of all the pixels to a value between 0 and 100 percent of themselves.
Since Black has Red, Green and Blue componenets with values of zero, any percentage of 0 is still 0.
Since White is made up of 100% Red, Green and Blue, then any percentage given to any of those three values will reduce that component by that amount directly affecting the result of the white color.

If you were to use this example with a bitmap that wasn't strickly Black and White, then the results would be quite different, but generally predictable. The code can only reduce a given color's components, so if you reduced the Red component by 50%, pixels that were pure red would be darkened. Colors with Red in them, such as yellow, would shift to a color with less red, towards brown. Colors with no red in them, such as Cyan or Green, would not be affected.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 04-22-2013, 07:15 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Ok thanks that makes a lot of sense when you have someone who understands colour explain it

I'm afraid my solution would have been much more primitive, with an array of different coloured bitmaps. Yours lacks that sledgehammer approach
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
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
 
Changing Color of Simple Bitmaps
Changing Color of Simple Bitmaps
 
-->