如何淡入和淡出的面板(化背景图像)(衰落过渡)的形象? [英] How to fade in and fade out (Fading transition ) image on panel(backgroud image)?

查看:126
本文介绍了如何淡入和淡出的面板(化背景图像)(衰落过渡)的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要送我的方法不同的图像,我想插入到这个变化一定的效果。



我怎么能淡入和淡出的图像?

 私人无效ShowImage(图片形象,ImageLayout imageLayout,诠释numberOfSeconds)
{

{
如果(this.image_timer!= NULL)
this.KillImageTimer();

this.customer_form.DisplayImage(图像,imageLayout);

this.image_timer =新的Timer();
this.image_timer.Tick + =(对象S,EventArgs的一个)=> NEXTIMAGE();
this.image_timer.Interval = numberOfSeconds * 1000;
this.image_timer.Start();
}

{
//什么都不做
}


公共无效DisplayImage(图片形象,ImageLayout imageLayout)
{
panel1.BackgroundImage =图像;
panel1.BackgroundImageLayout = imageLayout;
}


解决方案

有没有内置褪色的过渡在的WinForms



所以,你需要自己写一个。



我能想到的最简单的使用了第二个面板,即在第一个层次,实际上需要在第一个面板或者透明效果将无法正常工作。



下面是安装程序,使用两个面板

 公共Form1中()
{
的InitializeComponent();

pan_image.BackgroundImage = someImage;

pan_layer.Parent = pan_image;
pan_layer.BackColor = pan_image.BackColor;
pan_layer.Size = pan_image.Size;
pan_layer.Location = Point.Empty;
}

有关衰落的动画我用的是定时。这是一个简单的代码示例:

 定时器定时器=新的Timer(); 
INT计数器= 0;
INT DIR = 1; //方向1 =淡入..
INT secondsToWait = 5;
INT速度1 = 25; //剔速度毫秒
INT速度2 = 4; //阿尔法(0-255)变速

无效timer1_Tick(对象发件人,EventArgs五)
{
//我们只是等待,现在我们淡出:
如果(DIR == 0)
{
timer1.Stop();
DIR = -speed2;
计数器= 254;
timer1.Interval =速度2;
timer1.Start();
}

//下一个alpha值:
INT阿尔法= Math.Min(Math.Max​​(0,计数器+ = DIR),255);

button1.Text = DIR> 0? 淡入:淡出;

//完全消退时间:设立了漫长的等待:
如果(计数器> = 255)
{
timer1.Stop();
button1.Text =等待;
timer1.Interval = secondsToWait * 1000;
DIR = 0;
timer1.Start();


}
//完全消退出:尝试加载一个新的形象,并设置方向淡入或停止
,否则(计数器< = 0)
{
如果(changeImage(!))
{
timer1.Stop();
button1.Text =完成;
}
DIR =速度2;
}

//创建新的,半透明的颜色:
的色列= Color.FromArgb(255 - 阿尔法,pan_image.BackColor);
//显示层:
pan_layer.BackColor =关口;
pan_layer.Refresh();

}



我开始在一个按钮,关于这一点我也显示当前状态:

 私人无效的button1_Click(对象发件人,EventArgs五)
{

DIR =速度2;
timer1.Tick + = timer1_Tick;
timer1.Interval =速度1;
timer1.Start();
}



正如你可以看到我用两个速度可以设置:一个用于控制在定时器的速度和一个控制步骤,由每个透明度变化勾选



效果是通过简单地改变颜色 BackgroundColor中的创建图像面板来完全透明和背部,等待为指定的秒数之间。



和年底效果我调用一个函数来改变图像。如果这个函数返回定时停止为好..



我敢肯定,这可以在一个更清洁,更优雅的方式来编写的,但因为它是它似乎工作..


I'm sending to my method different images and I want insert some effect to this change.

How can I fade in and fade out images?

 private void ShowImage(Image image, ImageLayout imageLayout, int numberOfSeconds)
    {
        try
        {
            if (this.image_timer != null)
                this.KillImageTimer();

            this.customer_form.DisplayImage(image, imageLayout);

            this.image_timer = new Timer();
            this.image_timer.Tick += (object s, EventArgs a) => NextImage();
            this.image_timer.Interval = numberOfSeconds* 1000;
            this.image_timer.Start();
        }
        catch
        {
            //Do nothing
        }


public void DisplayImage(Image image, ImageLayout imageLayout)
    {
        panel1.BackgroundImage = image;
        panel1.BackgroundImageLayout = imageLayout;
    }

解决方案

There are no built-in fading transitions in Winforms.

So you will need to write one yourself.

The simplest one I can think of uses a second Panel, that is layered upon the first one and in fact needs to be inside the first Panel or else the transparency effect won't work..

Here is the setup, using two Panels:

public Form1()
{
    InitializeComponent();

    pan_image.BackgroundImage = someImage;

    pan_layer.Parent = pan_image;
    pan_layer.BackColor = pan_image.BackColor;
    pan_layer.Size = pan_image.Size;
    pan_layer.Location = Point.Empty;
}

For the fading animation I use a Timer. This is a quick code example:

Timer timer1 = new Timer();
int counter = 0;
int dir = 1;               // direction 1 = fade-in..
int secondsToWait = 5;
int speed1 = 25;           // tick speed ms
int speed2 = 4;            // alpha (0-255) change speed

void timer1_Tick(object sender, EventArgs e)
{
    // we have just waited and now we fade-out:
    if (dir == 0)
    {
        timer1.Stop(); 
        dir = -speed2;
        counter = 254;
        timer1.Interval = speed2;
        timer1.Start();
    }

    // the next alpha value:
    int alpha =  Math.Min(Math.Max(0, counter+= dir), 255);

    button1.Text = dir > 0 ? "Fade In" : "Fade Out";

    // fully faded-in: set up the long wait:
    if (counter >= 255)
    { 
        timer1.Stop(); 
        button1.Text = "Wait";
        timer1.Interval = secondsToWait * 1000;
        dir = 0;
        timer1.Start();


    }
    // fully faded-out: try to load a new image and set direction to fade-in or stop
    else if (counter <= 0)
    {
        if ( !changeImage() ) 
           {
             timer1.Stop(); 
             button1.Text = "Done";
           }
        dir = speed2;
    }

    // create the new, semi-transparent color:
    Color col = Color.FromArgb(255 - alpha, pan_image.BackColor);
    // display the layer:
    pan_layer.BackColor = col;
    pan_layer.Refresh();

}

I start it in a Button, on which I also show the current state:

private void button1_Click(object sender, EventArgs e)
{

    dir = speed2;
    timer1.Tick += timer1_Tick;
    timer1.Interval = speed1;
    timer1.Start();
}

As you can see I use two speeds you can set: One to control the speed of the Timer and one to control the steps by which the transparency changes on each Tick.

The effect is created by simply changing the Color from the BackgroundColor of the image Panel to fully transparent and back, waiting in between for a specified number of seconds.

And the end of the effect I call a function to change the images. If this function returns false the Timer is stopped for good..

I'm pretty sure this could be written in a cleaner and more elegant way, but as it is it seems to work..

这篇关于如何淡入和淡出的面板(化背景图像)(衰落过渡)的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