動態文字轉圖檔範例


範例網址:
 
程式碼可在微軟技術社群中找到,基本上是回覆網友提問,就寫成這個樣子了:
 
測試者可任意輸入字串,並選擇 Server 端有的字型,輸入字型尺寸、前景色、背景色、選擇圖檔格式,選擇開啟方式即可。
 
Server 端字型採用列舉方式,顏色支援透明層次的 ARGB ,A 為 FF 時不透明, 00 為透明,中間為半透明,背景色若不輸入,完全空白時,不填入背景色。
 
圖檔格式支援 BMP/GIF/EMF/JPG/PNG/TIF/WMF ,也就是所有 ASP.NET 預設支援圖檔,BMP/JPG 不支援透明,GIF 部分情況可透明,EMF/WMF 為向量檔,所以所選擇的字型必須在 client 端有才有效。
 
為了方便測試者存檔,不會讓 IE 依預設值直接開啟,提供選擇存檔對話盒,若需討論,可在上述技術社群網站討論,或於下列討論區討論:
 
延伸的應用可自己考慮,比如說計數器、動態確認碼(HiNet 就是再上面加一堆線條來干擾自動註冊機)等,如果要測試條碼,請選擇條碼字型:
C39HrP24DhTt
C39HrP24DlTt
C39HrP36DlTt
C39HrP48DhTt
barcode font
… 等
Categories: 更新與回報 | 13 則迴響

文章分頁導航

