【问题标题】:Output text as GIF or PNG for use in eBook将文本输出为 GIF 或 PNG 以用于电子书
【发布时间】:2009-02-26 01:25:52
【问题描述】:

我的目标是创建一本电子书,我可以使用我的黑莓手机上的 Mobipocket 阅读器阅读。问题是我的文本包含黑莓不支持的 UTF-8 字符,因此显示为黑框。

电子书将包含英语和旁遮普语单词列表供参考,例如:

bait          ਦਾਣਾ
baked       ਭੁੰਨਿਆ
balance     ਵਿਚਾਰ

我的一个想法是将列表写入 HTML 表格,并将旁遮普语转换为 GIF 或 PNG 文件。然后将此 HTML 文件包含在电子书中。目前所有的词都存在于一个访问数据库中,但可以很容易地导出到另一种形式以输入到生成例程中。

问题:使用 VB、VBA 或 C#,编写例程创建图像然后在表格中输出包含英文单词和图像的 HTML 文件会有多难

【问题讨论】:

    标签: vb.net utf-8 image-conversion mobipocket


    【解决方案1】:

    Python 中有一些简单的库可以处理这类问题。但是我不确定是否有一个简单的 VB/C# 解决方案。

    使用 python,您可以使用 PIL library 和类似的代码(我找到了 here):

    # creates a 50x50 pixel black box with hello world written in white, 8 point Arial text
    import Image, ImageDraw, ImageFont
    
    i = Image.new("RGB", (50,50))
    d = ImageDraw.Draw(i)
    f = ImageFont.truetype("Arial.ttf", 8)
    d.text((0,0), "hello world", font=f)
    i.save(open("helloworld.png", "wb"), "PNG")
    

    如果您已经熟悉其他语言,Python 应该很容易上手,而且与 VB/C# 不同,它几乎可以在任何平台上运行。 Python 还可以帮助您生成 HTML 以配合生成的图像。 here 有一些例子。

    【讨论】:

    • 不错的详尽答案——带引用
    • 哇,看起来很简单,几乎值得安装 python,只是为了尝试一下。
    【解决方案2】:

    使用 VB

    Sub createPNG(ByVal pngString As String, ByVal pngName As String)
    
    ' Set up Font
    Dim pngFont As New Font("Raavi", 14)
    
    ' Create a bitmap so we can create the Grapics object 
    Dim bm As Bitmap = New Bitmap(1, 1)
    Dim gs As Graphics = Graphics.FromImage(bm)
    
    ' Measure string.
    Dim pngSize As SizeF = gs.MeasureString(pngString, pngFont)
    
    ' Resize the bitmap so the width and height of the text 
    bm = New Bitmap(Convert.ToInt32(pngSize.Width), Convert.ToInt32(pngSize.Height))
    
    ' Render the bitmap 
    gs = Graphics.FromImage(bm)
    gs.Clear(Color.White)
    gs.TextRenderingHint = TextRenderingHint.AntiAlias
    gs.DrawString(pngString, pngFont, Brushes.Firebrick, 0, 0)
    gs.Flush()
    
    
    'Saving this as a PNG file
    Dim myFileOut As FileStream = New FileStream(pngName + ".png", FileMode.Create)
    bm.Save(myFileOut, ImageFormat.Png)
    myFileOut.Close()
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 2016-01-06
      • 2011-07-16
      • 1970-01-01
      • 2011-05-11
      • 2023-03-13
      相关资源
      最近更新 更多