说明
由于是以动画方式显示图像这里没办法直接贴静态截图因此决定给园友开源将所有的可运行代码附在案例后面由于所有的动画处理图像的对象放在都pictureBox控件中同时定义的类都大同小异因此这里先把下面案例中要用到的所有类及装载图像的代码给大家运行时用这里的代码加下面任意一个实例的代码即可运行程序!
private Bitmap SourceBitmap; private Bitmap MyBitmap; private void button_Click(object sender EventArgs e) { //打开图像文件 OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialogFilter = 图像文件(JPeg Gif Bmp etc) |*jpg;*jpeg;*gif;*bmp;*tif; *tiff; *png|
JPeg 图像文件(*jpg;*jpeg) |*jpg;*jpeg |GIF 图像文件(*gif)|*gif |BMP图像文件(*bmp)|*bmp |Tiff图像文件(*tif;*tiff)|*tif;*tiff|Png图像
文件(*png)| *png |所有文件(**)|**; if (openFileDialogShowDialog() == DialogResultOK) { //得到原始大小的图像 SourceBitmap = new Bitmap(openFileDialogFileName); //得到缩放后的图像 MyBitmap = new Bitmap(SourceBitmap thispictureBoxWidth this
pictureBoxHeight); thispictureBoxImage = MyBitmap; } }
一以上下反转的方式显示图像
原理计算图像位置和高度后以高度的一半为轴进行对换上下半边的图像
代码
private void button_Click(object sender EventArgs e) { try { int width = thisMyBitmapWidth; //图像宽度 int height = thisMyBitmapHeight; //图像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); for (int i = width / ; i <= width / ; i++) { gClear(ColorGray); int j = ConvertToInt(i * (ConvertToSingle(height) / ConvertToS
ingle(width))); Rectangle DestRect = new Rectangle( height / j width * j); Rectangle SrcRect = new Rectangle( MyBitmapWidth MyBitmapHeight); gDrawImage(MyBitmap DestRect SrcRect GraphicsUnitPixel); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }
二以上下对接的方式显示图像
原理首先将图像分为上下两部分 然后分别显示
代码
private void button_Click(object sender EventArgs e) { try { int width = thispictureBoxWidth; //图像宽度 int height = thispictureBoxHeight; //图像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); Bitmap bitmap = new Bitmap(width height); int x = ; while (x <= height / ) { for (int i = ; i <= width ; i++) { bitmapSetPixel(i x MyBitmapGetPixel(i x)); } for (int i = ; i <= width ; i++) { bitmapSetPixel(i height x MyBitmapGetPixel(i height x )); } x++; thispanelRefresh(); gDrawImage (bitmap); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }
三以四周扩散的方式显示图像
原理首先设置图像显示的位置 然后按高度和宽度的比例循环输出 直到高度和宽度为原始大小
代码
private void button_Click(object sender EventArgs e) { try { int width = thisMyBitmapWidth; //图像宽度 int height = thisMyBitmapHeight; //图像高度 //取得Graphics对象 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); //初始为全灰色 for (int i = ; i <= width / ; i++) { int j = ConvertToInt (i*(ConvertToSingle(height) / ConvertToSingle(width))); Rectangle DestRect = new Rectangle(width / i height/j * i *j); Rectangle SrcRect = new Rectangle( MyBitmapWidth MyBitmapHeight); gDrawImage(MyBitmap DestRect SrcRect GraphicsUnitPixel); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }
四以分块效果显示图像
原理首先将图分为几块 再使用 Bitmap 类的 Clone方法从原图指定的块中复制图像
最后将这些块依次显示出来便可
代码
private void button_Click(object sender EventArgs e) { Graphics g = thispanelCreateGraphics(); gClear(ColorWhite); int width = MyBitmapWidth; int height = MyBitmapHeight; //定义将图片切分成四个部分的区域 RectangleF[] block ={ new RectangleF(width/height/)
new RectangleF(width/width/height/) new RectangleF(height/width/height/) new RectangleF(width/height/width/height/)}; //分别克隆图片的四个部分 Bitmap[] MyBitmapBlack ={ MyBitmapClone(block[]SystemDrawingImagingPixelFormatDontCare) MyBitmapClone(block[]SystemDrawingImagingPixelFormatDontCare) MyBitmapClone(block[]SystemDrawingImagingPixelFormatDontCare) MyBitmapClone(block[]SystemDrawingImagingPixelFormatDontCare)}; //绘制图片的四个部分各部分绘制时间间隔为秒 gDrawImage(MyBitmapBlack[] ); SystemThreadingThreadSleep(); gDrawImage(MyBitmapBlack[] width / ); SystemThreadingThreadSleep(); gDrawImage(MyBitmapBlack[] width / height / ); SystemThreadingThreadSleep(); gDrawImage(MyBitmapBlack[] height / ); }
五以淡入淡出效果显示图像
原理使用 ImageAttrributes 类的 SetColorMatrix() 方法设置颜色 调整矩阵实现淡出的效果 此类还可以对颜色进行校正 调暗 调亮和移除等
代码
private void button_Click(object sender EventArgs e) { try { Graphics g = thispanelCreateGraphics(); gClear(ColorGray); int width = MyBitmapWidth; int height = MyBitmapHeight; ImageAttributes attributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix(); //创建淡入颜色矩阵 matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); //从到进行修改色彩变换矩阵主对角线上的数值 //使三种基准色的饱和度渐增 Single count = (float); while (count < ) { matrixMatrix = count; matrixMatrix = count; matrixMatrix = count; matrixMatrix = count; attributesSetColorMatrix(matrix ColorMatrixFlagDefault ColorAdjustTypeBitmap); gDrawImage(MyBitmap new Rectangle( width height) width height GraphicsUnitPixel attributes);private void button_Click(object sender EventArgs e) SystemThreadingThreadSleep(); count = (float)(count + ); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } } private void button_Click(object sender EventArgs e) { try { Graphics g = thispanelCreateGraphics(); gClear(ColorGray); int width = MyBitmapWidth; int height = MyBitmapHeight; ImageAttributes attributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix(); //创建淡出颜色矩阵 matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); matrixMatrix = (float); //从到进行修改色彩变换矩阵主对角线上的数值 //依次减少每种色彩分量 Single count = (float); while (count > ) { matrixMatrix = (float)count; matrixMatrix = (float)count; matrixMatrix = (float)count; matrixMatrix = (float)count; attributesSetColorMatrix(matrix ColorMatrixFlagDefault ColorAdjustTypeBitmap); gDrawImage(MyBitmap new Rectangle( width height) width height GraphicsUnitPixel attributes); SystemThreadingThreadSleep(); count = (float)(count ); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }
六以左右对接的方式显示图像
原理首先将图像分为左右两部分 然后分别显示
代码
private void button_Click(object sender EventArgs e) { //以左右对接方式显示图像 try { int width = thisMyBitmapWidth; //图像宽度 int height = thisMyBitmapHeight; //图像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); //初始为全灰色 Bitmap bitmap = new Bitmap(width height); int x = ; while (x <= width / ) { for (int i = ; i <= height ; i++) { bitmapSetPixel(x i MyBitmapGetPixel(x i)); } for (int i = ; i <= height ; i++) { bitmapSetPixel(width x i MyBitmapGetPixel(width x i)); } x++; thispanelRefresh(); gDrawImage (bitmap); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }
七以左右反转的方式显示图像
原理计算图像位置和高度后以宽度的一半为轴进行对换左右半边的图像
代码
private void button_Click(object sender EventArgs e) { //以左右反转方式显示图像 try { int width = thisMyBitmapWidth; //图像宽度 int height = thisMyBitmapHeight; //图像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); //初始为全灰色 for (int j = height / ; j <= height / ; j++) { gClear(ColorGray); //初始为全灰色 int i = ConvertToInt(j * (ConvertToSingle(width) / ConvertToSingle(height))); Rectangle DestRect = new Rectangle(width / i * i height); Rectangle SrcRect = new Rectangle( MyBitmapWidth MyBitmapHeight); gDrawImage(MyBitmap DestRect SrcRect GraphicsUnitPixel); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }
八以从上向下拉伸的方式显示图像
原理将图像的宽度不变每次显示图像的一部分 直到将图片完全显示
代码
private void button_Click(object sender EventArgs e) { //以从上向下拉伸方式显示图像 try { int width = thisMyBitmapWidth; //图像宽度 int height = thisMyBitmapHeight; //图像高度 Graphics g = thispanelCreateGraphics(); gClear(ColorGray); //初始为全灰色 for (int y = ; y <= height; y++) { Bitmap bitmap=MyBitmapClone (new Rectangle(width y) SystemDrawing ImagingPixelFormat FormatbppRgb ); gDrawImage (bitmap); SystemThreadingThreadSleep(); } } catch (Exception ex) { MessageBoxShow(exMessage 信息提示); } }
九以从左向右拉伸的方式显示图像
原理将图像的高度不变每次显示图像的一部分 直到将图片完全显示
代码
private void button_Click(object sender EventArgs e) {//以从左向右拉伸方式显示图像try { int width = thisMyBitmapWidth; //图像宽度 int height = thisMyBitmapHeight; //图像高度 Graphics g = thispanelCreateGraphics();gClear(ColorGray); //初始为全灰色 for (int x = ; x <= width; x++) { Bitmap bitmap=MyBitmapClone (new Rectangle (x height) SystemDrawing ImagingPixelFormat FormatbppRgb ); gDrawImage (bitmap); SystemThreadingThreadSleep(); } } catch (Exception ex){MessageBoxShow(exMessage 信息提示); } }
十以任意角度旋转图像
原理主要使用了 Graphics 类提供的 RotateTransform() 方法对图像进行旋转
代码
private void button_Click(object sender EventArgs e) { //以任意角度旋转显示图像 Graphics g = thispanelCreateGraphics(); float MyAngle = ;//旋转的角度 while (MyAngle < ) { TextureBrush MyBrush = new TextureBrush(MyBitmap); thispanelRefresh(); MyBrushRotateTransform(MyAngle); gFillRectangle(MyBrush thisClientRectangleWidth thisClientRectangleHeight); MyAngle += f; SystemThreadingThreadSleep(); } }
十一以椭圆的方式显示图像
原理主要使用了 Graphics 类提供的 FillEllipse() 方法和 TextureBrush() 方法
代码
十二以不同的透明度显示图像
原理Graphics 类的 FromArgb() 方法
代码
private void button_Click(object sender EventArgs e) { //以不同的透明度显示图像 Graphics g = thispanelCreateGraphics(); gSmoothingMode = SmoothingModeAntiAlias; TextureBrush MyBrush = new TextureBrush(MyBitmap); gFillRectangle(MyBrush thispanelClientRectangle); for (int i = ; i < ; i++) {//由透明变为不透明 gFillRectangle(new SolidBrush(ColorFromArgb(iColorDarkS
lateGray)) thispanelClientRectangle); SystemThreadingThreadSleep(); } }
十三以不同分辨率显示图像
原理Bitmap 类的 SetResolution 方法
代码
private void button_Click(object sender EventArgs e) { //以不同的分辨率显示图像 Graphics g = thispanelCreateGraphics(); for (int i = ; i < thispanelHeight; i += ) { gClear(ColorGray); MyBitmapSetResolution(i i); gDrawImage(MyBitmap ); SystemThreadingThreadSleep(); } }
十四以不同翻转方式显示图像
原理Bitmap 类的
RotateFip()方法
代码
private void button_Click(object sender EventArgs e) { //以不同翻转方式显示图像 Graphics g = thispanelCreateGraphics(); for (int i = ; i < ; i++) { switch (i) { case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipX);break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipNone); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipX); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipXY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipNone); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipX); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipXY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipNone); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipX); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipXY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateFlipY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipNone); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipX); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipXY); break; case : MyBitmapRotateFlip(RotateFlipTypeRotateNoneFlipY); break; } gClear(ColorWhite); gDrawImage(MyBitmap ); SystemThreadingThreadSleep(); } }
private void button_Click(object sender EventArgs e) { //椭圆显示图像 thispanelRefresh(); Graphics g = thispanelCreateGraphics(); TextureBrush MyBrush = new TextureBrush(MyBitmap); gFillEllipse(MyBrush thispanelClientRectangle); }