formstretchandreversal.cs

来自「csharp课本的源代码」· CS 代码 · 共 115 行

CS
115
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StretchAndReversal
{
    public partial class FormStretchAndReversal : Form
    {
        public FormStretchAndReversal()
        {
            InitializeComponent();
        }

        Bitmap myBitmap ;
    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)
        {//左到右拉伸
        //图像宽度
        int iWidth = this.pictureBox1.Width;
        //图像高度
        int iHeight = this.pictureBox1.Height;
        //取得Graphics对象
        Graphics g = this.pictureBox1.CreateGraphics();
        //初始为灰色
        g.Clear(Color.Gray);
        for (int x = 0; x <= iWidth; x++)
            {
            g.DrawImage(myBitmap, 0, 0, x, iHeight);
            }
        }
    private void buttonUptoDown_Click(object sender, EventArgs e)
        {//上到下拉伸
         //图像宽度
        int iWidth = this.pictureBox1.Width;
        //图像高度
        int iHeight = this.pictureBox1.Height;
        //取得Graphics对象
        Graphics g = this.pictureBox1.CreateGraphics();
        //初始为灰色
        g.Clear(Color.Gray);
        for (int y = 0; y <= iHeight; y++)
            {
            g.DrawImage(myBitmap, 0, 0, iWidth, y);
            }
        }
    private void buttonMiddletoSide_Click(object sender, EventArgs e)
        {//中间向两边拉伸
         //图像宽度
        int iWidth = this.pictureBox1.Width;
        //图像高度
        int iHeight = this.pictureBox1.Height;
        //取得Graphics对象
        Graphics g = this.pictureBox1.CreateGraphics();
        //初始为灰色
        g.Clear(Color.Gray);
        for (int y = 0; y <= iWidth / 2; y++)
            {
            Rectangle DestRect = new Rectangle(iWidth / 2 - y, 0, 2 * y, iHeight);
            Rectangle SrcRect = new Rectangle(0, 0, myBitmap.Width, myBitmap.Height);
            g.DrawImage(myBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
            }
        }
    private void buttonReversal_Click(object sender, EventArgs e)
        {//反转
         //图像宽度
        int iWidth = this.pictureBox1.Width;
        //图像高度
        int iHeight = this.pictureBox1.Height;
        //取得Graphics对象
        Graphics g = this.pictureBox1.CreateGraphics();
        //初始为灰色
        g.Clear(Color.Gray);
        for (int x = -iWidth / 2; x <= iWidth / 2; x++)
            {
            Rectangle DestRect = new Rectangle(0, iHeight / 2 - x, iWidth, 2 * x);
            Rectangle SrcRect = new Rectangle(0, 0, myBitmap.Width, myBitmap.Height);
            g.DrawImage(myBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
            }
        }
    private void buttonExpand_Click(object sender, EventArgs e)
        {//中间向四周扩散
         //图像宽度
        int iWidth = this.pictureBox1.Width;
        //图像高度
        int iHeight = this.pictureBox1.Height;
        //取得Graphics对象
        Graphics g = this.pictureBox1.CreateGraphics();
        //初始为灰色
        g.Clear(Color.Gray);
        for (int x = 0; x <= iWidth / 2; x++)
            {
            Rectangle DestRect = new Rectangle(iWidth / 2 - x, iHeight / 2 - x, 2 * x, 2 * x);
            Rectangle SrcRect = new Rectangle(0, 0, myBitmap.Width, myBitmap.Height);
            g.DrawImage(myBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
            }
        }
   
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?