13 thoughts on “動態文字轉圖檔範例

  1. Robbin

    你好,程式碼找不到耶,請問您
    http://tlcheng.twbbs.org/aspx/Tools/Count/FontEnum.aspx
    可以提供給我參考嗎

  2. 子璉

    文字轉圖檔:
       Public Function ResponseText()
             Dim boxHeight As Double = 1
          Dim boxWidth As Double = 1
          Dim strBackgroundColor = Request("BackgroundColor")
          Dim strFontName As String = GetCommandDefault(Request("FontName"), "Bauhaus 93")
          Dim dFontSize As Single = CSng(GetCommandDefault(Request("FontSize"), 12))
          Dim ifnColor As Integer = HexToObject(GetFullColorString(GetCommandDefault(Request("FontColor"), "FF00")), TypeCode.Int32, True)
          Dim iBgColor As Integer
          Dim strNumber As String = GetCommandDefault(Request("String"), "1234567890")
          Dim strExtName As String = GetCommandDefault(Request("ExtName"), "png")
          Dim bSaveFile As Boolean = CBool(GetCommandDefault(Request("rdoSaveFile"), "False"))
          Dim strFileName As String = "text." & strExtName
          Dim strContentType As String = "image/" & strExtName
                Dim pic As Object
          Dim bmp As New System.Drawing.Bitmap(boxWidth, boxHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
          Dim grfx As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
          Dim axFont As New System.Drawing.Font(strFontName, dFontSize)
          Dim lblSize As System.Drawing.SizeF
          Dim bgColor, frColor, axColor, ptColor As System.Drawing.Brush
          Dim ptf As New System.Drawing.PointF(0, 0)
          Dim strfmt As System.Drawing.StringFormat = System.Drawing.StringFormat.GenericTypographic
          strfmt.FormatFlags = strfmt.FormatFlags Or System.Drawing.StringFormatFlags.MeasureTrailingSpaces
          If Len(strBackgroundColor) > 0 Then
       strBackgroundColor = GetFullColorString(strBackgroundColor)
       iBgColor = HexToObject(strBackgroundColor, TypeCode.Int32, True)
       bgColor = myCreateSolidBrushArgb(iBgColor)
      End If
                axColor = myCreateSolidBrushArgb(ifnColor)
          With grfx
             lblSize = .MeasureString(strNumber, axFont, ptf, strfmt)
             ‘boxHeight = axFont.GetHeight(grfx)
             boxHeight = lblSize.Height
             boxWidth = lblSize.Width
             .Dispose()
          End With
          bmp.Dispose()
          Select Case LCase(strExtName)
       Case "emf", "wmf"
            Dim emf As New cMetaFile
        emf.CreatePictureGraphics()
        grfx = emf.MyCreateMetaFileGraphics()
            pic = emf
       Case Else
          bmp = New System.Drawing.Bitmap(boxWidth, boxHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
        grfx = System.Drawing.Graphics.FromImage(bmp)
        pic = bmp
      End Select
          With grfx
           If Len(strBackgroundColor) > 0 Then
        .FillRectangle(bgColor, 0, 0, CSng(boxWidth), CSng(boxHeight))
       End If
             .DrawString(strNumber, axFont, axColor, ptf, strfmt)
             .Dispose()
          End With
      Dim imgFormat As System.Drawing.Imaging.ImageFormat = GetImageFormat(strExtName)
          Select Case LCase(strExtName)
       Case "emf", "wmf"
        ResponseFile(pic.ToArray(), strContentType, strFileName, bSaveFile)
        pic.Dispose()
       Case Else
        Dim memStream As New System.IO.MemoryStream
                pic.Save(memStream, imgFormat)
        ResponseFile(memStream.ToArray(), strContentType, strFileName, bSaveFile)
      End Select
          bmp.Dispose()
             End Function

  3. Unknown

    璉璉大大:
      請教一下,我利用VB的語法想繪出線條與文字,可是在宣告上似乎有問題,有否請大大指點迷津,謝謝!
       Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        ‘在這裡放置使用者程式碼以初始化網頁        Dim BitMap1 As New Bitmap(800, 400)        Dim g As Graphics = Graphics.FromImage(BitMap1)        g.Clear(Color.Yellow)        Dim Bluepen As New System.Drawing.Pen(Color.Blue, 5)        Dim Redpen As New System.Drawing.Pen(Color.Red, 7)        Dim F As New System.Drawing.Font("arial", 10)        Dim b As New System.Drawing.Brushes(Color.Red)        g.DrawString("這是測試", F, b, 90, 20)        g.DrawLine(Bluepen, 10, 12, 100, 12)        g.DrawLine(Redpen, 100, 42, 380, 42)        BitMap1.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
        End Sub
    c:\inetpub\wwwroot\cgd\graph.aspx.vb(33): 多載解析失敗,因為具備這些引數的 ‘DrawString’ 不存在,故無法呼叫:    ‘Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, layoutRectangle As System.Drawing.RectangleF, format As System.Drawing.StringFormat)’: 型別 System.Drawing.Brushes 的值無法轉換成 System.Drawing.Brush。    ‘Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, layoutRectangle As System.Drawing.RectangleF, format As System.Drawing.StringFormat)’: 型別 Integer 的值無法轉換成 System.Drawing.RectangleF。    ‘Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, layoutRectangle As System.Drawing.RectangleF, format As System.Drawing.StringFormat)’: 型別 Integer 的值無法轉換成 System.Drawing.StringFormat。    ‘Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, point As System.Drawing.PointF, format As System.Drawing.StringFormat)’: 型別 System.Drawing.Brushes 的值無法轉換成 System.Drawing.Brush。    ‘Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, point As System.Drawing.PointF, format As System.Drawing.StringFormat)’: 型別 Integer 的值無法轉換成 System.Drawing.PointF。    ‘Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, point As System.Drawing.PointF, format As System.Drawing.StringFormat)’: 型別 Integer 的值無法轉換成 System.Drawing.StringFormat。    ‘Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, x As Single, y As Single)’: 型別 System.Drawing.Brushes 的值無法轉換成 System.Drawing.Brush。c:\inetpub\wwwroot\cgd\graph.aspx.vb(32): 無法在此內容中存取 ‘System.Drawing.Brushes.Private Sub New()’,因為它是 ‘Private’。
     

  4. 子璉

    你的錯誤訊息有說:「型別 System.Drawing.Brushes 的值無法轉換成 System.Drawing.Brush」
    所以可以推得你的宣告是錯誤的:
    Dim b As New System.Drawing.Brushes(Color.Red)請試著改為:
    Dim b As New System.Drawing.SolidBrush(Color.Red)
    你在這邊貼大量的原始碼與錯誤訊息,感覺有點亂,且你在討論區有留言了,建議你在討論區發文即可,在 spaces 上,因為版面有限,不太適合貼大量程式碼。

  5. Martin Lin

    您好~
    關於 http://tlcheng.twbbs.org/aspx/Tools/Count/FontEnum.aspx 範例程式.. 是否願不吝提供原始程式(全) .. 
    主要是對於您做的透明背景部份很感到好奇與興趣.  希望您提供給予參考.  謝謝您~
    (研究好幾天.就是搞不出來,都變成黑色背景了)

  6. Martin Lin

    您好…
    透明背景已經找出原因了….
    現在只差…. 轉成EMF檔的問題…
    頭快爆炸了…
    想請教您… 轉EMF檔要啥技巧才能轉呢?
    提供下列程式碼…
    除了PNG 要改成EMF…..
    METAFILE…. 不知道該怎弄… 麻煩您幫忙一下…  感謝.
     
    <%@ Page Language="VB" Debug="true" ContentType="image/png"%><%@ Import Namespace="System.Drawing" %><%@ Import Namespace="System.Drawing.Text" %><%@ Import Namespace="System.Drawing.Imaging" %><%@ Import Namespace="System.Drawing.Drawing2D" %><%@ Import Namespace="System.Web.UI.Page" %><%@ Import Namespace="NZlib.GZip" %>
    <script language=vb runat=server id="modResponseFile">Sub Page_Load(Sender As Object, E As EventArgs)    Dim stmMemory As New System.IO.MemoryStream
        Dim BitMap1 As New Bitmap(800, 400,System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
        Dim g As System.Drawing.Graphics  = System.Drawing.Graphics.FromImage(BitMap1) 
        g.Clear(Color.FromArgb(0,255,255,255))    Dim blueBrush As New SolidBrush(Color.Transparent)
        Dim F As New System.Drawing.Font("中國龍海報體", 140)    Dim b As New System.Drawing.SolidBrush(Color.Red)    g.DrawString("這是測試", F,new SolidBrush(Color.RED), 0, 0)    Response.ContentType = "image/png"’    Dim inse As new Metafile(g)    BitMap1.Save(stmMemory, System.Drawing.Imaging.ImageFormat.png)    stmMemory.WriteTo(Response.OutputStream )End SUB</script>

  7. 子璉

    關於程式技術的討論請在公開討論區討論,例如微軟官方 MSDN 討論區已有些既有討論,可透過搜尋功能搜尋 MetaFile 部份內容:
    http://forums.microsoft.com/MSDN-CHT/Search/Search.aspx?words=MetaFile&localechoice=31748&SiteID=14&searchscope=allforums
    可在該網站對應討論區張貼文章討論。

  8. 子璉

    列舉字型部分:
     Public Function ResponseFontOptions()
      Dim fntName As Drawing.FontFamily
      For Each fntName In Drawing.FontFamily.Families
       Response.Write("<OPTION value=’" & fntName.Name & "’>" & fntName.Name & "</OPTION>" & vbNewLine)
      Next 
    End Function 

  9. ?

    請問璉璉大大:
    文字轉圖片的程式碼可否有完整的程式碼可參考?
    上面程式碼仍有些問題
    麻煩你
     
     

  10. 子璉

    ASP.NET 因為有 Graphics 類別可以處理文字轉圖檔。ASP 不行,請明確選擇用詞。
    既有範例請參考:http://tlcheng.spaces.live.com/blog/cns!145419920BFD55A7!397.entry
    若有任何疑問,請到下面討論區發表:http://forums.microsoft.com/MSDN-CHT/ShowForum.aspx?ForumID=236&SiteID=14
    發表前請先搜尋相關文章,比如說:http://forums.microsoft.com/MSDN-CHT/Search/Search.aspx?words=Bitmap+MemoryStream&localechoice=31748&SiteID=14&searchscope=forumscope&ForumID=236
    若有任何疑問,請貼錯誤訊息提供參考。
     

  11. ShengHan

    璉璉大大:
    請教一下,我利用VB的語法將String 轉換成圖檔
    例如:
    圖片測試一
    圖片測試二
    該如何撰寫呢?   可否教一下

  12. 璉璉前輩您好:
    請問您有使用過FO.NET來將FO檔案轉成PDF檔嗎?
    目前小弟使用一般的中文字可以順利轉出PDF檔,但如果FO檔中包含了Uincode的自造字,這時透過FO.NET來轉出來那個字便會變成□,有試了多使用 privateFont 來設定字型檔的路徑,也沒有效果。
    不知璉璉前輩能否指導一下呢? 謝謝您!

發表留言

在WordPress.com寫網誌.