C#实现图片特效 ( 九)
十一: 扭曲效果
原理: 将图像缩放为一个非矩形的平等四边形即可
效果图:
实现代码:
扭曲效果 private void button1_Click(object sender, EventArgs e) { //以扭曲效果显示图像 if (h == panel1.Height/2) { w = 0; h = 0; } Size offset =new Size (w++,h++);//设置偏移量 Graphics g = panel1.CreateGraphics(); Rectangle rect = this.panel1.ClientRectangle; Point[] points = new Point[3]; points[0] = new Point(rect.Left+offset.Width ,rect.Top +offset .Height); points[1] = new Point(rect.Right, rect.Top + offset.Height); points[2] = new Point(rect.Left, rect.Bottom - offset.Height); g.Clear(Color.White); g.DrawImage(MyBitmap, points); }
十二.积木效果
原理: 对图像中的各个像素点着重(即加大分像素的颜色值)着色.
效果图:
实现代码:
积木效果 private void button1_Click(object sender, EventArgs e) { //以积木效果显示图像 try { Graphics myGraphics = this.panel1.CreateGraphics (); //Bitmap myBitmap = new Bitmap(this.BackgroundImage); 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, "信息提示"); } }
说明.这些大多为静态图. 后面会有图像的动态显示. 如分块合成图像, 四周扩散显示图像, 上下对接显示图像等.
这些也许能说明一下 PPT或者手机中的图片效果处理程序是如果做出来的.原理应该是相通的.
制作图像一般常用的类有: Bitmap; Graphics; Rectangle;Color; 用到的方法是 Graphics类的DrawImage;
此方法共有30个版本, 我习惯用 DrawImage("图像", "图框") 版本.
因为这个版本的思想是最简单的----把一张**地图像装在一个**地框里! (**代表某种效果的图像和某种效果的框)
如. g.DrawImage(new Bitmap("myPicture"), new Rectangle(0, 0, myWidth, myHeight));