C# Winform GDI+(3.图像显示)
1.图像旋转
private void button1_Click(object sender, EventArgs e) { //以任意角度旋转显示图像 Graphics g = this.panel1.CreateGraphics(); float MyAngle = 0;//旋转的角度 while (MyAngle < 360) { TextureBrush MyBrush = new TextureBrush(MyBitmap); this.panel1.Refresh(); MyBrush.RotateTransform(MyAngle); g.FillRectangle(MyBrush, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height); MyAngle += 0.5f; System.Threading.Thread.Sleep(50); } }
2.以不同透明度显示图像
private void button1_Click(object sender, EventArgs e) { //以不同的透明度显示图像 Graphics g = this.panel1.CreateGraphics(); g.SmoothingMode = SmoothingMode.AntiAlias; TextureBrush MyBrush = new TextureBrush(MyBitmap); g.FillRectangle(MyBrush, this.panel1.ClientRectangle); for (int i = 0; i < 255; i++) {//由透明变为不透明 g.FillRectangle(new SolidBrush(Color.FromArgb(i,Color.DarkSlateGray)), this.panel1.ClientRectangle); System.Threading.Thread.Sleep(100); } }
3.以不同的分辨率来显示
private void button1_Click(object sender, EventArgs e) { //以不同的分辨率显示图像 Graphics g = this.panel1.CreateGraphics(); for (int i = 10; i < this.panel1.Height; i += 2) { g.Clear(Color.Gray); MyBitmap.SetResolution(i, i); g.DrawImage(MyBitmap, 0, 0); System.Threading.Thread.Sleep(100); } }
4.图像翻转
private void button1_Click(object sender, EventArgs e) { //以不同翻转方式显示图像 Graphics g = this.panel1.CreateGraphics(); for (int i = 0; i < 17; i++) { switch (i) { case 0: MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 1: MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 2: MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipX); break; case 3: MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipXY); break; case 4: MyBitmap.RotateFlip(RotateFlipType.Rotate180FlipY); break; case 5: MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone); break; case 6: MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 7: MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipXY); break; case 8: MyBitmap.RotateFlip(RotateFlipType.Rotate270FlipY); break; case 9: MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 10: MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 11: MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipXY); break; case 12: MyBitmap.RotateFlip(RotateFlipType.Rotate90FlipY); break; case 13: MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipNone); break; case 14: MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 15: MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipXY); break; case 16: MyBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); break; } g.Clear(Color.White); g.DrawImage(MyBitmap, 0, 0); System.Threading.Thread.Sleep(1000); } }
5.四周渐进扩散方式显示图像(图像由小到大)
private void button1_Click(object sender, EventArgs e) { //以四周扩散方式显示图像 try { int width = this.MyBitmap.Width; //图像宽度 int height = this.MyBitmap.Height; //图像高度 //取得Graphics对象 Graphics g = this.panel1.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, "信息提示"); } }
6.上下渐进拉伸 (同理:左右渐进拉伸)
private void button1_Click(object sender, EventArgs e) { //以从上向下拉伸方式显示图像 try { int width = this.MyBitmap.Width; //图像宽度 int height = this.MyBitmap.Height; //图像高度 Graphics g = this.panel1.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, "信息提示"); } }
7.上下反转 (左右反转)
private void button1_Click(object sender, EventArgs e) { //以上下反转方式显示图像 try { int width = this.MyBitmap.Width; //图像宽度 int height = this.MyBitmap.Height; //图像高度 Graphics g = this.panel1.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, "信息提示"); } }
8.上下对接 (左右对接)
private void button1_Click(object sender, EventArgs e) { //以上下对接方式显示图像 try { int width = this.pictureBox1.Width; //图像宽度 int height = this.pictureBox1.Height; //图像高度 Graphics g = this.panel1.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++; this.panel1.Refresh(); g.DrawImage (bitmap,0,0); System.Threading.Thread.Sleep(10); } } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示"); } }
9.淡入淡出
private void button1_Click(object sender, EventArgs e) { //淡入显示图像 try { Graphics g = this.panel1.CreateGraphics(); g.Clear(Color.Gray); int width = MyBitmap.Width; int height = MyBitmap.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(MyBitmap, 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, "信息提示"); } } private void button3_Click(object sender, EventArgs e) { //淡出显示图像 try { Graphics g = this.panel1.CreateGraphics(); g.Clear(Color.Gray); int width = MyBitmap.Width; int height = MyBitmap.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; //从1到0进行修改色彩变换矩阵主对角线上的数值 //依次减少每种色彩分量 Single count = (float)1.0; while (count > 0.0) { matrix.Matrix00 = (float)count; matrix.Matrix11 = (float)count; matrix.Matrix22 = (float)count; matrix.Matrix33 = (float)count; attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(MyBitmap, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, attributes); System.Threading.Thread.Sleep(20); count = (float)(count - 0.01); } } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示"); } }