(转)C# 制作以动画的方式显示图像(二)
七. 以左右反转的方式显示图像

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 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 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, "信息提示");
}
}

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 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 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);
}
}

private void button1_Click(object sender, EventArgs e)
{
//椭圆显示图像
this.panel1.Refresh();
Graphics g = this.panel1.CreateGraphics();
TextureBrush MyBrush = new TextureBrush(MyBitmap);
g.FillEllipse(MyBrush, this.panel1.ClientRectangle);
}

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, his.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 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);
}
}

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);
}
}
原理: 计算图像位置和高度后以宽度的一半为轴进行对换左右半边的图像."
代码:
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 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 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, "信息提示");
}
}
九. 以从左向右拉伸的方式显示图像
原理: 将图像的高度不变每次显示图像的一部分, 直到将图片完全显示
代码:
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 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, "信息提示");
}
}
十. 以任意角度旋转图像
原理: 主要使用了 Graphics 类提供的 RotateTransform() 方法对图像进行旋转.
代码:
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);
}
}
十一. 以椭圆的方式显示图像
原理: 主要使用了 Graphics 类提供的 FillEllipse() 方法和 TextureBrush() 方法.
代码:
private void button1_Click(object sender, EventArgs e)
{
//椭圆显示图像
this.panel1.Refresh();
Graphics g = this.panel1.CreateGraphics();
TextureBrush MyBrush = new TextureBrush(MyBitmap);
g.FillEllipse(MyBrush, this.panel1.ClientRectangle);
}
十二. 以不同的透明度显示图像.
原理: Graphics 类的 FromArgb() 方法
代码:
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, his.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);
}
}
十三. 以不同分辨率显示图像
原理: Bitmap 类的 SetResolution 方法
代码:
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);
}
}
十四. 以不同翻转方式显示图像.
原理: Bitmap 类的 RotateFip()方法
代码:
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);
}
}

浙公网安备 33010602011771号