This article describes the method of implementing image switching effects under Winform, which is a very practical function in application development. Share it for your reference. The specific methods are as follows:
This example is from the Internet and has relatively complete and rich functions! The main function codes are as follows:
using System; using ; using ; using ; using ; using ; using ; using .Drawing2D; using ; using ; namespace { public class ImageClass { /// <summary> /// Convert the image to a black and white effect /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void HeiBaiSeImage(Bitmap bmp, PictureBox picBox) { //Display image in black and white effect Bitmap oldBitmap; Bitmap newBitmap=null; try { int Height = ; int 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 = (x, y); int r, g, b, Result = 0; r = ; g = ; b = ; //The example program uses the weighted average method to generate black and white images int iType = 2; switch (iType) { case 0://Average method Result = ((r + g + b) / 3); break; case 1://Maximum value method Result = r > g ? r : g; Result = Result > b ? Result : b; break; case 2://Weighted average method Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b)); break; } (x, y, (Result, Result, Result)); } } catch (Exception ex) { (, "Information prompt"); } = newBitmap; } /// <summary> /// Atomization effect /// </summary> /// <param name="bmp"></param> /// <param name="picBox"></param> public static void WuHuaImage(Bitmap bmp, PictureBox picBox) { //Atomization effect Bitmap oldBitmap; Bitmap newBitmap = null; try { int Height = ; int 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++) { MyRandom = new Random(); int k = (123456); //Pixel block size int dx = x + k % 19; int dy = y + k % 19; if (dx >= Width) dx = Width - 1; if (dy >= Height) dy = Height - 1; pixel = (dx, dy); (x, y, pixel); } } catch (Exception ex) { (, "Information prompt"); } = newBitmap; } /// <summary> /// Sharpening effect /// </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 = ; int Width = ; newBitmap = new Bitmap(Width, Height); oldBitmap = bmp; Color pixel; //Laplace template 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 = (x + row, y + col); r += * Laplacian[Index]; g += * Laplacian[Index]; b += * Laplacian[Index]; Index++; } // Process color value overflow 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; (x - 1, y - 1, (r, g, b)); } } catch (Exception ex) { (, "Information prompt"); } = newBitmap; } /// <summary> ///Negative effect /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void DiPianImage(Bitmap bmp, PictureBox picBox) { Bitmap oldBitmap; Bitmap newBitmap = null; try { int Height = ; int 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 = (x, y); r = 255 - ; g = 255 - ; b = 255 - ; (x, y, (r, g, b)); } } } catch (Exception ex) { (, "Information prompt", , ); } = newBitmap; } /// <summary> ///Relief effect /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void FuDiaoImage(Bitmap bmp, PictureBox picBox) { Bitmap oldBitmap; Bitmap newBitmap = null; try { int Height = ; int 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 = (x, y); pixel2 = (x + 1, y + 1); r = ( - + 128); g = ( - + 128); 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; (x, y, (r, g, b)); } } } catch (Exception ex) { (, "Information prompt", , ); } = newBitmap; } /// <summary> /// Sunlight effect /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void RiGuangZhaoSheImage(Bitmap bmp,PictureBox picBox) { //Display image with lighting effect Graphics MyGraphics = (); (); Bitmap MyBmp = new Bitmap(bmp, , ); int MyWidth = ; int MyHeight = ; Bitmap MyImage = (new RectangleF(0, 0, MyWidth, MyHeight), ); int A = MyWidth / 2; int B = MyHeight / 2; //MyCenter image center point, illuminating this value will cause the center of the strong light to shift Point MyCenter = new Point(MyWidth / 2, MyHeight / 2); //The radius of the surface of the R strong light irradiation, that is, "halo" int R = (MyWidth / 2, MyHeight / 2); for (int i = MyWidth - 1; i >= 1; i--) { for (int j = MyHeight - 1; j >= 1; j--) { float MyLength = (float)(((i - ), 2) + ((j - ), 2)); //If the pixel is within the "halo" if (MyLength < R) { Color MyColor = (i, j); int r, g, b; //220 brightness increases constant, the larger the value, the stronger the brightness becomes. float MyPixel = 220.0f * (1.0f - MyLength / R); r = + (int)MyPixel; r = (0, (r, 255)); g = + (int)MyPixel; g = (0, (g, 255)); b = + (int)MyPixel; b = (0, (b, 255)); //Write back the brightened pixel value to the bitmap Color MyNewColor = (255, r, g, b); (i, j, MyNewColor); } } //Repaint the picture (MyImage, new Rectangle(0, 0, MyWidth, MyHeight)); } } /// <summary> /// Oil painting effect /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void YouHuaImage(Bitmap bmp, PictureBox picBox) { //Display images with oil painting effect Graphics g = (); int width = ; int height = ; RectangleF rect = new RectangleF(0, 0, width, height); Bitmap MyBitmap = bmp; Bitmap img = (rect, ); // Generate a sequence of random numbers Random rnd = new Random(); //Take different values to determine the degree of oil painting effect int iModel = 2; int i = width - iModel; while (i > 1) { int j = height - iModel; while (j > 1) { int iPos = (100000) % iModel; //Set the RGB value of this point to any point within the nearby iModel point Color color = (i + iPos, j + iPos); (i, j, color); j = j - 1; } i = i - 1; } //Repaint the image (); (img, new Rectangle(0, 0, width, height)); } /// <summary> /// Vertical blinds /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void BaiYeChuang1(Bitmap bmp, PictureBox picBox) { //Vertical blinds display image try { Bitmap MyBitmap =(Bitmap) (); int dw = / 30; int dh = ; Graphics g = (); (); 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(, ); for (int i = 0; i < dw; i++) { for (int j = 0; j < 30; j++) { for (int k = 0; k < dh; k++) { (MyPoint[j].X + i, MyPoint[j].Y + k, (MyPoint[j].X + i, MyPoint[j].Y + k)); } } (); = bitmap; (120); } } catch (Exception ex) { (, "Information prompt"); } } /// <summary> /// Horizontal blinds /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void BaiYeChuang2(Bitmap bmp, PictureBox picBox) { //Horizontal blinds display image try { Bitmap MyBitmap = (Bitmap)(); int dh = / 20; int dw = ; Graphics g = (); (); 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(, ); for (int i = 0; i < dh; i++) { for (int j = 0; j < 20; j++) { for (int k = 0; k < dw; k++) { (MyPoint[j].X + k, MyPoint[j].Y + i, (MyPoint[j].X + k, MyPoint[j].Y + i)); } } (); = bitmap; (100); } } catch (Exception ex) { (, "Information prompt"); } } /// <summary> /// Left and left stretching effect /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void LaShen_ZuoDaoYou(Bitmap bmp, PictureBox picBox) { //Show the image by stretching from left to right try { int width = ; //Image Width int height = ; //Image height Graphics g = (); (); //Initially all gray for (int x = 1; x <= width; x++) { Bitmap bitmap = (new Rectangle(0, 0, x, height), .Format24bppRgb); (bitmap, 0, 0); (10); } } catch (Exception ex) { (, "Information prompt"); } } /// <summary> /// Fade in effect /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void DanRu(Bitmap bmp, PictureBox picBox) { //Fake in to display the image try { Graphics g = (); (); int width = ; int height = ; ImageAttributes attributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix(); //Create a fade color matrix 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; //Modify the value on the main diagonal of the color transformation matrix from 0 to 1 //Get the saturation of the three reference colors gradually increases Single count = (float)0.0; while (count < 1.0) { matrix.Matrix00 = count; matrix.Matrix11 = count; matrix.Matrix22 = count; matrix.Matrix33 = count; (matrix, , ); (bmp, new Rectangle(0, 0, width, height), 0, 0, width, height, , attributes); (200); count = (float)(count + 0.02); } } catch (Exception ex) { (, "Information prompt"); } } /// <summary> /// Rotate counterclockwise /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void XuanZhuan90(Bitmap bmp, PictureBox picBox) { try { Graphics g = (); (RotateFlipType.Rotate90FlipXY); (); (bmp, 0, 0); } catch (Exception e) { (()); } } /// <summary> /// Rotate clockwise /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void XuanZhuan270(Bitmap bmp, PictureBox picBox) { try { Graphics g = (); (RotateFlipType.Rotate270FlipXY); (); (bmp, 0, 0); } catch (Exception e) { (()); } } /// <summary> /// Display in blocks /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void FenKuai(Bitmap MyBitmap, PictureBox picBox) { //Show images with chunking effects Graphics g = (); (); int width = ; int height = ; //Define the area that divides the picture into four parts 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)}; //Clone the four parts of the picture separately Bitmap[] MyBitmapBlack ={ (block[0],), (block[1],), (block[2],), (block[3],)}; //Draw four parts of the picture, and the drawing time interval between each part is 0.5 seconds (MyBitmapBlack[0], 0, 0); (500); (MyBitmapBlack[1], width / 2, 0); (500); (MyBitmapBlack[3], width / 2, height / 2); (500); (MyBitmapBlack[2], 0, height / 2); } /// <summary> /// Building blocks special effects /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void JiMu(Bitmap MyBitmap, PictureBox picBox) { //Show images with building block effects try { Graphics myGraphics = (); int myWidth, myHeight, i, j, iAvg, iPixel; Color myColor, myNewColor; RectangleF myRect; myWidth = ; myHeight = ; myRect = new RectangleF(0, 0, myWidth, myHeight); Bitmap bitmap = (myRect, ); i = 0; while (i < myWidth - 1) { j = 0; while (j < myHeight - 1) { myColor = (i, j); iAvg = ( + + ) / 3; iPixel = 0; if (iAvg >= 128) iPixel = 255; else iPixel = 0; myNewColor = (255, iPixel, iPixel, iPixel); (i, j, myNewColor); j = j + 1; } i = i + 1; } (); (bitmap, new Rectangle(0, 0, myWidth, myHeight)); } catch (Exception ex) { (, "Information prompt"); } } /// <summary> /// Mosaic effect /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void MaSaiKe(Bitmap MyBitmap,PictureBox picBox) { //Show images with mosaic effect try { int dw = / 50; int dh = / 50; Graphics g = (); (); 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(, ); for (int i = 0; i < 10000; i++) { MyRandom = new Random(); int iPos = (2500); for (int m = 0; m < dw; m++) for (int n = 0; n < dh; n++) { (MyPoint[iPos].X + m, MyPoint[iPos].Y + n, (MyPoint[iPos].X + m, MyPoint[iPos].Y + n)); } (); = bitmap; } for (int i = 0; i < 2500; i++) for (int m = 0; m < dw; m++) for (int n = 0; n < dh; n++) { (MyPoint[i].X + m, MyPoint[i].Y + n, (MyPoint[i].X + m, MyPoint[i].Y + n)); } (); = bitmap; } catch (Exception ex) { (, "Information prompt"); } } /// <summary> /// Automatic rotation /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void XuanZhuan(Bitmap MyBitmap, PictureBox picBox) { Graphics g = (); float MyAngle = 0;//The rotation angle while (MyAngle < 360) { TextureBrush MyBrush = new TextureBrush(MyBitmap); (); (MyAngle); (MyBrush, 0, 0, ,); MyAngle += 0.5f; (50); } } /// <summary> /// Up and down docking /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void DuiJie_ShangXia(Bitmap MyBitmap, PictureBox picBox) { //Down-up and down-to-point image display try { int width = ; //Image Width int height = ; //Image height Graphics g = (); (); //Initially all gray Bitmap bitmap = new Bitmap(width, height); int x = 0; while (x <= height / 2) { for (int i = 0; i <= width - 1; i++) { (i, x, (i, x)); } for (int i = 0; i <= width - 1; i++) { (i, height - x - 1, (i, height - x - 1)); } x++; (); (bitmap, 0, 0); (10); } } catch (Exception ex) { (, "Information prompt"); } } /// <summary> /// Flip up and down /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void FanZhuan_ShangXia(Bitmap MyBitmap, PictureBox picBox) { //Previously display image try { int width = ; //Image Width int height = ; //Image height Graphics g = (); (); //Initially all gray for (int i = -width / 2; i <= width / 2; i++) { (); //Initially all gray int j = Convert.ToInt32(i * ((height) / (width))); Rectangle DestRect = new Rectangle(0, height / 2 - j, width, 2 * j); Rectangle SrcRect = new Rectangle(0, 0, , ); (MyBitmap, DestRect, SrcRect, ); (10); } } catch (Exception ex) { (, "Information prompt"); } } /// <summary> ///Door left and right /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void DuiJie_ZuoYou(Bitmap MyBitmap, PictureBox picBox) { //Display image in left and right docking try { int width = ; //Image Width int height = ; //Image height Graphics g = (); (); //Initially all gray Bitmap bitmap = new Bitmap(width, height); int x = 0; while (x <= width / 2) { for (int i = 0; i <= height - 1; i++) { (x, i, (x, i)); } for (int i = 0; i <= height - 1; i++) { (width - x - 1, i, (width - x - 1, i)); } x++; (); (bitmap, 0, 0); (10); } } catch (Exception ex) { (, "Information prompt"); } } /// <summary> /// Flip left and right /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void FanZhuan_ZuoYou(Bitmap MyBitmap, PictureBox picBox) { //Show the image in the left and right reverse mode try { int width = ; //Image Width int height = ; //Image height Graphics g = (); (); //Initially all gray for (int j = -height / 2; j <= height / 2; j++) { (); //Initially all gray int i = Convert.ToInt32(j * ((width) / (height))); Rectangle DestRect = new Rectangle(width / 2 - i, 0, 2 * i, height); Rectangle SrcRect = new Rectangle(0, 0, , ); (MyBitmap, DestRect, SrcRect, ); (10); } } catch (Exception ex) { (, "Information prompt"); } } /// <summary> ///Dispers around /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void KuoSan(Bitmap MyBitmap, PictureBox picBox) { try { int width = ; //Image Width int height = ; //Image height Graphics g = (); (); //Initially all gray for (int i = 0; i <= width / 2; i++) { int j = Convert.ToInt32(i * ((height) / (width))); Rectangle DestRect = new Rectangle(width / 2 - i, height / 2 - j, 2 * i, 2 * j); Rectangle SrcRect = new Rectangle(0, 0, , ); (MyBitmap, DestRect, SrcRect, ); (10); } } catch (Exception ex) { (, "Information prompt"); } } /// <summary> /// Stretch up and down /// </summary> /// <param name="bmp">Bitmap object</param> /// <param name="picBox">PictureBox object</param> public static void LaShen_ShangDaoXiao(Bitmap MyBitmap,PictureBox picBox) { //Show the image by stretching from top to bottom try { int width = ; //Image Width int height =; //Image height Graphics g =(); (); //Initially all gray for (int y = 1; y <= height; y++) { Bitmap bitmap= (new Rectangle(0,0,width ,y), . .Format24bppRgb ); (bitmap,0,0); (10); } } catch (Exception ex) { (, "Information prompt"); } } } }
There is also a special effect of calling API implementation:
// Code from CSDN//For reference onlyusing System; using ; using ; using ; using ; using ; using ; using ; namespace WindowsFormsApplication4 { [Flags] public enum AnimationType { AW_HOR_POSITIVE = 0x0001,//Show from left to right AW_HOR_NEGATIVE = 0x0002,//Show from right to left AW_VER_POSITIVE = 0x0004,//Show from top to bottom AW_VER_NEGATIVE = 0x0008,//Show from bottom to top AW_CENTER = 0x0010,//From the middle to the surrounding AW_HIDE = 0x10000, AW_ACTIVATE = 0x20000,//On normal display AW_SLIDE = 0x40000, AW_BLEND = 0x80000,//Transparent gradient display effect } public partial class Form1 : Form { [DllImport("")] 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(); = ; = ; = ; = true; pictureBox1 = new PictureBox(); = new Point(200, 100); = new (640, 480); = ; = false; (pictureBox1); pictureBox2 = new PictureBox(); = new Point(400, 300); = new (640, 480); = ; = false; (pictureBox2); using (OpenFileDialog dlg = new OpenFileDialog()) { = true; = "jpeg files(*.jpg)|*.jpg"; if (() == ) { foreach (string file in ) { ((file)); } } } = 3000; += new EventHandler(timer_Tick); = true; } void timer_Tick(object sender, EventArgs e) { if ( == 0) { return; } Image currentGirl = girls[index++]; = currentGirl; AnimateWindow(, 1000, GetRandomAnimationType()); AnimateWindow(, 1000, AnimationType.AW_HIDE); = false; PictureBox temp = pictureBox1; pictureBox1 = pictureBox2; pictureBox2 = temp; if (index >= ) { index = 0; } } private Random random = new Random(); private AnimationType[] animationTypes = null; private AnimationType GetRandomAnimationType() { if (animationTypes == null) { animationTypes = (typeof(AnimationType)) as AnimationType[]; } return animationTypes[(0, - 1)]; } protected override void OnKeyDown(KeyEventArgs e) { if ( == ) { = false; foreach (Image girl in girls) { (); } (); } (e); } } }
I hope that the examples described in this article will be helpful to everyone's C# programming.