【问题标题】:VB.Net Background gradient using LinearGradientBrush and display ImageVB.Net背景渐变使用LinearGradientBrush并显示图像
【发布时间】:2012-06-01 03:54:22
【问题描述】:

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

这可能吗?

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

【问题讨论】:

    标签: vb.net forms graphics


    【解决方案1】:

    我做到了!我想和你分享我的解决方案(很简单):

    外部帮助:Tile a Shape with an Image

    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
    

    【讨论】:

      【解决方案2】:

      这里是如何绘制渐变背景。除非您使用 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
      

      【讨论】:

      • 感谢回答,但我想在表单中实现平铺背景。另一种方法是创建具有大分辨率 (2000X2000) 的图像。但我认为应该存在一个简单有效的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      相关资源
      最近更新 更多