c#中picturebox的图像之间的转换
本文关键字:之间 转换 图像 picturebox | 更新日期: 2023-09-27 18:02:07
可能重复:
Windows窗体图片框中的图像转换
我使用下面的代码每5秒更改一次图片框中的图像,但更改图像时效果不太好:我想要图像之间的过渡效果。
使用的代码
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Bitmap[] 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");
Random random = new Random();
while (true)
{
int attempt = random.Next(0, pictures.Length);
pictureBox1.Image = pictures[attempt];
System.Threading.Thread.Sleep(5000);
}
}
示例代码非常感谢提前。。。
只需获取新的代码文件并将下面的代码粘贴到中
类似问题的原始答案,取自另一个问题的答案
答案
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");
调用。构建并运行。您应该看到显示的图像从第一个图像平滑地变形到第二个图像,没有任何闪烁。
我希望它能帮助其他。。。