返回首页

简介
我知道有alpha混合的东西的例子不胜枚举,但许多人并不真正的混合物,如在我的文章之一,。有人说,作为混合管理的透明胶片,和一些真正复杂的转换和合并。
我一直坚信会有一个简单的。NET解决方案,如5行代码... ...这是它..

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

我的孙女平铺超过一个DesignLab(C)2007设计的。这是在128阿尔法(50%)。这确实是约5行代码。如果你玩的图像属性,您可以管理以及阈值和γ。
{S0}

回答

评论会员:projectzombie 时间:2011/12/07
我用彩色矩阵的例子,从这个程式码范例和启发你的代码,我内置的alpha混合的在线版本,可以在查看BR}
修改11月4日'11
评论会员:。LatencyXXX 时间:2011/12/07
{C} 这是一个很酷的想法,所以我想我会重写它。虽然GRP,绘图,及BM被冷落,所以我认为它们是本地类。没有确定的Dispose()在这种情况下。 GC应该处理反正没有。

延时