Click here to Skip to main content
13,510,356 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 11 Jan 2007

Alpha blending two bitmaps using color matrix and imaging attributes

, 17 Jan 2007
Rate this:
Please Sign up or sign in to vote.
Here is another alpha blend that is truly a blend at pixel level.


I know there are numerous examples of alpha blending stuff, but many are not true blends, such as the in one of my articles. Others call managing transparencies as blends, and some are truly complicated transforms and merges.

I have been convinced there would be a simple .NET solution like 5 lines of code... This is it..

Public Sub SetAlphaRGB(ByRef abm As Image)

Dim bitm As New Bitmap(abm)

' I actually got help on this from using visual studio F1
' Initialize the color matrix.

Dim mxItems As 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, 128, 0}, _ Note that sets alpha at 128 50%
New Single() {0, 0, 0, 0, 1}}

Dim colorMatrix As New System.Drawing.Imaging.ColorMatrix(mxItems)

' Create an ImageAttributes object and set its color matrix.

Dim imageAtt As New System.Drawing.Imaging.ImageAttributes()

imageAtt.SetColorMatrix(colorMatrix, _
                        System.Drawing.Imaging.ColorMatrixFlag.Default, _

' look at properties of imageattributes also can set threshold and gamma
so this routine can handle most of your image manipulations

Dim r1 As RectangleF
r1.X = 0
r1.Y = 0
r1.Width = abm.Width
r1.Height = abm.Height

Dim r2 As RectangleF
r2.X = 0
r2.Y = 0
r2.Width = bm.Width
r2.Height = bm.Height 

graphics1 = Graphics.FromImage(bm)

Dim brush3 As New TextureBrush(bitm, r1, imageAtt)
brush3.WrapMode = Drawing2D.WrapMode.Tile ' if you want to tile else delete

graphics1.CompositingMode = Drawing2D.CompositingMode.SourceOver
graphics1.FillRectangle(brush3, r2) ' I didnt test but fill path should allow
                                    ' overlay in irregular polygon etc.

'draw image could be used, but if you want to tile a fill option is needed
graphics1.DrawImage(bitm, New Rectangle(0, 0, 1700, 1700), 0, 0,_
                    bitm.Width, bitm.Height, GraphicsUnit.Pixel, imageAtt)

MainForm.PictureBox1.Refresh() ' Required by my project
SetGraphics(MainForm.PictureBox1) ' Required by my project

End Sub

My granddaughter tiled over a DesignLab(c) 2007 design. This is at 128 Alpha (50%). This truly is about 5 lines of code. If you play with the image attributes, you can manage the threshold and gamma as well.

Sample image


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
Started in IT in 1959 working on Electronic accounting machines.

Worked through wiring panels and card systems.

1963-1965 USArmy
1961-1975 Computer Supervisor Contental Can
1975-1998 Data Force Incorporated - President
1998-2006 IT Director Promotions Unlimited Corp
2007 retired, project DesignLab(C)2007

Design and concept rules, coding is a required evil

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
projectzombie25-Oct-11 17:53
memberprojectzombie25-Oct-11 17:53 
GeneralC# version. (untested) Pin
LatencyXXX25-Feb-09 22:20
memberLatencyXXX25-Feb-09 22:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180417.1 | Last Updated 18 Jan 2007
Article Copyright 2007 by dmm12
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid