473,408 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Fade from one Panel to the other

I am building an application that will fade one panel to another
panel. Both panels will have picture boxes in them (thumbnails).

This is what I have tried:

private void fade(System.Windows.Forms.Panel fP){
for (int i = 255; i >= 0;i--){
System.Threading.Thread.Sleep(10);
fP.BackColor = System.Drawing.Color.FromArgb(i, 255,255,255);
System.Windows.Forms.Application.DoEvents();
}
}

There is another Panel under the one being passed in to the function.

It has two problems. One it flickers like crazy, and two (now this I
don't get at all) it does not become transparent at all. It just fades
the form color. (At the time of testing the top panel had no images in
it, but the bottom one did)

Am I doing something wrong? (Well of course you are idiot) But what?

I don't know much about GDI, but is it possible to take a image of my
control that is fading out and replace the control with that image and
fade the image out? Would that be any better. (Thinking out loud).

If you could, please point me in the right direction. If you had
sample code to do this I would erect a large alter in your name and
sacrifice many chipmunks in for you. (OK, for those of you with no
sense of humor, I'm joking).
keywords: dotnet c# fade panel control
Jul 21 '05 #1
1 7235
Doing this using a panels is a bad isea because Panel paints it's background
and that will cause terrible flicker.

It's so simple to create a control that holds a couple of images and fades
between them.

After my signature here is code that creates a simple fade-panel control.
The image draw modes could be made a wee bit more sophisticated but you'll
get the idea. There is also a test form that drives it.

Enjoy..

Oh. I have no sense of humour. You can sacrifice the chipmunks immediately.

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

--------------------- fade panel ------------------------
using System;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Drawing.Imaging;

using System.Windows.Forms;

namespace BobsControls

{

public class FadePanel : Control

{

Image _imageA;

public Image ImageA

{

get{return _imageA;}

set{_imageA=value;}

}

Image _imageB;

public Image ImageB

{

get{return _imageB;}

set{_imageB=value;}

}

int _fade=0;

float _fadeTime;

public float FadeTime

{

get{return _fadeTime;}

set{_fadeTime=value;}

}

Timer t=new Timer();

public FadePanel()

{

SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
ControlStyles.DoubleBuffer, true);

t.Tick+=new EventHandler(t_Tick);

}

protected override void OnPaint(PaintEventArgs e)

{

if(_imageA==null)

return;

e.Graphics.DrawImage(_imageA,this.ClientRectangle, 0,0,_imageA.Width,_imageA.
Height,GraphicsUnit.Pixel);

if(_imageB==null)

return;

ImageAttributes ia=new ImageAttributes();

ColorMatrix cm=new ColorMatrix();

cm.Matrix33=1.0f/255*_fade;

ia.SetColorMatrix(cm);

e.Graphics.DrawImage(_imageB,this.ClientRectangle, 0,0,_imageA.Width,_imageA.
Height,GraphicsUnit.Pixel,ia);

base.OnPaint (e);

}

public void Fade()

{

_fade=1;

this.t.Interval=(int)(1000f*_fadeTime/32);

this.t.Enabled=true;

}

private void t_Tick(object sender, EventArgs e)

{

_fade+=8;

if(_fade>=255)

{

_fade=255;

t.Enabled=false;

}

Invalidate();

}

}

}

-------------------- test form ------------------

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace testfadepanel

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private BobsControls.FadePanel fadePanel1;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.Button button3;

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.Label label1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.fadePanel1 = new BobsControls.FadePanel();

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();

this.button3 = new System.Windows.Forms.Button();

this.textBox1 = new System.Windows.Forms.TextBox();

this.label1 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// fadePanel1

//

this.fadePanel1.FadeTime = 0F;

this.fadePanel1.ImageA = null;

this.fadePanel1.ImageB = null;

this.fadePanel1.Location = new System.Drawing.Point(8, 32);

this.fadePanel1.Name = "fadePanel1";

this.fadePanel1.Size = new System.Drawing.Size(216, 192);

this.fadePanel1.TabIndex = 0;

this.fadePanel1.Text = "fadePanel1";

//

// button1

//

this.button1.Location = new System.Drawing.Point(264, 32);

this.button1.Name = "button1";

this.button1.TabIndex = 1;

this.button1.Text = "Image1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(264, 64);

this.button2.Name = "button2";

this.button2.TabIndex = 2;

this.button2.Text = "Image2";

this.button2.Click += new System.EventHandler(this.button2_Click);

//

// button3

//

this.button3.Location = new System.Drawing.Point(264, 192);

this.button3.Name = "button3";

this.button3.TabIndex = 3;

this.button3.Text = "Fade";

this.button3.Click += new System.EventHandler(this.button3_Click);

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(288, 136);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(88, 20);

this.textBox1.TabIndex = 4;

this.textBox1.Text = "2";

//

// label1

//

this.label1.Location = new System.Drawing.Point(224, 136);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(56, 23);

this.label1.TabIndex = 5;

this.label1.Text = "Time";

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(384, 266);

this.Controls.Add(this.label1);

this.Controls.Add(this.textBox1);

this.Controls.Add(this.button3);

this.Controls.Add(this.button2);

this.Controls.Add(this.button1);

this.Controls.Add(this.fadePanel1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{

OpenFileDialog dlg=new OpenFileDialog();

dlg.Filter="Image files|*.bmp;*.jpg;*.gif";

if(dlg.ShowDialog()==DialogResult.OK)

{

this.fadePanel1.ImageA=Image.FromFile(dlg.FileName );

this.fadePanel1.Invalidate();

}

}

private void button2_Click(object sender, System.EventArgs e)

{

OpenFileDialog dlg=new OpenFileDialog();

dlg.Filter="Image files|*.bmp;*.jpg;*.gif";

if(dlg.ShowDialog()==DialogResult.OK)

{

this.fadePanel1.ImageB=Image.FromFile(dlg.FileName );

this.fadePanel1.Invalidate();

}

}

private void button3_Click(object sender, System.EventArgs e)

{

try

{

this.fadePanel1.FadeTime=float.Parse(this.textBox1 .Text);

this.fadePanel1.Fade();

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

}

----------------------------

"Robert Skidmore" <ro*********@msn.com> wrote in message
news:d8**************************@posting.google.c om...
I am building an application that will fade one panel to another
panel. Both panels will have picture boxes in them (thumbnails).

This is what I have tried:

private void fade(System.Windows.Forms.Panel fP){
for (int i = 255; i >= 0;i--){
System.Threading.Thread.Sleep(10);
fP.BackColor = System.Drawing.Color.FromArgb(i, 255,255,255);
System.Windows.Forms.Application.DoEvents();
}
}

There is another Panel under the one being passed in to the function.

It has two problems. One it flickers like crazy, and two (now this I
don't get at all) it does not become transparent at all. It just fades
the form color. (At the time of testing the top panel had no images in
it, but the bottom one did)

Am I doing something wrong? (Well of course you are idiot) But what?

I don't know much about GDI, but is it possible to take a image of my
control that is fading out and replace the control with that image and
fade the image out? Would that be any better. (Thinking out loud).

If you could, please point me in the right direction. If you had
sample code to do this I would erect a large alter in your name and
sacrifice many chipmunks in for you. (OK, for those of you with no
sense of humor, I'm joking).
keywords: dotnet c# fade panel control

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Acer | last post by:
Hi, Can somebody help me to create a php function which show an image, fade-out to another images ? thx
1
by: Jonathan | last post by:
Hi Would anyone be able to tell me of an easy way to get fast fade in fade out in VB6? I need a method that can fade a whole screen very smoothly and it would be nice if it is compatible with...
1
by: jase_dukerider | last post by:
Hi I have an assignment to hand in shortly for which I am after some guidance. The task is to read a WAV file, request a fade in /out time for the track from the user and the do the fade by...
1
by: Robert Skidmore | last post by:
I am building an application that will fade one panel to another panel. Both panels will have picture boxes in them (thumbnails). This is what I have tried: private void...
4
by: Chris Lieb | last post by:
Hi, I am writing a class in JavaScript to cause a repeating fade effect. The fade class takes care of the fading and the rgb class takes care of manipulating rgb values: function rgb(red,...
2
by: Emil | last post by:
Hi, if you open the link below and click on "Show Me" and then "Toggle Transition" Button you will see a wonderfull fade in / fade out transition of 2 pictures. ...
1
by: Luciano A. Ferrer | last post by:
Hello! Im trying to do a few fade effects here http://relojurbano.com.ar/scalda/baseporque.php using fadomatic ( http://chimpen.com/fadomatic/ ) I dont know if fadomatic is a good solution...
6
by: Jake Barnes | last post by:
Please go look at this page using FireFox: http://www.ralphkrubner.com/Commercial/ Click the "Next" button a few times. The images fade out and then fade in. It's a nice effect. Now do try...
18
by: hopper | last post by:
I am having a memory block today and for the life of me cant seem to get it right. Can someone help me with code to fade text in 8 seconds after the page has loaded. This is what i have but i...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.