在Windows窗体图片框中的图像转换

我是新来的Windows窗体,在我的项目中,我需要在运行时更改图片框中的图像。 我可以在计时器的帮助下做到这一点。 图片只是变了。 是否有可能做一些过渡时,图像变化,例如淡入,淡出,模糊等。如果可能的话,请让我知道如何做到这一点。 我在网上search,但徒劳。提前感谢。

Varun的

只需要获取新的代码文件并粘贴下面的代码即可

类似问题的原始答案,来自另一个问题的答案

回答

using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; public class BlendPanel : Panel { private Image mImg1; private Image mImg2; private float mBlend; public BlendPanel() { SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true); } public Image Image1 { get { return mImg1; } set { mImg1 = value; Invalidate(); } } public Image Image2 { get { return mImg2; } set { mImg2 = value; Invalidate(); } } public float Blend { get { return mBlend; } set { mBlend = value; Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { if (mImg1 == null || mImg2 == null) e.Graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, this.Width, this.Height)); else { Rectangle rc = new Rectangle(0, 0, this.Width, this.Height); ColorMatrix cm = new ColorMatrix(); ImageAttributes ia = new ImageAttributes(); cm.Matrix33 = mBlend; ia.SetColorMatrix(cm); e.Graphics.DrawImage(mImg2, rc, 0, 0, mImg2.Width, mImg2.Height, GraphicsUnit.Pixel, ia); cm.Matrix33 = 1F - mBlend; ia.SetColorMatrix(cm); e.Graphics.DrawImage(mImg1, rc, 0, 0, mImg1.Width, mImg1.Height, GraphicsUnit.Pixel, ia); } base.OnPaint(e); } } 

建立你的项目。 现在,您可以从工具箱的顶部放置BlendPanel到表单上。 这是一个使用它的示例程序:

  private float mBlend; private int mDir = 1; public int count = 0; public Bitmap[] pictures; public void myPhoto() { pictures = new Bitmap[9]; pictures[0] = new Bitmap(@"Library Images\cf3.jpg"); pictures[1] = new Bitmap(@"Library Images\cf4.jpg"); pictures[2] = new Bitmap(@"Library Images\l1.JPG"); pictures[3] = new Bitmap(@"Library Images\l2.JPG"); pictures[4] = new Bitmap(@"Library Images\l3.JPG"); pictures[5] = new Bitmap(@"Library Images\l4.JPG"); pictures[6] = new Bitmap(@"Library Images\l5.JPG"); pictures[7] = new Bitmap(@"Library Images\l6.JPG"); pictures[8] = new Bitmap(@"Library Images\l7.JPG"); timer1.Interval = 50; //time of transition timer1.Tick += BlendTick; try { blendPanel1.Image1 = pictures[count]; blendPanel1.Image2 = pictures[++count]; } catch { } timer1.Enabled = true; } private void BlendTick(object sender, EventArgs e) { mBlend += mDir * 0.02F; if (mBlend > 1) { mBlend = 0.0F; if ((count + 1) < pictures.Length) { blendPanel1.Image1 = pictures[count]; blendPanel1.Image2 = pictures[++count]; } else { blendPanel1.Image1 = pictures[count]; blendPanel1.Image2 = pictures[0]; count = 0; } } blendPanel1.Blend = mBlend; } 

你需要修改new Bitmap(@"yourimagePath"); 调用。 建立并运行。 您应该看到显示的图像从第一张图像平滑地变形到第二张图像,没有任何闪烁。

我希望它有助于其他…

没有内置的对这种效果的支持,但是你可以实现它们。 我建议编写一个自定义控件来呈现图像,并有一个淡入淡出的方法,淡入淡出本身可以达到与alpha图形绘图与.NET图形类。

但是,Graphics类不是很快,我不建议使用这种技术来处理大图像。 如果你需要一些花哨的用户界面加快效果,看看WPF。

通过使用ColorMatrix类,混合效果很容易。 在这个线程的答案中有一个很好的例子。

弄模糊的一个简单的方法是调整图像的大小,使其变小,然后重新绘制,使其变大。 Graphics.InterpolationMode属性影响你将得到的模糊类型。

这些都是快捷的DIY解决方案。 任何体面的图形库都有这些内置的操作。 你可能想要免费的东西,看看ImageMagick.NET

简而言之,不是没有外部(第三方)库。