实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace ImageBitmap
{
public partial class Form1 : Form
{
private Bitmap SourceBitmap;
private Bitmap MyBitmap;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.panel2.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
//打开图像文件
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图像文件(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 (openFileDialog.ShowDialog() == DialogResult.OK)
{
//得到原始大小的图像
SourceBitmap = new Bitmap(openFileDialog.FileName);
//得到缩放后的图像
MyBitmap = new Bitmap(SourceBitmap, this.pictureBox1.Width, this.pictureBox1.Height);
this.pictureBox1.Image = MyBitmap;
}
this.panel2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以上下反转的方式显示图像";
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, "信息提示");
}
}
private void button3_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以上下对接的方式显示图像 ";
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, "信息提示");
}
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以四周扩散的方式显示图像";
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, "信息提示");
}
}
private void button5_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以分块效果显示图像";
Graphics g = this.panel1.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(1000);
g.DrawImage(MyBitmapBlack[1], width / 2, 0);
System.Threading.Thread.Sleep(1000);
g.DrawImage(MyBitmapBlack[3], width / 2, height / 2);
System.Threading.Thread.Sleep(1000);
g.DrawImage(MyBitmapBlack[2], 0, height / 2);
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以淡入淡出效果显示图像";
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 button7_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以左右对接的方式显示图像";
//以左右对接方式显示图像
try
{
int width = this.MyBitmap.Width; //图像宽度
int height = this.MyBitmap.Height; //图像高度
Graphics g = this.panel1.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 ;
this.panel1.Refresh();
g.DrawImage(bitmap, 0, 0);
System.Threading.Thread.Sleep(10);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}
private void button8_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以左右反转方式显示图像";
//以左右反转方式显示图像
try
{
int width = this.MyBitmap.Width; //图像宽度
int height = this.MyBitmap.Height; //图像高度
Graphics g = this.panel1.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, "信息提示");
}
}
private void button9_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以从上向下拉伸方式显示图像";
//以从上向下拉伸方式显示图像
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, "信息提示");
}
}
private void button10_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以从左向右拉伸方式显示图像";
//以从左向右拉伸方式显示图像
try
{
int width = this.MyBitmap.Width; //图像宽度
int height = this.MyBitmap.Height; //图像高度
Graphics g = this.panel1.CreateGraphics(); g.Clear(Color.Gray); //初始为全灰色
for (int x = 1; x <= width; x )
{
Bitmap bitmap = MyBitmap.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, "信息提示");
}
}
private void button11_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以任意角度旋转显示图像";
//以任意角度旋转显示图像
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);
}
}
private void button12_Click(object sender, EventArgs e)
{
this.textBox1.Text = "椭圆显示图像";
//椭圆显示图像
this.panel1.Refresh();
Graphics g = this.panel1.CreateGraphics();
TextureBrush MyBrush = new TextureBrush(MyBitmap);
g.FillEllipse(MyBrush, this.panel1.ClientRectangle);
}
private void button13_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以不同的透明度显示图像";
//以不同的透明度显示图像
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);
}
}
private void button14_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以不同的分辨率显示图像";
//以不同的分辨率显示图像
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);
}
}
private void button15_Click(object sender, EventArgs e)
{
this.textBox1.Text = "以不同翻转方式显示图像";
//以不同翻转方式显示图像
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);
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论