0

How could you - in WinForms - create a black area that (over time) loses/gains transparency and is redrawn constantly so a kind of transition is created? I dont want to create a seperate form, so this would be very helpful and I could rearrange my new items as soon as the black area reaches a transparency of 0.

Left: What i have what at the moment:
Right: What the program looks like on startup
What I would want: A totally black screen

The resulting

toddmo
  • 20,682
  • 14
  • 97
  • 107
Ian H.
  • 3,840
  • 4
  • 30
  • 60
  • Check this out, maybe you will like it IDK: http://stackoverflow.com/questions/12497826/better-algorithm-to-fade-a-winform – toddmo Nov 22 '15 at 20:36
  • Do you need all contents of form to get black smoothly and the change the contents and bring the new contents to view smoothly? Is this your question? – Reza Aghaei Nov 23 '15 at 08:56
  • @RezaAghaei Yes, so the pictureboxes I use are faded out too. – Ian H. Nov 23 '15 at 09:00

1 Answers1

2

Override the OnPaint method of the form, and fill a rectangle the size of the form with the desired transparency. In this example, transparency keeps increasing:

var transparency = 0;
protected override OnPaint(PaintEventArgs e)
{
  base.OnPaint(e);
  // e.Graphics.DrawImage(Image, imageLeft, imageTop, imageWidth, imageHeight);
  e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(transparency, Black)),   this.ClientRectangle);
}

If you want to do this from a Timer event, inside the Tick event Invalidate the form to force the paint.

protected void MyTimer_Tick(object sender, eventArgs e) {
  transparency++;
  MyForm.Invalidate();
}

If you need to draw on top of the PictureBox, that presents a problem. One way to solve it is to get rid of the PictureBox and draw the image using Graphics.DrawImage before you draw the fade. If you want to show the image but have the black go up against the edges of your animal, then make it's background color of the PictureBox transparent and convert your image to use a PNG or GIF with all around the animal transparent in the original image.

For more complex effects, Check out the dot-net-transitions project on Google Code. It supports a variety of linear/non-linear transitions.

toddmo
  • 20,682
  • 14
  • 97
  • 107
  • Could you also call it from a timer_Tick event? Because I want to repeatedly increase the transparency over time so a transmission effect is created. – Ian H. Nov 22 '15 at 20:06
  • Ah, thanks thats almost perfect :) Just one thing: Is it possible that the Rectangle that is draw overlays all of the pictureboxes? – Ian H. Nov 22 '15 at 20:14
  • I edited my question, note the text is inserted with MS Paint. – Ian H. Nov 22 '15 at 20:27
  • Ok I changed it to call the base paint first, then add the rectangle last. You are trying to do a fade out, I see. – toddmo Nov 22 '15 at 20:34
  • Yeah sorry probably would have been a better description. – Ian H. Nov 22 '15 at 20:35
  • Well, this doesnt work because: "No overload for 'OnPaint' matches delegate 'PaintEventHandler'". What did I do wrong? – Ian H. Nov 22 '15 at 20:38
  • You have some code in your "code behind" file that needs to be deleted. It's hooking up the event handler to the event. When you renamed it, it stupidly messed that up. It's in the form designer.cs file. It's not an event anymore; no need for an event handler. – toddmo Nov 22 '15 at 20:41
  • This didnt work out for me, the picturebox still doesnt get overlayed by then rectangle. – Ian H. Nov 23 '15 at 06:41
  • Ok I added some more ideas to my answer. – toddmo Nov 23 '15 at 14:57