View Single Post
 
Old 04-17-2013, 06:22 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,030
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