Last post Apr 24, 2005 06:37 AM by imagemaker
Apr 22, 2005 12:55 PM|PDraigh|LINK
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.
Apr 22, 2005 01:07 PM|PDraigh|LINK
Figured it out. Duh. The image resolution was doing it. I thought the image I was using was 96 dpi, but alas, no. Sorry.
Apr 24, 2005 06:37 AM|imagemaker|LINK
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.CenterImg = New Bitmap(Server.MapPath("./images/artwork/" & ImageFileName))H = Img.HeightW = Img.WidthWScaled = 250HScaled = 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.AntiAliasg.SmoothingMode = Drawing2D.SmoothingMode.AntiAliasDim MyFontCollection As New PrivateFontCollectionMyFontCollection.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.Heightrectf.Height = Leading * 2g.DrawString(ArtistName & vbCrLf & ArtistCityState, Fnt2, System.Drawing.Brushes.Black, rectf, sf)