2010-11-01 11 views
1

我目前正在为一个项目构建一个Intranet引擎,我现在正忙着为它准备一个项目,尽可能从代码生成头部图像,但是,我希望它能匹配我们的概念图像。在VB.NET中以编程方式在文本上创建一个带有阴影的图像

我想什么来实现低于:

Some Text

我的问题是我根本不懂如何创建一个从码。我可以做绝对的基础知识,但这就是它。

当谈到文本上的渐变背景和阴影时,我开始摔倒。我可以避免将文本放置在较大的标题图像上,因此如果无法生成我在那里的确切渐变,那么我有一个解决方法,但我真正想要实现的是带有字体的文本并投下阴影。

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

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

回答

3

下面是执行任务的代码,但它是用于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。

+0

如果我决定不在图像中包含背景,我可以使文本图像透明吗? – LiamGu 2010-11-02 09:51:20

+0

将位图位图声明的行更改为'Dim bmp As New Bitmap(200,200,Imaging.PixelFormat.Format32bppArgb)'并跳过背景绘制代码。 – 2010-11-02 10:53:14

相关问题