Winfrom中实现图片切换特效的方法。是我在jb51.net 上看到的! 写的很全。需要的可以看看!!!

来源:互联网 发布:对称经济学知乎 编辑:程序博客网 时间:2024/05/11 17:36
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace MengYu.Image
{
  public class ImageClass
  {
    /// <summary>
    /// 积木特效
    /// </summary>
    /// <param name="bmp">Bitmap 对象</param>
    /// <param name="picBox">PictureBox 对象</param>
    public static void JiMu(Bitmap MyBitmap, PictureBox picBox)
    {
      //以积木效果显示图像
      try
      {
        Graphics myGraphics = picBox.CreateGraphics ();
        int myWidth, myHeight, i, j, iAvg, iPixel;
        Color myColor, myNewColor;
        RectangleF myRect;
        myWidth = MyBitmap.Width;
        myHeight = MyBitmap.Height;
        myRect = new RectangleF(0, 0, myWidth, myHeight);
        Bitmap bitmap = MyBitmap.Clone(myRect, System.Drawing.Imaging.PixelFormat.DontCare);
        i = 0;
        while (i < myWidth - 1)
        {
          j = 0;
          while (j < myHeight - 1)
          {
            myColor = bitmap.GetPixel(i, j);
            iAvg = (myColor.R + myColor.G + myColor.B) / 3;
            iPixel = 0;
            if (iAvg >= 128)
              iPixel = 255;
            else
              iPixel = 0;
            myNewColor = Color.FromArgb(255, iPixel, iPixel, iPixel);
            bitmap.SetPixel(i, j, myNewColor);
            j = j + 1;
          }
          i = i + 1;
        }
        myGraphics.Clear(Color.WhiteSmoke);
        myGraphics.DrawImage(bitmap, new Rectangle(0, 0, myWidth, myHeight));
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "信息提示");
      }
    }
 
    /// <summary>
    /// 马赛克效果
    /// </summary>
    /// <param name="bmp">Bitmap 对象</param>
    /// <param name="picBox">PictureBox 对象</param>
    public static void MaSaiKe(Bitmap MyBitmap,PictureBox picBox)
    {
       //以马赛克效果显示图像
      try
      {
        int dw = MyBitmap.Width / 50;
        int dh = MyBitmap.Height / 50;
        Graphics g = picBox.CreateGraphics();
        g.Clear(Color.Gray);
        Point[] MyPoint = new Point[2500];
        for (int x = 0; x < 50; x++)
          for (int y = 0; y < 50; y++)
          {
            MyPoint[x * 50 + y].X = x * dw;
            MyPoint[x * 50 + y].Y = y * dh;
          }
        Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
        for (int i = 0; i < 10000; i++)
        {
          System.Random MyRandom = new Random();
          int iPos = MyRandom.Next(2500);
          for (int m = 0; m < dw; m++)
            for (int n = 0; n < dh; n++)
            {
              bitmap.SetPixel(MyPoint[iPos].X + m, MyPoint[iPos].Y + n, MyBitmap.GetPixel(MyPoint[iPos].X + m, MyPoint[iPos].Y + n));
            }
          picBox.Refresh();
          picBox.Image = bitmap;
        }
        for (int i = 0; i < 2500; i++)
          for (int m = 0; m < dw; m++)
            for (int n = 0; n < dh; n++)
            {
              bitmap.SetPixel(MyPoint[i].X + m, MyPoint[i].Y + n, MyBitmap.GetPixel(MyPoint[i].X + m, MyPoint[i].Y + n));
            }
        picBox.Refresh();
        picBox.Image = bitmap;
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "信息提示");
      }
    }
 
    /// <summary>
    /// 自动旋转
    /// </summary>
    /// <param name="bmp">Bitmap 对象</param>
    /// <param name="picBox">PictureBox 对象</param>
    public static void XuanZhuan(Bitmap MyBitmap, PictureBox picBox)
    {
      Graphics g = picBox.CreateGraphics();
      float MyAngle = 0;//旋转的角度
      while (MyAngle < 360)
      {
        TextureBrush MyBrush = new TextureBrush(MyBitmap);
        picBox.Refresh();
        MyBrush.RotateTransform(MyAngle);
        g.FillRectangle(MyBrush, 0, 0, MyBitmap.Width,MyBitmap.Height);
        MyAngle += 0.5f;
        System.Threading.Thread.Sleep(50);
      }
    }
    /// <summary>
    /// 上下对接
    /// </summary>
    /// <param name="bmp">Bitmap 对象</param>
    /// <param name="picBox">PictureBox 对象</param>
    public static void DuiJie_ShangXia(Bitmap MyBitmap, PictureBox picBox)
    {
      //以上下对接方式显示图像
      try
      {
        int width = MyBitmap.Width; //图像宽度
        int height = MyBitmap.Height; //图像高度
        Graphics g = picBox.CreateGraphics();
        g.Clear(Color.Gray); //初始为全灰色
        Bitmap bitmap = new Bitmap(width, height);
        int x = 0;
        while (x <= height / 2)
        {
          for (int i = 0; i <= width - 1; i++)
          {
            bitmap.SetPixel(i, x, MyBitmap.GetPixel(i, x));
          }
          for (int i = 0; i <= width - 1; i++)
          {
            bitmap.SetPixel(i, height - x - 1, MyBitmap.GetPixel(i, height - x - 1));
          }
          x++;
          g.Clear(Color.Gray);
          g.DrawImage(bitmap, 0, 0);
          System.Threading.Thread.Sleep(10);
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "信息提示");
      }
    }
 
    /// <summary>
    /// 上下翻转
    /// </summary>
    /// <param name="bmp">Bitmap 对象</param>
    /// <param name="picBox">PictureBox 对象</param>
    public static void FanZhuan_ShangXia(Bitmap MyBitmap, PictureBox picBox)
    {
      //以上下反转方式显示图像
      try
      {
        int width = MyBitmap.Width; //图像宽度
        int height = MyBitmap.Height; //图像高度
        Graphics g = picBox.CreateGraphics();
        g.Clear(Color.Gray); //初始为全灰色
        for (int i = -width / 2; i <= width / 2; i++)
        {
          g.Clear(Color.Gray); //初始为全灰色
          int j = Convert.ToInt32(i * (Convert.ToSingle(height) / Convert.ToSingle(width)));
          Rectangle DestRect = new Rectangle(0, height / 2 - j, width, 2 * j);
          Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
          g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
          System.Threading.Thread.Sleep(10);
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "信息提示");
      }
    }
 
    /// <summary>
    /// 左右对接
    /// </summary>
    /// <param name="bmp">Bitmap 对象</param>
    /// <param name="picBox">PictureBox 对象</param>
    public static void DuiJie_ZuoYou(Bitmap MyBitmap, PictureBox picBox)
    {
      //以左右对接方式显示图像
      try
      {
        int width = MyBitmap.Width; //图像宽度
        int height = MyBitmap.Height; //图像高度
        Graphics g = picBox.CreateGraphics();
        g.Clear(Color.Gray); //初始为全灰色
        Bitmap bitmap = new Bitmap(width, height);
        int x = 0;
        while (x <= width / 2)
        {
          for (int i = 0; i <= height - 1; i++)
          {
            bitmap.SetPixel(x, i, MyBitmap.GetPixel(x, i));
          }
          for (int i = 0; i <= height - 1; i++)
          {
            bitmap.SetPixel(width - x - 1, i,
            MyBitmap.GetPixel(width - x - 1, i));
          }
          x++;
          g.Clear(Color.Gray);
          g.DrawImage(bitmap, 0, 0);
          System.Threading.Thread.Sleep(10);
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "信息提示");
      }
    }
    /// <summary>
    /// 左右翻转
    /// </summary>
    /// <param name="bmp">Bitmap 对象</param>
    /// <param name="picBox">PictureBox 对象</param>
    public static void FanZhuan_ZuoYou(Bitmap MyBitmap, PictureBox picBox)
    {
      //以左右反转方式显示图像
      try
      {
        int width = MyBitmap.Width; //图像宽度
        int height = MyBitmap.Height; //图像高度
        Graphics g = picBox.CreateGraphics();
        g.Clear(Color.Gray); //初始为全灰色
        for (int j = -height / 2; j <= height / 2; j++)
        {
          g.Clear(Color.Gray); //初始为全灰色
          int i = Convert.ToInt32(j * (Convert.ToSingle(width) / Convert.ToSingle(height)));
          Rectangle DestRect = new Rectangle(width / 2 - i, 0, 2 * i, height);
          Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
          g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
          System.Threading.Thread.Sleep(10);
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "信息提示");
      }
    }
 
    /// <summary>
    /// 四周扩散
    /// </summary>
    /// <param name="bmp">Bitmap 对象</param>
    /// <param name="picBox">PictureBox 对象</param>
    public static void KuoSan(Bitmap MyBitmap, PictureBox picBox)
    {
      try
      {
        int width = MyBitmap.Width; //图像宽度
        int height = MyBitmap.Height; //图像高度
        Graphics g = picBox.CreateGraphics();
        g.Clear(Color.Gray); //初始为全灰色
        for (int i = 0; i <= width / 2; i++)
        {
          int j = Convert.ToInt32(i * (Convert.ToSingle(height) / Convert.ToSingle(width)));
          Rectangle DestRect = new Rectangle(width / 2 - i, height / 2 - j, 2 * i, 2 * j);
          Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
          g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
          System.Threading.Thread.Sleep(10);
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "信息提示");
      }
    }
    /// <summary>
    /// 上下拉伸
    /// </summary>
    /// <param name="bmp">Bitmap 对象</param>
    /// <param name="picBox">PictureBox 对象</param>
    public static void LaShen_ShangDaoXiao(Bitmap MyBitmap,PictureBox picBox)
    {
            //以从上向下拉伸方式显示图像
      try
      {
        int width = MyBitmap.Width; //图像宽度
        int height =MyBitmap.Height; //图像高度
        Graphics g =picBox.CreateGraphics();
        g.Clear(Color.Gray); //初始为全灰色
        for (int y = 1; y <= height; y++)
        {
          Bitmap bitmap=MyBitmap.Clone (new Rectangle(0,0,width ,y),System.Drawing .Imaging.PixelFormat .Format24bppRgb );
          g.DrawImage (bitmap,0,0);
          System.Threading.Thread.Sleep(10);
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "信息提示");
      }
    }
  }

}

0 0
原创粉丝点击