Click here to Skip to main content
15,895,777 members
Articles / Multimedia / GDI+

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

Rate me:
Please Sign up or sign in to vote.
1.86/5 (9 votes)
11 Jan 2007CPOL1 min read 37.2K   14   3
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).

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

Image 1

Image 2

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
Manoj Kumar Choubey17-Mar-12 4:40
professionalManoj Kumar Choubey17-Mar-12 4:40 
QuestionWhat's the matter? Pin
dherrmann9-Sep-07 6:26
dherrmann9-Sep-07 6:26 
GeneralErrors in IDE Pin
dherrmann21-Aug-07 21:32
dherrmann21-Aug-07 21:32 

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.