• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

Images on the fly font & color problem

Hi,
I'm currently using this code to create text headings on the fly.
The problem I have is I want to use a custom font. I have created the code below to use the font but I get an error.
"BC30519: Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:"

Any Ideas?

Also I want to use a specific RGB or HEX value colour but I am unable to find out how. (It uses a built in color "Brushes.Navy")

Thanks for your help

Pete

--snip--
dim fontName, fontFamily
fontName = "Tarragon.ttf"
dim privateFontCollection as new PrivateFontCollection()
privateFontCollection.AddFontFile(Server.MapPath("./") + fontName)
fontFamily = privateFontCollection.Families(0)

Dim objFont As New Font(fontFamily, 36, FontStyle.Bold) <<<<<<<<<<<<<ERROR ON THIS LINE
--end snip--


MAIN CODE

<%@ Page Language="VB" ResponseEncoding="iso-8859-1" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%
 

   Dim objGraphics As Graphics
   Dim intHeight As Integer
   Dim intWidth As Integer

   Dim strLetter As String = Request.QueryString("letter")

dim fontName, fontFamily
fontName = "Tarragon.ttf"
dim privateFontCollection as new PrivateFontCollection()
privateFontCollection.AddFontFile(Server.MapPath("./") + fontName)
fontFamily = privateFontCollection.Families(0)

'   Dim objFont As New Font(fontFamily, 36, FontStyle.Bold)
   Dim objFont As New Font("Verdana", 36, FontStyle.Bold)




   ' Use a dummy Bitmap to get the functionality
   ' needed to measure
   Dim bmpDummy As New Bitmap(100, 100)
   objGraphics = Graphics.FromImage(bmpDummy)
   Dim objSize As SizeF = objGraphics.MeasureString(strLetter, objFont)
   intHeight = objSize.Height - 5
   intWidth = objSize.Width + 2

   ' You are now ready to create the real Bitmap
   Dim bmpLetter As New Bitmap(intWidth, intHeight)
   objGraphics = Graphics.FromImage(bmpLetter)
   objGraphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit

   ' You are now ready to draw the string into
   ' your box...
   objGraphics.Clear(Color.white)
   Dim objRect As New RectangleF(0, 3, intWidth, intHeight)
   Dim objSF As New StringFormat()
   objSF.Alignment = StringAlignment.Center
   objSF.LineAlignment = StringAlignment.Center
   objGraphics.DrawString(strLetter, objFont, Brushes.Navy, objRect, objSF)

   ' You send the Bitmap to the browser
   Response.ContentType = "image/gif"
   bmpLetter.Save(Response.OutputStream, ImageFormat.Gif)

   ' You clean up
   bmpLetter.Dispose()
   bmpDummy.Dispose()
   objGraphics.Dispose()
   objFont.Dispose()
   objSF.Dispose()
%>
0
pforeman
Asked:
pforeman
  • 5
  • 4
  • 2
1 Solution
 
Bob LearnedCommented:
Try this to get a String type, and not Object:
   Dim fontName, fontFamily As String

Bob
0
 
pforemanAuthor Commented:
Error
BC30311: Value of type 'System.Drawing.FontFamily' cannot be converted to 'String'.
0
 
Bob LearnedCommented:
1) Untyped variables are just plain evil, it leaves someone to make guesses (and I was wrong :( )

2) I missed this line:

    fontFamily = privateFontCollection.Families(0)

3) Did this line work for you?

    Dim objFont As New Font("Verdana", 36, FontStyle.Bold)

Bob
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
pforemanAuthor Commented:
;o)


Yes Dim objFont As New Font("Verdana", 36, FontStyle.Bold) works fine.

I found this code as C# and rewrote it in VB but wrong obviously. ;o)

dim fontName, fontFamily
fontName = "Tarragon.ttf"
dim privateFontCollection as new PrivateFontCollection()
privateFontCollection.AddFontFile(Server.MapPath("./") + fontName)
fontFamily = privateFontCollection.Families(0)

The original code looked like this:

string fontName = "tarragon.ttf";
PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile(Server.MapPath("./") + fontName);
FontFamily fontFamily = privateFontCollection.Families[0];
Font font = new Font(fontFamily, fontSize, style, GraphicsUnit.Pixel);
0
 
smeggheadCommented:
you have to specify that fontFamily is of type 'FontFamily' - otherwise, when you call the constructor for Font, it doesn't know that the first parameter is 'FontFamily', it's just an object.

So, change it to.

dim fontFamily as FontFamily
dim fontName as string

Also, to create a colour from rgb, just call

MyColour=Color.FromArgb(255,255,0) ' This is r=255 g=255 b=0

Hope this helps
Smg.
0
 
pforemanAuthor Commented:
Thank you the font part is working.

RE the colour part. What do I need to dim MyColour as?

I seem to geting an error at the moment.

Exception Details: System.InvalidCastException: Specified cast is not valid.
Line 52:   objGraphics.DrawString(strLetter, objFont, Mycolour, objRect, objSF)

Cheers
Pete
0
 
smeggheadCommented:
dim MyColour as Color

also, for methods that have mutliple overloads.. you should always pass in the variable of the correct type, so that the method knows which overload you are trying to call.

Alternatively, you can cast them at the point of calling.. but it's much tidier to have them defined as the correct type initially.

objGraphics.DrawString((string)strLetter, (Font)objFont, (Color)Mycolour, (Rectangle)objRect, (StringFormat)objSF)
0
 
smeggheadCommented:
oops, sorry, you're using VB.. the cast only applies to c#.. I think you can say Ctype(objFont,Font) rather than (Font)objFont, but I'm not 100%

Smg.
0
 
pforemanAuthor Commented:
When specifying the color it errors with the following error

Compiler Error Message: BC30518: Overload resolution failed because no accessible 'DrawString' can be called with these arguments:
Line 50:   objGraphics.DrawString(strLetter, objFont, Mycolor, objRect, objSF)

If I use objGraphics.DrawString(strLetter, objFont, Brushes.Navy, objRect, objSF) it works fine

Any ideas?
0
 
smeggheadCommented:
the drawstring method requires a 'Brush' not a 'Color'.

i.e.

dim MyBrush as SolidBrush
MyBrush=new SolidBrush(Mycolor)
.. your code
MyBrush.Dispose() ' you should always call dispose after creating a brush/pen/font + many others



0
 
pforemanAuthor Commented:
Funnily enough I just found that by trawling through MSDN.

Cheers
For your help
Pete
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now