Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:54:59+00:00 2026-06-16T22:54:59+00:00

Using Visual Studio 2012 Ultimate C#.NET4.5 Well this one has rotted my brain, I’ve

  • 0

Using Visual Studio 2012 Ultimate C#.NET4.5

Well this one has rotted my brain, I’ve got some code I tweaked a little to embed a font onto my application. So far so good it works, so my client machine no longer needs the font.

Here’s my code so far:

//add font
    [DllImport("gdi32.dll", ExactSpelling = true)]
    private static extern IntPtr AddFontMemResourceEx(byte[] pbFont, int cbFont, IntPtr pdv, out uint pcFonts);
    /****/
    //Dispose Font
    [DllImport("gdi32.dll", ExactSpelling = true)]
    internal static extern bool RemoveFontMemResourceEx(IntPtr fh);
    /****/
    static private IntPtr m_fh = IntPtr.Zero;
    static private PrivateFontCollection m_pfc = null;

public Font GtSpecialFont(float size)
    {
        Font fnt = null;

        if (null == m_pfc)
        {
            Stream stmFnt = Assembly.GetExecutingAssembly().GetManifestResourceStream("NewLabelPrinter.Resources.FREE3OF9.TTF"); // always returns null?

            if (null != stmFnt)
            {
                byte[] rgbyt = new byte[stmFnt.Length];
                stmFnt.Read(rgbyt, 0, rgbyt.Length);

                uint cFonts;
                AddFontMemResourceEx(rgbyt, rgbyt.Length, IntPtr.Zero, out cFonts);

                IntPtr pbyt = Marshal.AllocCoTaskMem(rgbyt.Length);
                if (null != pbyt)
                {
                    Marshal.Copy(rgbyt, 0, pbyt, rgbyt.Length);
                    m_pfc = new PrivateFontCollection();
                    m_pfc.AddMemoryFont(pbyt, rgbyt.Length);
                    Marshal.FreeCoTaskMem(pbyt);

                }
            }
        }
        try
        {
            if (m_pfc.Families.Length > 0)
            {
                fnt = new Font(m_pfc.Families[0], size);
            }
        }
        catch (Exception rdf)
        {
            MessageBox.Show("", rdf.ToString());
        }

        return fnt;
    }

 private void myFont()
    {
        txtBarCal.Font = GtSpecialFont(48.0f);
        txtBarCodeOLD.Font = GtSpecialFont(48.0f);
        txtBarCV.Font = GtSpecialFont(48.0f);
        txtBarBK.Font = GtSpecialFont(48.0f);
        txtNewBar.Font = GtSpecialFont(48.0f);
    }

As you can see this code is pretty nice, I’m loving it works perfectly. Now my form finally can have fonts without my clients needing them.

there is one small problem, how in the world do I use this to set a visual studio report textbox font???? there is no code for the report so I’m at an utter loss!

well I’m hoping someone somewhere knows something, the Only thing I can think of, is something to do with using expressions and/or possibly parameters.

many thanks guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:55:00+00:00Added an answer on June 16, 2026 at 10:55 pm

    You have some odds to make this work. Your code makes the font available both to any code that renders with GDI as well as any code that renders with GDI+. With GDI being the likely one that ReportViewer uses to render the report, it is a pretty old chunk of code. The only real requirement is that you ensure that this code runs before you ever display the report.

    The one thing you however cannot rely on is the Font that’s returned by your code, for obvious reasons. To fix that, you’ll have to refer to the font in your report by the family name. To make that work, you’ll have to actually install the font on your dev machine so you can select it in the report designer. On the user’s machine, that font name should then be good enough to get the font selected from the memory font.

    No guarantees, just decent odds.

    Do make sure you fix a bug in this code, also possibly the reason you’ve got a problem with the report, releasing the memory for the font after calling AddMemoryFont() is not correct. You must keep it allocated as long as the app can use the font. The failure mode is very flaky since it has such good odds of not being noticed, background answer is here. In a nutshell: just don’t call FreeCoTaskMem(). Windows will clean up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Visual Studio 2012 Ultimate version. I've got this error and I don't
Started using Visual Studio 2012 RC since yesterday, We have one WCF solution. Whenever
I'm using Visual Studio 2012 for Windows Phone 7 and this is my first
I am using Visual Studio 2012 Ultimate with the following Boost Signals2 code: at
I am using Visual Studio 2012 to deploy an Outlook Add-In and for some
I've developing a Windows 8 app using .NET 4.5 in visual studio 2012 ultimate.
I've been using Microsoft.Web.Optimization package for some time via the Visual Studio 2012 trial
I am using Windows 8 Pro and Visual Studio 2012 Ultimate (both RTM). I
I'm using Visual Studio 2012 Ultimate and Resharper and whenever I search for (and
I'm writing a Windows 8 app in C# using visual studio 2012 Ultimate. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.