我尝试实现用渐变背景颜色绘制表单并与透明度重叠图像。
这可能吗?
我想使用具有透明背景的平铺背景图像,并使用自定义线性渐变绘制背景。
我做到了!,我想与您分享我的解决方案(这很容易):
外部帮助:使用图像平铺形状
Private Sub BackgroundGradient(ByRef Control As Object, _
ByVal Color1 As Drawing.Color, _
ByVal Color2 As Drawing.Color)
Dim vLinearGradient As Drawing.Drawing2D.LinearGradientBrush = _
New Drawing.Drawing2D.LinearGradientBrush(New Drawing.Point(Control.Width, Control.Height), _
New Drawing.Point(Control.Width, 0), _
Color1, _
Color2)
Dim vGraphic As Drawing.Graphics = Control.CreateGraphics
' To tile the image background - Using the same image background of the image
Dim vTexture As New Drawing.TextureBrush(Control.BackgroundImage)
vGraphic.FillRectangle(vLinearGradient, Control.DisplayRectangle)
vGraphic.FillRectangle(vTexture, Control.DisplayRectangle)
vGraphic.Dispose() : vGraphic = Nothing : vTexture.Dispose() : vTexture = Nothing
End Sub
下面介绍如何绘制渐变背景。平铺图像会很慢,除非你使用Windows API或其他东西。
Imports System.Drawing
Public Class frmBG
Private Sub frmBG_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim p1 As Point = Me.ClientRectangle.Location
Dim p2 As Point = New Point(Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
Using brsGradient As New System.Drawing.Drawing2D.LinearGradientBrush(p1, p2, Color.Red, Color.Blue)
g.FillRectangle(brsGradient, e.ClipRectangle)
g.DrawImage(My.Resources.demoImage, Me.ClientRectangle.Location)
End Using
End Sub
Private Sub frmBG_ResizeEnd(sender As Object, e As System.EventArgs) Handles Me.ResizeEnd
Me.Invalidate()
End Sub
End Class