|
-
Oct 19th, 2014, 12:24 PM
#1
Thread Starter
Addicted Member
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:
Imports System.Drawing.Imaging
Public Class Form1
Dim bit As Bitmap = New Bitmap(My.Resources.test)
Private Function TintBitmap(b As Bitmap, color As Color, intensity As Single) As Bitmap
Dim b2 As New Bitmap(b.Width, b.Height)
Dim ia As New ImageAttributes
Dim m As ColorMatrix
m = New ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {color.R / 255 * intensity, color.G / 255 * intensity, color.B / 255 * intensity, 0, 1}})
ia.SetColorMatrix(m)
Dim g As Graphics = Graphics.FromImage(b2)
g.DrawImage(b, New Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, ia)
Return b2
End Function
Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
PictureBox1.Image = TintBitmap(bit, Color.Red, 0.5)
End Sub
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.
-
Oct 19th, 2014, 04:28 PM
#2
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
Last edited by boops boops; Oct 22nd, 2014 at 04:10 AM.
-
Oct 20th, 2014, 08:47 PM
#3
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|