如何更改此代码以淡入图像? [英] How do I change this code to fade in an image?

查看:91
本文介绍了如何更改此代码以淡入图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我已经尝试过使用这个批量代码另一批代码,用于淡入淡出图像。两者都设法成功淡出图像,但我无法让第二张图像淡入。我相信这与两个批次在图像上绘制矩形相比,可以是较小和较小的不透明度,也可以是更大和更大的不透明度。



我已经尝试了嵌套if语句中的这两个代码,这些语句决定了图像是否淡出以及何时将新图像放入其中以开始淡化它。

如果有人能指出我正确的方向来改变图像的不透明度,那将非常感激。



这里这是我的参考代码:



  private   void  timer1_Tick( object  sender,EventArgs e)
{

// 运行检查以确定第一个图像是否应淡出。
if (imageChange == true
{
if (breakCheck2 == false
{

// 淡出图像。
if (alpha ++ < 40
{
Image image = pictureBox1.Image;
使用(图形g = Graphics.FromImage(图像))
{
Pen pen = new 笔(Color.FromArgb(alpha, 255 255 255 ),image.Width);
g.DrawRectangle(pen,-1,-1,image.Width,image.Height);
g.Save();

}
pictureBox1.Image = image;
if (alpha == 40
{
breakCheck = true ;
}
}
}
}

// 运行检查第二个图像是否应该淡出。
if (breakCheck == true
{
if (alpha-- > 0
{
// 适用时评分第二张图片。
if (imageChange == true
{
pictureBox1.Image = Properties.Resources.transitionTest;
pictureBox1.Invalidate();
imageChange = false ;
}

// 应淡入第二张图片,但将其淡出。
Image image = pictureBox1.Image;


float opacityvalue = opacity = + .5F;
pictureBox1.Image = ChangeOpacity(image,opacityvalue);



}
if (alpha == 0
{
breakCheck = false ;
breakCheck2 = false ;
imageChange = true ;
opacity = 0 ;
timer1.Stop();
}

}

解决方案

您的问题是因为您使用的是 pictureBox1.Image 同时作为练习的源和目标。一旦你的新起点和原始图像丢失就更新了图像。



我设法得到了你正在寻找的效果使用首先 [ ^ ]你建议使用第二个pictureBox并使用其中的图像调用ChangeOpacity函数的链接。

 pictureBox1.Image = ImageUtils.ImageTransparency.ChangeOpacity(picture  Box2  .Image,opacityvalue); 



你会注意到Ravi在文章中使用了一个文件作为他的源图像 - 效果相同。


Hey, I''ve tried using both this batch of code and another batch of code for fading images in and out. Both manage to successfully fade the image out, but I cannot get the second image to fade back in. I believe this has to do with both batches drawing rectangles over the image of either lesser and lesser opacity or greater and greater opacity.

I''ve tried both of those codes within nested if statements that determine whether the image present fades out and when to bring the new image in to start fading it up.
If anyone could point me in the proper direction for changing the opacity of the image back up that would be greatly appreciated.

Here''s my code for refrence:

private void timer1_Tick(object sender, EventArgs e)
        {
           
            //Runs checks to determine if first image should fade out.
            if (imageChange == true)
            {
                if (breakCheck2 == false)
                {

                    //fades out the image.
                    if (alpha++ < 40)
                    {
                        Image image = pictureBox1.Image;
                        using (Graphics g = Graphics.FromImage(image))
                        {
                            Pen pen = new Pen(Color.FromArgb(alpha, 255, 255, 255), image.Width);
                            g.DrawRectangle(pen, -1, -1, image.Width, image.Height);
                            g.Save();

                        }
                        pictureBox1.Image = image;
                        if (alpha == 40)
                        {
                            breakCheck = true;
                        }
                    }
                }
            }
            
            //runs check to see if second image should fade up.
            if (breakCheck == true)
            {
                if (alpha-- > 0 )
                {
                    //grads second image when applicable.
                    if (imageChange == true)
                    {
                        pictureBox1.Image = Properties.Resources.transitionTest;
                        pictureBox1.Invalidate();
                        imageChange = false;
                    }
                    
                    //Should fade in second image, but instead fades it out.
                    Image image = pictureBox1.Image;
           

                    float opacityvalue = opacity = +.5F;
                    pictureBox1.Image = ChangeOpacity(image, opacityvalue);
                    
                   
                    
                }
                if (alpha == 0)
                {
                    breakCheck = false;
                    breakCheck2 = false;
                    imageChange = true;
                    opacity = 0;
                    timer1.Stop();
                }
               
            }

解决方案

Your problem is because you are using pictureBox1.Image as both the source and the target of the exercise. Once you have updated the image once that''s your new starting point and the original image is lost.

I managed to get the effect you''re looking for by using the first[^] of the links you suggested by having a 2nd pictureBox and calling the ChangeOpacity Function with the image from that.

pictureBox1.Image = ImageUtils.ImageTransparency.ChangeOpacity(pictureBox2.Image, opacityvalue);


You''ll notice that Ravi uses a file as his source image in the article - same effect.


这篇关于如何更改此代码以淡入图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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