0

If I have made a form with multiple labels (some dynamic labels) how can I add Anti aliasing to the labels and remove/ make the form background transparent.

Also how do I make the form background invisible so its just the labels left?

  • (I experimented with some stuff that are commented out in the code, that's somewhat works)*

  • Using visual studio

Currently how it is:

enter image description here

How I want it to be:

enter image description here

C#

using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        Thread thread1;

        public Form1()
        {
            InitializeComponent();

            /*// Problem code:
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingFonts_Paint); 
            //end*/

            thread1 = new Thread(new ThreadStart(Thread_test));
            thread1.IsBackground = true;
            thread1.Start();
        }

        public void Thread_test()
        {
            int i = 0;
            while (true)
            {
                i++;
                label2.Text = Convert.ToString(i);
                Thread.Sleep(1000);
            }
        }

        /*//Problem code:
        private void SmoothingFonts_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
        {                                                                                       
            Font TextFont = new Font("Segoe UI", 48, FontStyle.Bold);
            Color myColor = Color.FromArgb(30, 35, 42);
            SolidBrush myBrush = new SolidBrush(myColor);
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            e.Graphics.DrawString("TestTEXT 2", TextFont, myBrush, 20, 20);
        }
        //end*/
    }
}

Download Visual Studio Project Here

Patrik Fröhler
  • 1,221
  • 1
  • 11
  • 38

2 Answers2

1

Here is the final solution I ended up with:

Instead of windows form I just did WPF, then AA and transparency works out of the box (super easy).

enter image description here

WPF CODE:

using System;
using System.Windows;
using System.Windows.Threading;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        DispatcherTimer timer = new DispatcherTimer();


        public MainWindow()
        {
            InitializeComponent();
            timer.Tick += new EventHandler(timer_tick);
            timer.Interval = new TimeSpan(0, 0, 1);

        }

        int dynCount = 0;

        private void timer_tick(object sender, EventArgs e)
        {
            dynCount++;
            dynNum.Content = dynCount.ToString();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            timer.Start();

        }


    }
}

Download "WPF" visual studio project here

Here are a few other tests in windows form:

Label Text with AA but NOT with transparent background:

enter image description here

CODE:

using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        Label[] _Labels = new Label[2];
        String[] _LabelsText = new string[2];
        Graphics g;
        Bitmap btm;
        Color myColor;
        SolidBrush myBrush;


        private void Form1_Load(object sender, EventArgs e)
        {
            _Labels[0] = label1;
            _Labels[1] = label2;

            _LabelsText[0] = "CPU";
            _LabelsText[1] = "0";

            label1.Text = "";
            label2.Text = "";
            draw();
            this.BackgroundImage = btm;

        }

        public void draw()
        {
            btm = new Bitmap(this.Width, this.Height);
            g = Graphics.FromImage(btm);
            for (int i = 0; i < _Labels.Length; i++)
            {
                myColor = _Labels[i].ForeColor;
                myBrush = new SolidBrush(myColor);

                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                g.DrawString(_LabelsText[i], _Labels[i].Font, myBrush, _Labels[i].Location);
            }
        }


        int dynNumCount = 0;
        private void dynamicNumber_Tick(object sender, EventArgs e)
        {
            dynNumCount++;
            _LabelsText[1] = Convert.ToString(dynNumCount);
            draw();
            this.BackgroundImage = btm;

        }
    }
}

Download "AA only" visual studio project here

Label Text with AA and transparent background using direct draw to desktop:

(I had problem clearing before redrawing so you would need to solve that).

enter image description here

CODE:

    using System;
    using System.Runtime.InteropServices;
    using System.Drawing;
    using System.Drawing.Text;
    using System.Windows.Forms;


    namespace WindowsFormsApplication3
    {

        public partial class Form1 : Form
        {

            [DllImport("User32.dll")]
            static extern IntPtr GetDC(IntPtr hwnd);

            public Form1()
            {
                InitializeComponent();
                this.Opacity = 0;
            }

            Label[] _Labels = new Label[2];
            String[] _LabelsText = new string[2];
            Graphics g;
            Color myColor;
            SolidBrush myBrush;


            private void Form1_Load(object sender, EventArgs e)
            {

                _Labels[0] = label1;
                _Labels[1] = label2;

                _LabelsText[0] = "CPU";
                _LabelsText[1] = "0";

                label1.Text = "";
                label2.Text = "";
                draw();
            }


            private void draw()
            {
                using (Graphics g = Graphics.FromHdc(GetDC(IntPtr.Zero)))
                {
                    g.TextRenderingHint = TextRenderingHint.AntiAlias;
                    Font theFont = new Font(FontFamily.GenericSansSerif, 100.0F, FontStyle.Bold);


                    for (int i = 0; i < _Labels.Length; i++)
                    {
                        myColor = _Labels[i].ForeColor;
                        myBrush = new SolidBrush(myColor);
                        g.TextRenderingHint = TextRenderingHint.AntiAlias;
                        g.DrawString(_LabelsText[i], _Labels[i].Font, myBrush, _Labels[i].Location);
                    }

                }
            }


            int dynNumCount = 0;
            private void dynamicNumber_Tick(object sender, EventArgs e)
            {
                dynNumCount++;
                _LabelsText[1] = Convert.ToString(dynNumCount);
                draw();
            }
        }
    }

Download "Draw to desktop" visual studio project here

Patrik Fröhler
  • 1,221
  • 1
  • 11
  • 38
0

Did You try this?

    this.BackColor = System.Drawing.Color.Black;
    this.TransparencyKey= System.Drawing.Color.Black;


    label1.BackColor = System.Drawing.Color.Transparent;
    label1.ForeColor = Color.White;

first two line make form transparent

and the second two line make the label transparent, so it's like glass and there is nothing in back ground of your label

hope it help you

  • Yeah, I've tried that but doesn't work very good, it don't fully remove the background and leaves a color fringe around the the text edge :/ – Patrik Fröhler May 28 '16 at 07:53
  • ok Your main problem is Anti aliasing (use layered window) so here is the best solution I think : http://stackoverflow.com/questions/1596961/anti-aliased-text-in-a-transparent-net-form – Arman.Salehi May 28 '16 at 08:12
  • Patrik here is a TransparencyKey that looks ok , I test it on your sample project and result was good : this.BackColor = Color.FromArgb(255, 7, 11, 5); this.TransparencyKey = Color.FromArgb(255, 7, 11, 5); – Arman.Salehi May 28 '16 at 08:33
  • Orginal Answer is here : http://stackoverflow.com/questions/24398097/border-on-label-when-using-transparency-key – Arman.Salehi May 28 '16 at 08:34