The fastest way to manipulate alpha values in a bitmap? Help please?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi
This is a follow on from a recent thread http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6ca7cb0f-b4cf-4d12-bab6-f0319efd136c
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6ca7cb0f-b4cf-4d12-bab6-f0319efd136c
This thread is also interesting http://social.msdn.microsoft.com/forums/en-us/netfxbcl/thread/225D77D7-4D1E-4F11-ACC5-5EF3B8FDB96A
http://social.msdn.microsoft.com/forums/en-us/netfxbcl/thread/225D77D7-4D1E-4F11-ACC5-5EF3B8FDB96A
I am hoping to find the best .net method currnetly available of fading an bitmap image against a non uniform background. Hopefully the contributors to this thread can give runnable code examples - but all responses are welcome. The following is
just a beginning example to show what I mean. Put a button on a form with the following code. Double-buffering Off.

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Class Form5

<span style="color:Blue; Dim I% : <span style="color:Blue; Dim J% : <span style="color:Blue; Dim K%
<span style="color:Blue; Dim ntgr(0 <span style="color:Blue; To 10) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim btmp(0 <span style="color:Blue; To 4) <span style="color:Blue; As Bitmap
<span style="color:Blue; Dim destPoints <span style="color:Blue; As Point() = {<span style="color:Blue; New Point(0, 60), <span style="color:Blue; New Point(310, 0), <span style="color:Blue; New Point(10, 390)}
<span style="color:Blue; Dim pnt(0 <span style="color:Blue; To 4) <span style="color:Blue; As Point
<span style="color:Blue; Dim colr(0 <span style="color:Blue; To 5) <span style="color:Blue; As Color
<span style="color:Blue; Dim backgroundBitmap <span style="color:Blue; As Bitmap = <span style="color:Blue; New Bitmap(500, 500)
<span style="color:Blue; Dim grfx(0 <span style="color:Blue; To 2) <span style="color:Blue; As Graphics
<span style="color:Blue; Dim rctngl(0 <span style="color:Blue; To 2) <span style="color:Blue; As Rectangle
<span style="color:Blue; Dim flag <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Dim sw <span style="color:Blue; As Stopwatch

<span style="color:Blue; Private <span style="color:Blue; Sub Form5_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; Me.Load

