Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Make a transparent bitmap for a texture brush or blend two bitmaps

0.00/5 (No votes)
11 Jan 2007 1  
A small amount of code can simulate transparencies whenever you need them without managing alpha channels.

Introduction

I searched a lot for an easy way to merge two bitmaps using .NET code. I always get referred back to GDI bitblt, which isn't friendly to .NET style images. Or managing alpha channels. I suppose I missed something...? Finally, I devised a simple method to apply variable transparency to any .NET bitmap. This doesn't actually blend colors between two bitmaps in the real sense, but sets a percentage of transparency in bitmap A, which when used as a texture brush applied to bitmap B, dose a pretty good job.

Specifying the correct rectangles would allow the typical overlays. This example tiles overlay when you view screen 2. That is the code that uses this brush, which is not shown here. This code represents transparency as white. This code allows you to blend bitmaps without managing alpha channels.

Confusing that the Windows color set of 16 million hasn't reserved a transparent color. The Bitmap.MakeTransparent allows you to assign the transparent color. This code will assign transparent color in a nice blend that works.

This code is easy, and works quickly. Just use the bitmap from this code as a texture brush with bitmap.maketransparency(system.drawing.color.white).

Public Sub SetAlphaVary(ByRef abm As Image)
' This code is from DesignLab(C)2007 author Dominic J. Melfi
' It may be used freely as long as the above line is included
=====================================================

' abm is passed as the "Source Image"

' size of source 

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

'
' Set brush into graphics context
graphics1.Dispose()
graphics1 = Graphics.FromImage(abm)

' Dim Path
Dim path1 As New System.Drawing.Drawing2D.GraphicsPath
path1.FillMode = Drawing2D.FillMode.Alternate
path1.AddRectangle(r1)

'Dim brush
' mainform.alphapercent.text argument is 6-17 code for hatch percent
' we are establishing the percent of transparency for our overlay
' actually you cold use any of 52 hatch codes 6-17 are without pattern, 
' and represent a percent

' the forecolor is white, and white will be used as transparent color 
' when this brush is overlayed
' you could pass the transparent color from the other bitmap
' and the backcolor is transparent which retains origional for these pixels

Dim brush3 As New Drawing2D.HatchBrush _
(CInt(getarg2(MainForm.AlphaPercent.Text, "code")), _
System.Drawing.Color.White, System.Drawing.Color.Transparent)

graphics1.FillPath(brush3, path1)
brush3.Dispose()
path1.Dispose()

' the end result here is you have a brush with a percent of white, 
' this is added to any existing white

' that there was, when using this brush declare
' image.maketransparent=system.drawing.color.white

End Sub

The images below are from my project DesignLab(c) 2007. This program can manage bitmaps up to 7200x7200, and has been used to print out designs as large as 56"x17" on my Epson7800. We have printed designs to Silk for Silk Scarves etc.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here