欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Winform下实现图片切换特效的方法

程序员文章站 2023-12-17 12:13:34
本文实例讲述了winform下实现图片切换特效的方法,是应用程序开发中非常实用的一个功能。分享给大家供大家参考之用。具体方法如下: 本实例源自网络,功能较为齐全、丰富!主...

本文实例讲述了winform下实现图片切换特效的方法,是应用程序开发中非常实用的一个功能。分享给大家供大家参考之用。具体方法如下:

本实例源自网络,功能较为齐全、丰富!主要功能代码如下:

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 heibaiseimage(bitmap bmp, picturebox picbox)
    {
      //以黑白效果显示图像
      bitmap oldbitmap;
      bitmap newbitmap=null;
      try
      {
        int height = bmp.height;
        int width = bmp.width;
        newbitmap = new bitmap(width, height);
        oldbitmap = bmp;
        color pixel;
        for (int x = 0; x < width; x++)
          for (int y = 0; y < height; y++)
          {
            pixel = oldbitmap.getpixel(x, y);
            int r, g, b, result = 0;
            r = pixel.r;
            g = pixel.g;
            b = pixel.b;
            //实例程序以加权平均值法产生黑白图像
            int itype = 2;
            switch (itype)
            {
              case 0://平均值法
                result = ((r + g + b) / 3);
                break;
              case 1://最大值法
                result = r > g ? r : g;
                result = result > b ? result : b;
                break;
              case 2://加权平均值法
                result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));
                break;
            }
            newbitmap.setpixel(x, y, color.fromargb(result, result, result));
          }
      }
      catch (exception ex)
      {
        messagebox.show(ex.message, "信息提示");
      }
      picbox.image = newbitmap;
    }
    /// <summary>
    /// 雾化效果
    /// </summary>
    /// <param name="bmp"></param>
    /// <param name="picbox"></param>
    public static void wuhuaimage(bitmap bmp, picturebox picbox)
    {
      //雾化效果
      bitmap oldbitmap;
      bitmap newbitmap = null;
      try
      {
        int height = bmp.height;
        int width = bmp.width;
        newbitmap = new bitmap(width, height);
        oldbitmap = bmp;
        color pixel;
        for (int x = 1; x < width - 1; x++)
          for (int y = 1; y < height - 1; y++)
          {
            system.random myrandom = new random();
            int k = myrandom.next(123456);
            //像素块大小
            int dx = x + k % 19;
            int dy = y + k % 19;
            if (dx >= width)
              dx = width - 1;
            if (dy >= height)
              dy = height - 1;
            pixel = oldbitmap.getpixel(dx, dy);
            newbitmap.setpixel(x, y, pixel);
          }
      }
      catch (exception ex)
      {
        messagebox.show(ex.message, "信息提示");
      }
      picbox.image= newbitmap;
    }

    /// <summary>
    /// 锐化效果
    /// </summary>
    /// <param name="bmp"></param>
    /// <param name="picbox"></param>
    public static void ruihuaimage(bitmap bmp, picturebox picbox)
    {
      bitmap oldbitmap;
      bitmap newbitmap = null;
      try
      {
        int height = bmp.height;
        int width = bmp.width;
        newbitmap = new bitmap(width, height);
        oldbitmap = bmp;
        color pixel;
        //拉普拉斯模板
        int[] laplacian ={ -1, -1, -1, -1, 9, -1, -1, -1, -1 };
        for (int x = 1; x < width - 1; x++)
          for (int y = 1; y < height - 1; y++)
          {
            int r = 0, g = 0, b = 0;
            int index = 0;
            for (int col = -1; col <= 1; col++)
              for (int row = -1; row <= 1; row++)
              {
                pixel = oldbitmap.getpixel(x + row, y + col); r += pixel.r * laplacian[index];
                g += pixel.g * laplacian[index];
                b += pixel.b * laplacian[index];
                index++;
              }
            //处理颜色值溢出
            r = r > 255 ? 255 : r;
            r = r < 0 ? 0 : r;
            g = g > 255 ? 255 : g;
            g = g < 0 ? 0 : g;
            b = b > 255 ? 255 : b;
            b = b < 0 ? 0 : b;
            newbitmap.setpixel(x - 1, y - 1, color.fromargb(r, g, b));
          }
      }
      catch (exception ex)
      {
        messagebox.show(ex.message, "信息提示");
      }
      picbox.image = newbitmap;
    }
    /// <summary>
    ///底片效果
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void dipianimage(bitmap bmp, picturebox picbox)
    {
      bitmap oldbitmap;
      bitmap newbitmap = null;
      try
      {
        int height = bmp.height;
        int width = bmp.width;
        newbitmap = new bitmap(width, height);
        oldbitmap = bmp;
        color pixel;
        for (int x = 1; x < width; x++)
        {
          for (int y = 1; y < height; y++)
          {
            int r, g, b;
            pixel = oldbitmap.getpixel(x, y);
            r = 255 - pixel.r;
            g = 255 - pixel.g;
            b = 255 - pixel.b;
            newbitmap.setpixel(x, y, color.fromargb(r, g, b));
          }
        }
      }
      catch (exception ex)
      {
        messagebox.show(ex.message, "信息提示", messageboxbuttons.ok, messageboxicon.information);
      }
      picbox.image = newbitmap;
    }

    /// <summary>
    ///浮雕效果
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void fudiaoimage(bitmap bmp, picturebox picbox)
    {
      bitmap oldbitmap;
      bitmap newbitmap = null;
      try
      {
        int height = bmp.height;
        int width = bmp.width;
        newbitmap = new bitmap(width, height);
        oldbitmap = bmp;
        color pixel1, pixel2;
        for (int x = 0; x < width - 1; x++)
        {
          for (int y = 0; y < height - 1; y++)
          {
            int r = 0, g = 0, b = 0;
            pixel1 = oldbitmap.getpixel(x, y);
            pixel2 = oldbitmap.getpixel(x + 1, y + 1);
            r = math.abs(pixel1.r - pixel2.r + 128);
            g = math.abs(pixel1.g - pixel2.g + 128);
            b = math.abs(pixel1.b - pixel2.b + 128);
            if (r > 255)
              r = 255;
            if (r < 0)
              r = 0;
            if (g > 255)
              g = 255;
            if (g < 0)
              g = 0;
            if (b > 255)
              b = 255;
            if (b < 0)
              b = 0;
            newbitmap.setpixel(x, y, color.fromargb(r, g, b));
          }
        }
      }
      catch (exception ex)
      {
        messagebox.show(ex.message, "信息提示", messageboxbuttons.ok, messageboxicon.information);
      }
      picbox.image = newbitmap;
    }

    /// <summary>
    /// 日光照射效果
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void riguangzhaosheimage(bitmap bmp,picturebox picbox)
    {
      //以光照效果显示图像
      graphics mygraphics = picbox.creategraphics();
      mygraphics.clear(color.white);
      bitmap mybmp = new bitmap(bmp, bmp.width, bmp.height);
      int mywidth = mybmp.width;
      int myheight = mybmp.height;
      bitmap myimage = mybmp.clone(new rectanglef(0, 0, mywidth, myheight), system.drawing.imaging.pixelformat.dontcare);
      int a = mywidth / 2;
      int b = myheight / 2;
      //mycenter图片中心点,发亮此值会让强光中心发生偏移
      point mycenter = new point(mywidth / 2, myheight / 2);
      //r强光照射面的半径,即”光晕”
      int r = math.min(mywidth / 2, myheight / 2);
      for (int i = mywidth - 1; i >= 1; i--)
      {
        for (int j = myheight - 1; j >= 1; j--)
        {
          float mylength = (float)math.sqrt(math.pow((i - mycenter.x), 2) + math.pow((j - mycenter.y), 2));
          //如果像素位于”光晕”之内
          if (mylength < r)
          {
            color mycolor = myimage.getpixel(i, j);
            int r, g, b;
            //220亮度增加常量,该值越大,光亮度越强
            float mypixel = 220.0f * (1.0f - mylength / r);
            r = mycolor.r + (int)mypixel;
            r = math.max(0, math.min(r, 255));
            g = mycolor.g + (int)mypixel;
            g = math.max(0, math.min(g, 255));
            b = mycolor.b + (int)mypixel;
            b = math.max(0, math.min(b, 255));
            //将增亮后的像素值回写到位图
            color mynewcolor = color.fromargb(255, r, g, b);
            myimage.setpixel(i, j, mynewcolor);
          }
        }
        //重新绘制图片
        mygraphics.drawimage(myimage, new rectangle(0, 0, mywidth, myheight));
      }
    }

    /// <summary>
    /// 油画效果
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void youhuaimage(bitmap bmp, picturebox picbox)
    {
      //以油画效果显示图像
      graphics g = picbox.creategraphics();
      int width = bmp.width;
      int height = bmp.height;
      rectanglef rect = new rectanglef(0, 0, width, height);
      bitmap mybitmap = bmp;
      bitmap img = mybitmap.clone(rect, system.drawing.imaging.pixelformat.dontcare);
      //产生随机数序列
      random rnd = new random();
      //取不同的值决定油画效果的不同程度
      int imodel = 2;
      int i = width - imodel;
      while (i > 1)
      {
        int j = height - imodel;
        while (j > 1)
        {
          int ipos = rnd.next(100000) % imodel;
          //将该点的rgb值设置成附近imodel点之内的任一点
          color color = img.getpixel(i + ipos, j + ipos);
          img.setpixel(i, j, color);
          j = j - 1;
        }
        i = i - 1;
      }
      //重新绘制图像
      g.clear(color.white);
      g.drawimage(img, new rectangle(0, 0, width, height));
    }

    /// <summary>
    /// 垂直百叶窗
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void baiyechuang1(bitmap bmp, picturebox picbox)
    {
      //垂直百叶窗显示图像
      try
      {
        bitmap mybitmap =(bitmap) bmp.clone();
        int dw = mybitmap.width / 30;
        int dh = mybitmap.height;
        graphics g = picbox.creategraphics();
        g.clear(color.gray);
        point[] mypoint = new point[30];
        for (int x = 0; x < 30; x++)
        {
          mypoint[x].y = 0;
          mypoint[x].x = x * dw;
        }
        bitmap bitmap = new bitmap(mybitmap.width, mybitmap.height);
        for (int i = 0; i < dw; i++)
        {
          for (int j = 0; j < 30; j++)
          {
            for (int k = 0; k < dh; k++)
            {
              bitmap.setpixel(mypoint[j].x + i, mypoint[j].y + k, mybitmap.getpixel(mypoint[j].x + i, mypoint[j].y + k));
            }
          }
          picbox.refresh();
          picbox.image = bitmap;
          system.threading.thread.sleep(120);
        }
      }
      catch (exception ex)
      {
        messagebox.show(ex.message, "信息提示");
      }
    }

    /// <summary>
    /// 水平百叶窗
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void baiyechuang2(bitmap bmp, picturebox picbox)
    {
      //水平百叶窗显示图像
      try
      {
        bitmap mybitmap = (bitmap)bmp.clone();
        int dh = mybitmap.height / 20;
        int dw = mybitmap.width;
        graphics g = picbox.creategraphics();
        g.clear(color.gray);
        point[] mypoint = new point[20];
        for (int y = 0; y < 20; y++)
        {
          mypoint[y].x = 0;
          mypoint[y].y = y * dh;
        }
        bitmap bitmap = new bitmap(mybitmap.width, mybitmap.height);
        for (int i = 0; i < dh; i++)
        {
          for (int j = 0; j < 20; j++)
          {
            for (int k = 0; k < dw; k++)
            {
              bitmap.setpixel(mypoint[j].x + k, mypoint[j].y + i, mybitmap.getpixel(mypoint[j].x + k, mypoint[j].y + i));
            }
          }
          picbox.refresh();
          picbox.image = bitmap;
          system.threading.thread.sleep(100);
        }
      }
      catch (exception ex)
      {
        messagebox.show(ex.message, "信息提示");
      }
    }
    /// <summary>
    /// 左右拉伸效果
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void lashen_zuodaoyou(bitmap bmp, picturebox picbox)
    {
      //以从左向右拉伸方式显示图像
      try
      {
        int width = bmp.width; //图像宽度
        int height = bmp.height; //图像高度
        graphics g = picbox.creategraphics();
        g.clear(color.gray); //初始为全灰色
        for (int x = 1; x <= width; x++)
        {
          bitmap bitmap = bmp.clone(new rectangle(0, 0, x, height), system.drawing.imaging.pixelformat.format24bpprgb);
          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 danru(bitmap bmp, picturebox picbox)
    {
      //淡入显示图像
      try
      {
        graphics g = picbox.creategraphics();
        g.clear(color.gray);
        int width = bmp.width;
        int height = bmp.height;
        imageattributes attributes = new imageattributes();
        colormatrix matrix = new colormatrix();
        //创建淡入颜色矩阵
        matrix.matrix00 = (float)0.0;
        matrix.matrix01 = (float)0.0;
        matrix.matrix02 = (float)0.0;
        matrix.matrix03 = (float)0.0;
        matrix.matrix04 = (float)0.0;
        matrix.matrix10 = (float)0.0;
        matrix.matrix11 = (float)0.0;
        matrix.matrix12 = (float)0.0;
        matrix.matrix13 = (float)0.0;
        matrix.matrix14 = (float)0.0;
        matrix.matrix20 = (float)0.0;
        matrix.matrix21 = (float)0.0;
        matrix.matrix22 = (float)0.0;
        matrix.matrix23 = (float)0.0;
        matrix.matrix24 = (float)0.0;
        matrix.matrix30 = (float)0.0;
        matrix.matrix31 = (float)0.0;
        matrix.matrix32 = (float)0.0;
        matrix.matrix33 = (float)0.0;
        matrix.matrix34 = (float)0.0;
        matrix.matrix40 = (float)0.0;
        matrix.matrix41 = (float)0.0;
        matrix.matrix42 = (float)0.0;
        matrix.matrix43 = (float)0.0;
        matrix.matrix44 = (float)0.0;
        matrix.matrix33 = (float)1.0;
        matrix.matrix44 = (float)1.0;
        //从0到1进行修改色彩变换矩阵主对角线上的数值
        //使三种基准色的饱和度渐增
        single count = (float)0.0;
        while (count < 1.0)
        {
          matrix.matrix00 = count;
          matrix.matrix11 = count;
          matrix.matrix22 = count;
          matrix.matrix33 = count;
          attributes.setcolormatrix(matrix, colormatrixflag.default, coloradjusttype.bitmap);
          g.drawimage(bmp, new rectangle(0, 0, width, height), 0, 0, width, height, graphicsunit.pixel, attributes);
          system.threading.thread.sleep(200);
          count = (float)(count + 0.02);
        }
      }
      catch (exception ex)
      {
        messagebox.show(ex.message, "信息提示");
      }
    }
    /// <summary>
    /// 逆时针旋转
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void xuanzhuan90(bitmap bmp, picturebox picbox)
    {
      try
      {
        graphics g = picbox.creategraphics();
        bmp.rotateflip(rotatefliptype.rotate90flipxy);
        g.clear(color.white);
        g.drawimage(bmp, 0, 0);
      }
      catch (exception e)
      {
        messagebox.show(e.tostring());
      }
    }
    /// <summary>
    /// 顺时针旋转
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void xuanzhuan270(bitmap bmp, picturebox picbox)
    {
      try
      {
        graphics g = picbox.creategraphics();
        bmp.rotateflip(rotatefliptype.rotate270flipxy);
        g.clear(color.white);
        g.drawimage(bmp, 0, 0);
      }
      catch (exception e)
      {
        messagebox.show(e.tostring());
      }
    }
    /// <summary>
    /// 分块显示
    /// </summary>
    /// <param name="bmp">bitmap 对象</param>
    /// <param name="picbox">picturebox 对象</param>
    public static void fenkuai(bitmap mybitmap, picturebox picbox)
    {
      //以分块效果显示图像
      graphics g = picbox.creategraphics();
      g.clear(color.white);
      int width = mybitmap.width;
      int height = mybitmap.height;
      //定义将图片切分成四个部分的区域
      rectanglef[] block ={
          new rectanglef(0,0,width/2,height/2),
          new rectanglef(width/2,0,width/2,height/2),
          new rectanglef(0,height/2,width/2,height/2),
          new rectanglef(width/2,height/2,width/2,height/2)};
      //分别克隆图片的四个部分
      bitmap[] mybitmapblack ={
        mybitmap.clone(block[0],system.drawing.imaging.pixelformat.dontcare),
        mybitmap.clone(block[1],system.drawing.imaging.pixelformat.dontcare),
        mybitmap.clone(block[2],system.drawing.imaging.pixelformat.dontcare),
        mybitmap.clone(block[3],system.drawing.imaging.pixelformat.dontcare)};
      //绘制图片的四个部分,各部分绘制时间间隔为0.5秒
      g.drawimage(mybitmapblack[0], 0, 0);
      system.threading.thread.sleep(500);
      g.drawimage(mybitmapblack[1], width / 2, 0);
      system.threading.thread.sleep(500);
      g.drawimage(mybitmapblack[3], width / 2, height / 2);
      system.threading.thread.sleep(500);
      g.drawimage(mybitmapblack[2], 0, height / 2);
    }

    /// <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, "信息提示");
      }
    }
  }
}

