Code behind implementing a new System.Drawing.Font

Recently, I have been attempting to generate an image using the Black Rose font in c# but unfortunately, the output does not reflect the desired font. Below is the code snippet I used for creating the image:

    protected void Page_Load(object sender, EventArgs e)
    {
        string MoneyImg = "1000";
        Bitmap bitmap = new Bitmap(MoneyImg.Length * 40, 150);

        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            Font oFont = new System.Drawing.Font("BLACKR", 20);
            PointF point = new PointF(2f, 2f);
            SolidBrush black = new SolidBrush(Color.Black);
            SolidBrush white = new SolidBrush(Color.White);
            graphics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height);
            graphics.DrawString("$" + MoneyImg , oFont, black, point);
        }
    }

Despite my attempts, changing the font file name from

Font oFont = new System.Drawing.Font("BLACKR", 20);

to

Font oFont = new System.Drawing.Font("BLACKR.TTF", 20);

has not yielded any positive results so far. The correct font file directory has been verified with a CSS test which displays fine. Sharing below the CSS code and a snapshot of it.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        @font-face 
        { 
        font-family: Black Rose; src: url('BLACKR.TTF'); 
        } 
        h3 {
        font-family: 'Black Rose'
        }
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Image ID="Image1" runat="server" />
        <br />
        <h3>This is what the Image Font Looks Like</h3>
    </div>
    </form>
</body>
</html>

https://i.sstatic.net/vhLVM.png

Answer №1

The host system may not recognize the font being used in the file, even if it is located within the web application's file structure. This is because Windows needs to be explicitly informed about the presence of a font on the hard drive in order for it to be accessible.

To resolve this issue, you can include the font in a PrivateFontCollection in your code and load it from there. Here is an example of how to do so:

var customFonts = new System.Drawing.Text.PrivateFontCollection();
customFonts.AddFontFile(@"C:\path\to\BLACKR.TTF");
var customFont = new System.Drawing.Font(customFonts.Families[0], 20);

Answer №2

It's possible that this information may not be accurate since I haven't had the opportunity to experiment with it. However, considering that 'BLACKR.TTF' is a unique font, my suggestion would be to include it in a PrivateFontCollection initially.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery not functioning properly when included in a separate JavaScript file

I'm facing an issue with my jquery code not working properly when I place it in a separate js file instead of embedding it directly into the html. Currently, my setup looks like this: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3 ...

Tips for disabling default browser input validation in React

https://i.stack.imgur.com/834LB.png Is there a way to remove the message "Please fill out this field" while still keeping the "required" attribute intact? I have my own validation system in place, so I need to use the "required" attribute to determine whe ...

Refrain from showing content beneath a certain element

Is there a way to hide all content that appears after a specific element, such as a particular class of div? The issue I am facing involves using a 1&1 webpage builder with a restrictive layout-template enforced by my boss. I am trying to remove the foote ...

Personalizing the dimensions of audio.js

I recently integrated the audio.js plugin into my website. I've added the necessary code to the header.php file located in the includes folder. <script src="audio/audiojs/audio.min.js"></script> While the player appears correctly on othe ...

pictures on the blog entries

I'm looking to customize the look of images within a section. I want to add borders to the images, but since they are being added dynamically as content in blog posts, I can't reference them directly. Can you recommend a way for me to achieve thi ...

Numerous CSS class attributes

I have been experimenting with creating my own custom progress bar using CSS. While the HTML5 <progress value="10" max="100"></progress> element could work, I prefer to create it from scratch. In my CSS code, I have defined the .progressbar c ...

Icon that can be clicked before

I'm struggling to figure out how to make the link icon in my code clickable so that users can click anywhere within the card to navigate to another page. I experimented with (" attr(href) ") but it resulted in adding the URL text next to the ...

What is the reason that when a <div> click on a mobile device includes a jQuery ('#item').fadeOut() function, the CSS of the '#item' element maintains its previous hover state

While creating a mock back to top button, I encountered an issue with my jQuery call to fadeOut() behaving differently on mobile devices (this discrepancy can be observed using any desktop browser simulator). You can find the source code here: https://cod ...

Improving JSON string formatting with JsonSerializer for better readability: Adding proper indentation

I am receiving a JSON string from a web service, but it is poorly formatted. Is there a way to clean up the formatting for better display on my screen? I was thinking of using the following code: JsonSerializerOptions() { WriteIndented = true } , but I c ...

Using Several Gridviews on a Single Webpage

My goal is to incorporate 2 Gridviews on a single page. The first Gridview should be visible upon page load, and the second one should become visible upon clicking on a link button inside each row of the first Gridview. Here's the design for the fi ...

When I try to run my asp.net app with mono, I encounter the error System.BadImageFormatException: Invalid image for System.Runtime.InteropServices.RuntimeInformation.dll

Our development team has a web application built on the .NET Framework 4.6 that we are currently supporting. I'm attempting to run the same app on my Linux machine (Ubuntu 20.04) using Mono and JetBrains Rider. While my code compiles without any issue ...

Sorry, the picture is not currently visible

Could someone please help me understand why my image is not displaying? I have stored the address of the image in a column named ImageArticle. My intention is to have this image displayed after all the fields in an MVC project. Below is the code snippet: ...

Fixed Navigation - Employing stickUp.js extension

* { box-sizing: border-box; } body { margin: 0; padding: 0; font-weight: 500; font-family: 'Helvetica Neue', sans-serif; } main {} .bckgrnd { background-position: center center; background-repeat: no-repeat; background-attachment: ...

Tips for achieving a design aesthetic similar to that of the JQuery UI website

On my website, I am using jQuery UI along with the "Smooth Default" CSS theme. However, I have noticed that everything appears larger compared to the jQuery UI website itself. For instance, when looking at the buttons here: http://jqueryui.com/button/ The ...

Explicitly employ undefined constants deliberately

So I am working on a project called WingStyle: https://github.com/IngwiePhoenix/WingStyle. It is still in the development phase, and I'm looking for ways to improve its functionality. One thing I want to address is the use of quotes. For example, whe ...

Update the CSS property according to the selected list item in the slider using Glider JS

Looking to dynamically change the background image in CSS based on the active slide value in Glider.js Here is the CSS: html { background-image: url("https://images.unsplash.com/photo-1496518908709-02b67989c265?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEy ...

Problem encountered when attempting to insert a new division into an existing flexbox container

I'm in the process of designing a basic login page using HTML/CSS. To center the box on the screen, I opted for a flexbox layout. So far, I have successfully positioned the Username/password fields and login button just the way I want them. The only t ...

CSS styling not rendering consistently across various web browsers

Hey everyone! I've encountered an issue with my webpage. Feel free to check out the code on my CodePen here. When I view this page on Safari, everything looks perfect. However, when I switch over to Chrome or Firefox, the layout is all messed up. Ele ...

Issue with the pluses and minuses in the Bootstrap accordion

Can someone explain why the panel-header displays all "-" instead of "+"? When I click on the panel, it changes all "-" to "+". This happens consistently, not just the first time the page loads. My CSS code: .panel-heading a:after { font-family: &ap ...

The challenges of utilizing breakpoints effectively within Tailwind CSS

So here's the issue. I've gone through various other queries related to this problem but haven't found a solution yet. Tailwind seems to be functioning well except for breakpoints. This is what I have in the head <meta name="viewpo ...