0

I want to make a PictureBox that containts a round picture and a string. The shape of the Box should not be rectangular, but adapted to the String and the round Picture. I need this for achieving "perfect" transparency, because apparently WindowsForms, when set to transparent, just lets through the picture of the parent box.

Here is the code that I made:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

namespace myProject
{
    class ShapedPictureBoxes : PictureBox
    {
        public ShapedPictureBoxes()
        {
            this.Paint += this.shapedPaint;
        }

    void shapedPaint(object sender, PaintEventArgs e)
    {
        System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
        Rectangle rectangle = this.ClientRectangle;
        graphicsPath.AddEllipse(rectangle);
        graphicsPath.AddString(text, new FontFamily("Arial"), (int)System.Drawing.FontStyle.Bold, 14f, new Point(0,0), new StringFormat());

        e.Graphics.DrawString(text, new Font("Arial", 14f), Brushes.Red, new Point(0, 0));

        this.Region = new Region(graphicsPath);
    }

    public string text = "HERE COMES THE SUN";
}
}

When using this class and assigning a picture to it, this leads to strange behavior:

  1. The text is not red, but white, there are some red particles in it, but mainly it is white. I assume that this comes from not identical Fonts. So how can I use the same font for both, the drawing and adding?
  2. The text is white exept for there, where it lays above the image: there, it is transparent and you can see through the form.

How comes this and how can I fix it?

enter image description here on the left side you see how it currently looks like, on the right side you see the two different pictureboxes next to each other.. I want it to look like on the left side, but with a readable RED text..

Salocin
  • 393
  • 6
  • 17
  • Can you post an example with your desired rendered output? It's hard to picture exactly what you're asking for. – Anthony Dec 10 '14 at 16:00
  • I added a picture, hope it helps.. – Salocin Dec 11 '14 at 08:10
  • I don't think this is possible in winforms. change your target or your plans! – TaW Dec 11 '14 at 13:04
  • @Salocin You're attempting to solve your problem the wrong way. Instead of trying to make a `PictureBox` the shape of a string you should try to find a way to paint the three elements (the bar, the guage, and the text) together on a single custom control. – Anthony Dec 11 '14 at 14:04
  • The problem is, that later I will use WMP-Controls in the background(kind of replacing the bar). In these, I can't draw the two pictures together. – Salocin Dec 12 '14 at 08:14

0 Answers0