Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / Visual Basic

Alpha blending two bitmaps using color matrix and imaging attributes

Rate me:
Please Sign up or sign in to vote.
2.52/5 (9 votes)
17 Jan 2007CPOL 31.7K   17   2
Here is another alpha blend that is truly a blend at pixel level.

Introduction

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..

VB
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, _
                        System.Drawing.Imaging.ColorAdjustType.Bitmap)

' 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.Dispose()
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)

brush3.Dispose()
graphics1.Dispose()
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

License

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


Written By
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


Comments and Discussions

 
GeneralMy vote of 5 Pin
projectzombie25-Oct-11 17:53
projectzombie25-Oct-11 17:53 
GeneralC# version. (untested) Pin
LatencyXXX25-Feb-09 22:20
LatencyXXX25-Feb-09 22:20 
public void AlphaBlend(Graphics grp, Bitmap abm, Bitmap bm) {
  // Initialize the color matrix.

  float [][] mxItems = new float[][] {
	new float[] {1, 0, 0, 0, 0},
	new float[] {0, 1, 0, 0, 0},
	new float[] {0, 0, 1, 0, 0},
	new float[] {0, 0, 0, 128, 0}, // Note that sets alpha at 128 50%
	new float[] {0, 0, 0, 0, 1}
  };

  System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(mxItems);

  // Create an ImageAttributes object and set its color matrix.

  ImageAttributes imageAtt = new ImageAttributes();
  imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

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

  RectangleF r1 = new RectangleF(0, 0, abm.Width, abm.Height),
	    r2 = new RectangleF(0, 0, bm.Width, bm.Height);


  grp.Dispose();
  grp = Graphics.FromImage(bm);

  Bitmap bitm = new Bitmap(abm);
  TextureBrush brush3 = new TextureBrush(bitm, r1, imageAtt);
  brush3.WrapMode = WrapMode.Tile; // if you want to tile else delete

  grp.CompositingMode = CompositingMode.SourceOver;
  grp.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
  grp.DrawImage(bitm, new Rectangle(0, 0, 1700, 1700), 0, 0, bitm.Width, bitm.Height, GraphicsUnit.Pixel, imageAtt);

  brush3.Dispose();
  grp.Dispose();
}



This was a cool idea, so I throught I would rewrite it. Although the grp, graphics1, and bm were left out so I assumed they were local to the class. Not sure about the Dispose() either in this case. GC should handle anyways without.

-Latency

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.