• C#图像的拉伸与反转变换


     

    (2013-01-12 15:25:18)
    标签:

    c

    图像

    变换

    拉伸

    反转

    it

    分类: C#.NET
    例子:
    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();
            }
        }
    }
     
    界面:

    C#图像的拉伸与反转变换

  • 相关阅读:
    ndarray转PIL
    215. 数组中的第K个最大元素
    pytorch的分布式
    剑指 Offer 06. 从尾到头打印链表
    最长公共子序列
    剑指 Offer 57
    剑指 Offer 59
    剑指 Offer 48. 最长不含重复字符的子字符串
    Python线程池
    Python 常用内置函数
  • 原文地址:https://www.cnblogs.com/wahaccp/p/3225238.html
Copyright © 2020-2023  润新知