0

I currently set the background color to my form blue. I also set the transparency key equal to the back color of the form.

As you can see, it is doing something, but it didn't get rid of all of the blue. How can I fix this problem so all the blue will go away? enter image description here

Iosnowore
  • 211
  • 1
  • 6
  • 14
  • Show the code! Try Fuchsia! – TaW Oct 03 '16 at 02:37
  • Looks like your image has semi-transparent pixels at the spots where the blue shines through. Make sure it doesn't! All pixels need to be opaque or fully tranparent!! - See [here for a function to help and a link](http://stackoverflow.com/questions/33189112/remove-the-black-background-color-of-a-bitmap/33191068?s=22|0.1194#33191068) to a fast lockbits method. But yur favourite image editor should also be able to do it.. – TaW Oct 03 '16 at 08:26

2 Answers2

1

The rule is: Pick a rare TranparencyKey Color like Color.Fuchsia and then set form.BackColor = form.TransparencyKey. This works just fine, as long as your Image doesn't contain pixels that are a) semi-transparent or b) the TransparencyKey color

This will create and open a simple form with a BackgroundImage and a Paint event that draws a 2nd image onto the form.

enter image description here

As you can see transparency works fine. As you can also see, the background image (of the chart) does have a smal fringe of anti-aliased pixels that let the TransparencyKey/BackColor pixels shine through:

private void button35_Click_1(object sender, EventArgs e)
{
    Form form = new Form();
    //form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    //form.MaximizeBox = false;
    //form.MinimizeBox = false;
    //form.ControlBox = false;
    form.BackColor = Color.Fuchsia;
    form.TransparencyKey = form.BackColor;
    form.BackgroundImage = Image.FromFile("D:\\pie.png");
    form.Paint += form_Paint;
    //form.MouseDown += form_MouseDown;
    form.Show();
}

void form_Paint(object sender, PaintEventArgs e)
{
    using (Image img = Image.FromFile("D:\\proj.png"))
    e.Graphics.DrawImage(img, Point.Empty);
}

//void form_MouseDown(object sender, MouseEventArgs e)
//{
//    if (e.Button == MouseButtons.Left)
//    {
//        ReleaseCapture();
//        SendMessage( ((Form)sender).Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
//    }
//}

//public const int WM_NCLBUTTONDOWN = 0xA1;
//public const int HT_CAPTION = 0x2;
//[DllImportAttribute("user32.dll")]
//public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
//[DllImportAttribute("user32.dll")]
//public static extern bool ReleaseCapture();

The commented parts will create a borderless form, which can be moved.

TaW
  • 53,122
  • 8
  • 69
  • 111
0

EDIT: You'll need to either disable anti-aliasing and deal with a choppy splash screen, or create the splash screen in WPF. If your game is made in Winforms then you can still start it up via a WPF application.

If I'm correct, this is the inherent problem with using TransparencyKey instead of GDI+. Try doing the same process with TransparencyKey, but draw on the form's canvas with GDI+ instead of plopping down an image.

You can draw the image in your form's paint event. You can call the paint event whenever you want by calling this.Invalidate();

Just as a bit of guidance, your paint event will need to use the Graphics class. You can obtain the Graphics class of the form to paint with using Graphics g = this.CreateGraphics();

You can then do stuff like g.Draw...();. Check documentation for more help or comment on my answer and I can point you in the right direction.

Dan
  • 1,163
  • 3
  • 14
  • 28
  • Okay, I will give it a try! So I how would I go about doing this with a background image? – Iosnowore Oct 03 '16 at 01:36
  • Unless you're willing to use WPF, you don't. You'll always have the border if you use Winforms and a BackgroundImage. The only way around it is by drawing with GDI+. The concepts are essentially the same, just you're getting a little more "under-the-hood". – Dan Oct 03 '16 at 01:55
  • This is all wrong. All he needs is to make the pixels of the image either opaque or fully transparent. – TaW Oct 03 '16 at 08:27
  • @TaW The color isn't coming from an image, the color is coming from the background color itself, which I'm trying to make transparent. – Iosnowore Oct 03 '16 at 22:19
  • Since you still didn't show the code I don't know if it is so. The rule is: Pick a rare TranparencyColor like Color.Fuchsia and then set form.BackColor = form.TransparencyColor. This works just fine, as long as your Image doesn't contain pixels that are a) semi-transparent or b) the TranparencyColor – TaW Oct 03 '16 at 23:18
  • @TaW http://i.imgur.com/T86ht1e.png That is with TransparencyKey. It's a common "bug" with Winforms. I attempted to resolve it but unfortunately drawing any images like this with GDI+ only minimizes the problems. Iosnowore: you will need to use WPF to achieve what you want. If your game is written in Winforms, you can still open a Winforms form through your WPF splash screen. – Dan Oct 05 '16 at 04:48
  • If you avoid the issues I mentioned TransparencyKey works fine, both with BackgroundImages and with Images you draw in a Paint event. (Obviously they need to have transparent parts!). – TaW Oct 05 '16 at 06:56
  • @TaW do you have a screenshot? I'm curious to see your results. The image I posted does not have any semi-transparent pixels and was not the colour of the TransparencyKey. – Dan Oct 05 '16 at 15:16
  • I have posted an animation. What format was your image? – TaW Oct 05 '16 at 15:18
  • @TaW It was a .png. Just saw your animation, as per my comment on the answer you still have a slim coloured border the same colour as the TransparencyKey. – Dan Oct 05 '16 at 16:14
  • Indeed. If you read my post you will have notice that I said so. Those are semi-transparent anti-aliasing pixels, of of the two things to avoid. – TaW Oct 05 '16 at 16:31
  • @TaW Ah okay yeah that makes sense. If OP doesn't want this I suppose they'll have to use WPF or disable anti-aliasing then. Would you agree? – Dan Oct 05 '16 at 16:40
  • Yes, that is right. no semi-tranparent pixels, unless the whole form is made semi-transparent (by reducing the Opaque property) – TaW Oct 05 '16 at 17:05