例子:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace StretchAndReversalWindows
{
public partial class Form1 : Form
{
Bitmap myBitmap;
int width, height;
Graphics g;
public Form1()
{
InitializeComponent();
width = this.pictureBox1.Width;
height = this.pictureBox1.Height;
g = this.pictureBox1.CreateGraphics();
}
//选择图像
private void buttonSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "*.jpg;*.bmp|*.jpg;*.bmp;";
if (openFile.ShowDialog() == DialogResult.OK)
{
Bitmap srcBitmap = new Bitmap(openFile.FileName);
myBitmap = new Bitmap(srcBitmap, this.pictureBox1.Width, this.pictureBox1.Height);
this.pictureBox1.Image = myBitmap;
}
}
//左到右拉伸
private void buttonLeftToRight_Click(object sender, EventArgs e)
{
g = this.pictureBox1.CreateGraphics();
g.Clear(this.BackColor);
for (int x = 0; x <= width; x++)
{
g.DrawImage(myBitmap, 0, 0, x, height);
}
g.Dispose();
}
//上到下拉伸
private void buttonUpToDown_Click(object sender, EventArgs e)
{
g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
for (int y = 0; y <= height; y++)
{
g.DrawImage(myBitmap, 0, 0, width, y);
}
g.Dispose();
}
//中间向两边拉伸
private void buttonMiddleToSide_Click(object sender, EventArgs e)
{
g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
for (int y = 0; y < width / 2; y++)
{
Rectangle DestRect = new Rectangle(width / 2 - y, 0, 2 * y, height);
Rectangle SrcRect = new Rectangle(0, 0, myBitmap.Width, myBitmap.Height);
g.DrawImage(myBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
}
g.Dispose();
}
//反转
private void buttonReversal_Click(object sender, EventArgs e)
{
g = this.pictureBox1.CreateGraphics();
g.Clear(this.BackColor);
for (int x = -width / 2; x <= width; x++)
{
Rectangle DestRect = new Rectangle(0 ,height / 2 - x, width, 2 * x);
Rectangle SrcRect = new Rectangle(0, 0, myBitmap.Width, myBitmap.Height);
g.DrawImage(myBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
}
g.Dispose();
}
//中间向四周扩散
private void buttonExpand_Click(object sender, EventArgs e)
{
g = this.pictureBox1.CreateGraphics();
g.Clear(this.BackColor);
for(int x = 0; x <= width; x++)
{
Rectangle DestRect = new Rectangle(width / 2 - x, height / 2 - x, 2 * x, 2 * x);
Rectangle SrcRect = new Rectangle(0, 0, myBitmap.Width, myBitmap.Height);
g.DrawImage(myBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
}
g.Dispose();
}
}
}
界面: