1

I have a service that's designed to do image processing. One of the tasks is to overlay a string on an image which is then sent to a program and displayed.

I'm having trouble overlaying the text on a Bitmap image, can anybody help please?

I'm attempting to use:

PrivateFontCollection fonts = new PrivateFontCollection();
public static FontFamily LoadFontFamily(string fileName, out PrivateFontCollection fontCollection)
{
    fontCollection = new PrivateFontCollection();
    fontCollection.AddFontFile(HostingEnvironment.ApplicationPhysicalPath + '/' + fileName);
    return fontCollection.Families[0];
}

FontFamily family = LoadFontFamily("arial.ttf", out fonts);
Font font = new Font(family, 20);

using (Graphics g = Graphics.FromImage(bitmap))
{
    g.DrawString("text", font, new SolidBrush(GetColorFromHexString(foreground)), new PointF(10F, 10F));
}

As per the information on this page.

As the service is unaware of what 'Arial' is - it's obviously failing. I've included the .ttf font as a resource in the service with the build content set to 'copy always'.

Thanks!

Paul Keister
  • 12,851
  • 5
  • 46
  • 75
user1567095
  • 480
  • 1
  • 5
  • 13

2 Answers2

1

Arial is one the very oldest Windows fonts, and it has long been available as part of the default installation of all Windows operating system versions, including Windows server. So there's no reason that:

    Font font = new Font("Arial", 20);

shouldn't work.

Paul Keister
  • 12,851
  • 5
  • 46
  • 75
0

Wcf has nothing to do with fonts, so I'd suggest to remove this tag.

I you have no errors, but your image does not contains your string - your logic is invalid.

I'd suggest following steps:

  1. Ensure this code works on regular winforms project
  2. Try to re-factor this logic into separate dll (class library, not win forms). If you has tools like Resharper - it'll add all required references.
    at this point - your code should work in all cases, so
  3. Move it back to be exposed via your wcf service.
evgenyl
  • 7,837
  • 2
  • 27
  • 32