Transparent Form with C #

0

Next I'm doing a tutorial tutorial for the program, I did not find a good solution to do.

I created one screen over another with transparency, so far so good, but when I placed an image on this screen, it also became transparent. So I created a third screen, it is totally transparent and with the images, so far so good, but the images were pixelated or squared, it is very blurry to see the pixels around it, I do not know what the effect is called, but anyway, I can not make them pretty so to speak.

I have tried with a PictureBox, but it has the black background, I also tried with the code below, it stays transparent, but it stays as I described pixeadas:

protected override void OnPaint(PaintEventArgs e)
{
    RectangleF rect = new RectangleF((this.Size.Width * 0.26f) / 2, (this.Size.Height * 0.26f) / 2, this.Size.Width * 0.84f, this.Size.Height * 0.84f);
    e.Graphics.DrawImage(this.img, rect);
}

If anyone has any idea how it can be done, or if it can be done with less Form's ...

    
asked by anonymous 11.01.2017 / 12:12

1 answer

1

Dude, I do not know if I understood the question very well, but come on: If your goal is to have a transparent form with an image on top, add the PictureBox normally and, in your Form class, inside the constructor, you paste the following code:

this.BackColor = Color.Magenta;
this.TransparencyKey = Color.Magenta;
this.FormBorderStyle = FormBorderStyle.None;

Your builder would look something like this:

public MyForm() { // construtor do seu Form
    InitializeComponent(); // inicialização dos controls presentes no Form e etc
    this.BackColor = Color.Magenta; // ou qualquer outra cor muito incomum (de preferência uma que não exista na imagem)
    this.TransparencyKey = Color.Magenta; // igual à cor de fundo do Form
    this.FormBorderStyle = FormBorderStyle.None; // se quiser remover as bordas do Form
}

Alternatively, if you are using Visual Studio you can access the properties in your Form Design and there you will find the three options: BackColor, TransparencyKey and FormBorderStyle. Then just change them to the values of the above code and the VS even adds the code to you inside the InitializeComponent(); method.

EDIT: You can also replace the PictureBox by placing the desired Image as BackgroundImage of your Form. Although, I've done it before using the PictureBox and in the end it has the same result visually.

    
15.01.2017 / 08:02