另外还有一种调用api实现的特效:

// 代码出自 csdn
//仅供参考
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.runtime.interopservices;
namespace windowsformsapplication4
{
  [flags]
  public enum animationtype
  {
    aw_hor_positive = 0x0001,//从左向右显示
    aw_hor_negative = 0x0002,//从右向左显示
    aw_ver_positive = 0x0004,//从上到下显示
    aw_ver_negative = 0x0008,//从下到上显示
    aw_center = 0x0010,//从中间向四周
    aw_hide = 0x10000,
    aw_activate = 0x20000,//普通显示
    aw_slide = 0x40000,
    aw_blend = 0x80000,//透明渐变显示效果
  }
  public partial class form1 : form
  {
    [dllimport("user32.dll")]
    public static extern bool animatewindow(intptr hwnd, uint dwtime, animationtype dwflags);
    private picturebox picturebox1, picturebox2;
    private list<image> girls = new list<image>();
    private timer timer = new timer();
    private int index = 0;
    public form1()
    {
      initializecomponent();
      this.windowstate = formwindowstate.maximized;
      this.formborderstyle = formborderstyle.none;
      this.backcolor = color.black;
      this.doublebuffered = true;
      picturebox1 = new picturebox();
      picturebox1.location = new point(200, 100);
      picturebox1.size = new system.drawing.size(640, 480);
      picturebox1.sizemode = pictureboxsizemode.autosize;
      picturebox1.visible = false;
      this.controls.add(picturebox1);
      picturebox2 = new picturebox();
      picturebox2.location = new point(400, 300);
      picturebox2.size = new system.drawing.size(640, 480);
      picturebox2.sizemode = pictureboxsizemode.autosize;
      picturebox1.visible = false;
      this.controls.add(picturebox2);
      using (openfiledialog dlg = new openfiledialog())
      {
        dlg.multiselect = true;
        dlg.filter = "jpeg files(*.jpg)|*.jpg";
        if (dlg.showdialog() == dialogresult.ok)
        {
          foreach (string file in dlg.filenames)
          {
            girls.add(image.fromfile(file));
          }
        }
      }
      timer.interval = 3000;
      timer.tick += new eventhandler(timer_tick);
      timer.enabled = true;
    }
    void timer_tick(object sender, eventargs e)
    {
      if (girls.count == 0)
      { return; }
      image currentgirl = girls[index++];
      picturebox2.image = currentgirl;
      animatewindow(picturebox2.handle, 1000,
        getrandomanimationtype());
      animatewindow(picturebox1.handle, 1000, animationtype.aw_hide);

      picturebox1.visible = false;
      picturebox temp = picturebox1;
      picturebox1 = picturebox2;
      picturebox2 = temp;
      if (index >= girls.count)
      {
        index = 0;
      }
    }
    private random random = new random();
    private animationtype[] animationtypes = null;
    private animationtype getrandomanimationtype()
    {
      if (animationtypes == null)
      {
        animationtypes = enum.getvalues(typeof(animationtype))
          as animationtype[];
      }
      return animationtypes[random.next(0, animationtypes.length - 1)];
    }
    protected override void onkeydown(keyeventargs e)
    {
      if (e.keycode == keys.escape)
      {
        timer.enabled = false;
        foreach (image girl in girls)
        {
          girl.dispose();
        }
        this.close();
      }
      base.onkeydown(e);
    }
  }
}

希望本文所述实例对大家c#程序设计有所帮助。

上一篇:

下一篇: