Search code examples
vb.netutf-8image-conversionmobipocket

Output text as GIF or PNG for use in eBook


My goal is to create an eBook that I can read with the Mobipocket reader on my Blackberry. The problem is that my text includes UTF-8 characters which are not supported on the Blackberry, and therefore display as black boxes.

The eBook will contain a list of English and Punjabi words for reference, such as:

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

One thought I had was to write the list to an HTML table with the Punjabi converted into a GIF or PNG file. Then include this HTML file in the eBook. All of the words currently exist in an access database, but could easily be exported to another form for input to the generation routines.

QUESTION: Using VB, VBA or C#, how hard would it be to write a routine create the images and then output an HTML file containing the English words and images in a table


Solution

  • Using 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