4

我试图实现用渐变背景色绘制表单并用透明度重叠图像。

这个有可能?

我想使用具有透明背景的平铺背景图像,并使用自定义线性渐变绘制背景。

4

2 回答 2

8

我做到了!,我想与您分享我的解决方案(这很容易):

外部帮助:用图像平铺形状

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
于 2012-06-01T14:29:55.563 回答
4

这是绘制渐变背景的方法。除非您使用 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
于 2012-06-01T07:11:50.983 回答