Transparent form with image C#

Asked

Viewed 1,275 times

0

Next I’m making a tutorial screen for the program, I did not find a good solution to do.

I created a screen over another with transparency, so far so good, but when I put an image on this screen, it was also transparent. So I created a third screen, it’s totally transparent and with the images, so far so good, but the images were pixelated or checkered, it’s very blurred, to see the pixels around it, I don’t know what this effect is called, but anyway, I can’t make them look pretty.

I have tried with a Picturebox, but keep the black background, I tried also with the code below, this is transparent, but it is as I described pixel:

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...

1 answer

1


Man, I’m not sure I understand the question, but let’s go: If your goal is to have a transparent form with an image above, add the Picturebox normally and, in the class of your Form, 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 same VS adds the code for you within the method InitializeComponent();.

EDIT: You can also replace the Picturebox by placing the desired image as the Backgroundimage of your Form. Although, I have done before using the Picturebox and at the end has the same result visually.

  • It was more or less what I used, but with the black color, but would it be possible to reduce the border of pictureBox a little? The border is kind of flashy for the image I use. @Vinicius

Browser other questions tagged

You are not signed in. Login or sign up in order to post.