<span style="color:Green; create an image for the sake of this exercise
btmp(0) = <span style="color:Blue; New Bitmap(64, 64)
<span style="color:Blue; For K = 15 <span style="color:Blue; To 255 <span style="color:Blue; Step 16
ntgr(0) = <span style="color:Blue; CInt(Int((K - 15) / 64) * 16)
ntgr(1) = ((K - 15) <span style="color:Blue; Mod 64)
ntgr(2) = 0
<span style="color:Blue; For J = 15 <span style="color:Blue; To 255 <span style="color:Blue; Step 16
ntgr(3) = 0
<span style="color:Blue; For I = 15 <span style="color:Blue; To 255 <span style="color:Blue; Step 16
btmp(0).SetPixel(ntgr(0) + ntgr(2), ntgr(1) + ntgr(3), Color.FromArgb(255, I, J, K))
ntgr(3) += 1
<span style="color:Blue; Next I
ntgr(2) += 1
<span style="color:Blue; Next J
<span style="color:Blue; Next K
btmp(1) = <span style="color:Blue; New Bitmap(350, 400)
grfx(2) = Graphics.FromImage(btmp(1))
grfx(2).DrawImage(btmp(0), destPoints)
btmp(0).Dispose()
grfx(2).Dispose()
btmp(3) = <span style="color:Blue; CType(btmp(1).Clone, Bitmap)

<span style="color:Green; Create a gradient color background for the sake of this exercise
<span style="color:Blue; Dim path <span style="color:Blue; As <span style="color:Blue; New Drawing2D.GraphicsPath()
rctngl(0) = <span style="color:Blue; New Rectangle(0, 0, 500, 500)
path.AddRectangle(rctngl(0))
<span style="color:Blue; Dim pthGrBrush <span style="color:Blue; As <span style="color:Blue; New Drawing2D.PathGradientBrush(path)
pthGrBrush.CenterPoint = <span style="color:Blue; New PointF(330, 400)
colr(0) = Color.FromArgb(255, 240, 140, 140) <span style="color:Green; reddish color
colr(1) = Color.FromArgb(255, 230, 230, 140) <span style="color:Green; yellowish color
colr(2) = Color.FromArgb(255, 140, 140, 240) <span style="color:Green; blueish color
colr(3) = Color.FromArgb(255, 140, 240, 140) <span style="color:Green; greenish color
pthGrBrush.CenterColor = colr(0)
<span style="color:Blue; Dim colors <span style="color:Blue; As Color() = {colr(1), colr(2), colr(3)}
pthGrBrush.SurroundColors = colors
grfx(0) = Graphics.FromImage(backgroundBitmap)
grfx(0).FillRectangle(pthGrBrush, rctngl(0))

<span style="color:Green; set form details
<span style="color:Blue; Me.Width = <span style="color:Blue; Me.Width - <span style="color:Blue; Me.ClientSize.Width + 500
<span style="color:Blue; Me.Height = <span style="color:Blue; Me.Height - <span style="color:Blue; Me.ClientSize.Height + 500
<span style="color:Blue; Me.BackgroundImage = backgroundBitmap
grfx(1) = <span style="color:Blue; Me.CreateGraphics
<span style="color:Green; capture background behind proposed image
rctngl(1) = <span style="color:Blue; New Rectangle(90, 30, 350, 400)
btmp(2) = backgroundBitmap.Clone(rctngl(1), Imaging.PixelFormat.DontCare)
<span style="color:Green; draw image
pnt(0).X = 90 : pnt(0).Y = 30
grfx(0).DrawImage(btmp(1), pnt(0))
<span style="color:Green; set button1 details
pnt(1).X = 50 : pnt(1).Y = 450
Button1.Location() = pnt(1)
Button1.AutoSize = <span style="color:Blue; True
Button1.Text = <span style="color:#A31515; "Fade image away"
sw = <span style="color:Blue; New Stopwatch
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
<span style="color:Blue; If flag <span style="color:Blue; Then
grfx(0).DrawImage(btmp(1), pnt(0))
grfx(1).DrawImage(backgroundBitmap, rctngl(1), rctngl(1), GraphicsUnit.Pixel)
Button1.Text = <span style="color:#A31515; "Fade image away"
btmp(3) = <span style="color:Blue; CType(btmp(1).Clone, Bitmap)
sw.Reset()
flag = <span style="color:Blue; False
<span style="color:Blue; Else
sw.Start()
<span style="color:Blue; For I = 224 <span style="color:Blue; To 0 <span style="color:Blue; Step -32
grfx(0).DrawImage(btmp(2), pnt(0))
<span style="color:Blue; For J = 0 <span style="color:Blue; To 349
<span style="color:Blue; For K = 0 <span style="color:Blue; To 399
colr(4) = btmp(3).GetPixel(J, K)
<span style="color:Blue; If colr(4).A > I <span style="color:Blue; Then
colr(5) = Color.FromArgb(I, colr(4).R, colr(4).G, colr(4).B)
btmp(3).SetPixel(J, K, colr(5))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next K
<span style="color:Blue; Next J
grfx(0).DrawImage(btmp(3), pnt(0))
grfx(1).DrawImage(backgroundBitmap, rctngl(1), rctngl(1), GraphicsUnit.Pixel)
<span style="color:Blue; Next I
sw.<span style="color:Blue; Stop()
Button1.Text = Str(sw.ElapsedMilliseconds) + <span style="color:#A31515; " milliseconds (8 Frames) . Redo?."
flag = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]

Next example I hope to use the BitmapData.Scan0 method - which I think will be much faster. <hr class="sig Leon C Stanley - - A dinky di VBer - -

View the full article
 
Back
Top