Results 1 to 3 of 3

Thread: How to tint a bitmap... *Solved*

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    How to tint a bitmap... *Solved*

    Hello!
    I'm trying out new things on VB, and now I want to know how to tint a bitmap image. I've googled it, and I've found this:

    vb Code:
    1. Imports System.Drawing.Imaging
    2. Public Class Form1
    3.  
    4.     Dim bit As Bitmap = New Bitmap(My.Resources.test)
    5.  
    6.     Private Function TintBitmap(b As Bitmap, color As Color, intensity As Single) As Bitmap
    7.         Dim b2 As New Bitmap(b.Width, b.Height)
    8.  
    9.         Dim ia As New ImageAttributes
    10.  
    11.         Dim m As ColorMatrix
    12.         m = New ColorMatrix(New Single()() _
    13.             {New Single() {1, 0, 0, 0, 0}, _
    14.              New Single() {0, 1, 0, 0, 0}, _
    15.              New Single() {0, 0, 1, 0, 0}, _
    16.              New Single() {0, 0, 0, 1, 0}, _
    17.              New Single() {color.R / 255 * intensity, color.G / 255 * intensity, color.B / 255 * intensity, 0, 1}})
    18.  
    19.         ia.SetColorMatrix(m)
    20.         Dim g As Graphics = Graphics.FromImage(b2)
    21.         g.DrawImage(b, New Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, ia)
    22.         Return b2
    23.  
    24.     End Function
    25.  
    26.     Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    27.         PictureBox1.Image = TintBitmap(bit, Color.Red, 0.5)
    28.     End Sub
    29. End Class

    It works, but can someone please explain to me what this code means; what I am doing? What is a color matrix?
    Thanks!
    Last edited by NinjaNic; Oct 20th, 2014 at 08:48 PM.

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

    Re: How to tint a bitmap...

    Hi NinjaNic,

    A matrix is an array of numbers. Matrices have their own rules for adding, subtracting, multiplying etc. The ColorMatrix is an object in GDI+ which represents a 5*5 matrix. When you define a ColorMatrix and use it in a DrawImage statement, it can transform the colors of an image in many different ways.

    In the code you posted, the values in the bottom row of the ColorMatrix multiply the R, G and B values of each pixel by the value of Intensity. For example, the first value color.R/255 * Intensity multiplies the Red component of a color by the value of Intensity. It has to be divided by 255 because Color.R is on a scale of 0 to 255, but the value in the ColorMatrix has to be on a scale of 0 to 1. This may look like a complicated way of doing it, but the advantage of using the ColorMatrix in a DrawImage statement is that the values are applied to every pixel in the image.
    [much later edit, for future reference]I made a mistake in the above paragraph. The values in the bottom row are added to the existing R, G and B values instead of multiplying them (with a maximum of 1). I should have read the second link below more carefully.

    If you want to understand more about matrices and ColorMatrix, here are 3 links that may help you:

    Math Is Fun -- Introduction to Matrices

    MSDN -- How to: Translate Image Colors

    Code Project -- ColorMatrix Basics - Simple Image Color Adjustment

    On the other hand, if you want a simpler way of changing the general color of an image, you can achieve a certain amount just by painting a rectangle in a transparent color over the image. For example, you could reduce the brightness of an image (as in your code) by painting a transparent gray rectangle over the image.

    Code:
    e.Graphics.DrawImage(img, rect)
    Using sbr As New SolidBrush(Color.FromArgb((1 - Intensity) * 255, Color.Black))    
          e.Graphics.FillRectangle(sbr, rect)
    End Using
    (code not tested!)

    BB

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: How to tint a bitmap...

    Thanks for the help and the information on matrices.

Tags for this Thread

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