locked
Font size oddity RRS feed

  • Question

  • User1281985059 posted

    I'm proofing out a concept we may use and I'm confused.  I simply want to take three lines of text and write them over the top of an image.  I need to center each line.  So, I was going to use MeasureString to get the length of the strings, use some math to determine the center point and place each line so that it's centered.  Easy enough.  Adding the text is no problem.  Measuring the string is no problem.  So, my problem:  I need to add the text with a font size of 3 to get it the right size which is still fairly large on screen and quite readable.  When I measure the text using font size 3, I get something like 29 pixels, 6 pixels, etc.  It displays much more than 29 or 6 pixels wide.  I believe that the measurement is correct because a font size of 3 is obviously quite small.  If I use a font size of 12 when I add the text, it's huge and doesn't even fit on an image that's 432 x 269 pixels.

    Is there a unit of measure issue I'm missing, resolution issue, could the image have some funky attribute causing that?  Using a size of 3, I would expect it to be very small, but it's what I would expect 12 to look like.

    Any insight would be greatly appreciated.

    Pete

    Friday, April 22, 2005 12:55 PM

All replies

  • User1281985059 posted

    [:$]

    Figured it out.  Duh.  The image resolution was doing it.  I thought the image I was using was 96 dpi, but alas, no.  Sorry.

    Thanks

    Friday, April 22, 2005 1:07 PM
  • User-865881720 posted

    Would it be easier to set the width of your drawing rectangle to the width of the bitmap and use one of the overloaded methods of DrawString (with the format parameter's Alignment property set to StringAlignment.Center) to draw centered text within the rectangle? Here's a section of code that I've used before to superimpose centered text over a randomly selected image:

    ImageFileName = CStr(RandomRow("ID")) & ".jpg"
    ArtistName = FlipName(CStr(RandomRow("Name")))
    ArtistCityState = CStr(RandomRow("CS"))
    ArtworkCaption = CStr(RandomRow("ArtworkCaption"))
    sf = New StringFormat(StringFormatFlags.LineLimit)
    sf.Alignment = StringAlignment.Center
    Img = New Bitmap(Server.MapPath("./images/artwork/" & ImageFileName))
    H = Img.Height
    W = Img.Width
    WScaled = 250
    HScaled = CInt((H * WScaled) / W)
    ImgFormat = Img.RawFormat()
    Canvas = New Bitmap(WScaled, HScaled + 200)
    g = Graphics.FromImage(Canvas)
    g.Clear(System.Drawing.Color.Beige)
    g.TextRenderingHint = TextRenderingHint.AntiAlias
    g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    Dim MyFontCollection As New PrivateFontCollection
    MyFontCollection.AddFontFile(Server.MapPath("/") & dataPath & "FRSCRIPT.TTF")
    Dim Fnt1 As Font = New Font(MyFontCollection.Families(0), 18, FontStyle.Bold)
    Dim Fnt2 As Font = New Font("Arial Black", 10, FontStyle.Regular)
    szf = g.MeasureString(ArtworkCaption, Fnt1, WScaled, sf)
    rectf = New RectangleF(0, HScaled + 6, WScaled, szf.Height)
    g.DrawString(ArtworkCaption, Fnt1, System.Drawing.Brushes.Black, rectf, sf)
    rectf.Offset(0, szf.Height + 6)
    Leading = Fnt2.Height
    rectf.Height = Leading * 2
    g.DrawString(ArtistName & vbCrLf & ArtistCityState, Fnt2, System.Drawing.Brushes.Black, rectf, sf)
    Sunday, April 24, 2005 6:37 AM