六. 以左右對接的方式顯示圖像
原理: 首先將圖像分為左右兩部分, 然後分別顯示.
代碼:
以左右對接方式顯示圖像
Code
[copy to clipboard]
CODE:
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); //初始為全灰色
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, "信息提示");
}
}
七. 以左右反轉的方式顯示圖像
原理: 計算圖像位置和高度後以寬度的一半為軸進行對換左右半邊的圖像.
代碼:
以左右反轉方式顯示圖像
Code
[copy to clipboard]
CODE:
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, "信息提示");
}
}