代码之家  ›  专栏  ›  技术社区  ›  LiamGu

在vb.net中通过编程在文本上创建带有放置阴影的图像

  •  1
  • LiamGu  · 技术社区  · 14 年前

    我目前正在为一个我正在进行的项目构建一个内部网引擎,我希望尽可能通过从代码生成头图像来节省一些时间,但是,我希望它与我们的概念图像相匹配。

    我想达到以下目标:

    我的问题是我还不知道如何从代码中创建它。我可以做一些基本的工作,但就是这样。

    当涉及到渐变背景和文本上的阴影时,我开始下降。我可以将文本定位在较大的页眉图像上,因此如果无法生成我在那里的确切渐变,那么我有一个解决方法,但我真正想要实现的是使用字体和放置阴影的文本。

    我想说,假设使用“非标准”字体是安全的,我只需要在Web服务器上安装它?

    感谢您提前提供任何帮助。

    我希望实现以下目标:

    Some Text

    我的问题是我还不知道如何从代码中创建它。我可以做一些基本的工作,但就是这样。

    当涉及到渐变背景和文本上的阴影时,我开始下降。我可以将文本定位在更大的标题图像上,因此如果无法生成在那里的确切渐变,那么我有一个解决方案,但我真正想要实现的是字体和阴影的文本。

    我想说,假设使用“非标准”字体是安全的,我只需要在Web服务器上安装它?

    感谢您的帮助。

    1 回复  |  直到 11 年前
        1
  •  4
  •   Alex Essilfie    14 年前

    这是执行该任务的代码,但它是针对WinForms的。将它应用到Web服务器应该不难:

    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.windows.Forms
    
    Public Class Form1
    
        Sub Form1_Paint(ByVal sender As Object, _
                        ByVal e As PaintEventArgs) Handles MyBase.Paint
    
            'g is the graphics context used to do the drawing.'
            'gp is the path used to draw the circular gradient background'
            'f is a generic font for drawing'
    
            Using g = e.Graphics, gp As New GraphicsPath(), _
                  f As New Font(FontFamily.GenericSansSerif, 20, FontStyle.Bold)
    
                'add the ellipse which will be used for the '
                'circular gradient to the graphics path '
                gp.AddEllipse(Me.ClientRectangle)
    
                'then create a path gradient brush from the graphics path '
                'created earlier to do the drawing on the background      '
    
                Using pgb As New PathGradientBrush(gp)
                    'set the center colour '
                    pgb.CenterColor = Color.White
                    'and then make all the colours around it a different colour '
                    pgb.SurroundColors = New Color() {Color.LightSteelBlue}
    
                    'fill a rectangle with the border colour of the gradient brush'
                    g.FillRectangle(Brushes.LightSteelBlue, Me.ClientRectangle)
                    'and then draw the gradient on top'
                    g.FillRectangle(pgb, Me.ClientRectangle)
    
                    'The secret to shadowed text is that the shadow is drawn first'
                    'and it is usually offset to the lower right of the main text '
                    'so we draw the shadow with a shade of grey                   '
                    g.DrawString("SOME TEXT", f, Brushes.Gray, 12, 12)
                    'after which we draw the text itself'
                    g.DrawString("SOME TEXT", f, Brushes.Black, 10, 10)
                End Using
            End Using
        End Sub
    End Class
    

    上面的代码直接绘制到表单上。 如果要改为绘制图像,请按以下方式修改代码:

    Function GetImage(....) As Image
        Dim bmp As New Bitmap(200,200) 'you may use any size here'
        Dim bmpRect As New Rectangle(Point.Empty, bmp.Size)
    
        Using g = Graphics.FromImage(bmp), ...
            .....
        End Using
    
        return bmp
    End Sub
    

    一定要用 bmpRect 而不是 Me.ClientSize .

    我希望这能奏效,因为这完全是Winforms。