C#-Bitmap-Image图片翻转和旋转

测试环境:WIN10 VS2013


在项目中用到了图片的翻转,使用了Image类的RotateFlip方法,特将相关资料整理记录。

Image.RotateFlip方法参考官方https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.image.rotateflip?view=netframework-4.8#System_Drawing_Image_RotateFlip_System_Drawing_RotateFlipType_

RotateFlipType Enum枚举参考官方https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.rotatefliptype?redirectedfrom=MSDN&view=netframework-4.8

上述只看文字叙述不直观,特将上述代码做了一个Demo可以更直观的观察旋转和翻转效果。Demo核心代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace BitmapFlipDemo
{
    public partial class frmBitmapFlipMirror : Form
    {
        private Bitmap _origImg;

        public frmBitmapFlipMirror()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                string imgPath = "testpic.png";
                //OpenFileDialog ofd = new OpenFileDialog();
                //DialogResult dr = ofd.ShowDialog();
                //if (dr == DialogResult.OK)
                //{
                //    imgPath = ofd.FileName;
                //}
                _origImg = (Bitmap)Image.FromFile(imgPath).Clone();
                pictureBox1.Image = _origImg;
            }
            catch { }            
        }

        private void btnTransform_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap bmp;
                this.BitmapFlip(_origImg, comboBox1.SelectedIndex, out bmp);
                this.pictureBox2.Image = bmp;
                lblIndex.Text = "当前序号:" + comboBox1.SelectedIndex.ToString();
            }
            catch { }
                    
        }

        /// <summary>
        /// 图像翻转,基于图像中心
        /// </summary>
        private void BitmapFlip(Bitmap btSource,int iFlip,out Bitmap btDes)
        {
            btDes = (Bitmap)_origImg.Clone();
            switch(iFlip)
            {
                case 0:
                    btDes.RotateFlip(RotateFlipType.Rotate180FlipNone); //指定后接水平翻转和垂直翻转的180度顺时针旋转。(不进行顺时针旋转和翻转)
                    break;
                case 1:
                    btDes.RotateFlip(RotateFlipType.Rotate270FlipXY);  //指定不进行翻转的 90 度旋转
                    break;
                case 2:
                    btDes.RotateFlip(RotateFlipType.Rotate180FlipNone);  //指定不进行翻转的 180 度旋转  (垂直翻转+水平翻转)
                    break;
                case 3:
                    btDes.RotateFlip(RotateFlipType.Rotate270FlipNone);  //指定不进行翻转的 270 度旋转
                    break;
                case 4:
                    btDes.RotateFlip(RotateFlipType.Rotate180FlipY);  //指定水平翻转不旋转  (水平翻转)
                    break;
                case 5:
                    btDes.RotateFlip(RotateFlipType.Rotate90FlipX);  //指定90 度旋转后接水平翻转
                    break;
                case 6:
                    btDes.RotateFlip(RotateFlipType.RotateNoneFlipY);  //指定180 度旋转后接水平翻转  (垂直翻转)
                    break;
                case 7:
                    btDes.RotateFlip(RotateFlipType.Rotate90FlipY);  //指定270 度旋转后接水平翻转
                    break;
            }
            
        }

    }
}

Demo下载地址:

链接:https://pan.baidu.com/s/1Iu03SPwjeSaPNlrLKuxj5A 
提取码:qzoa 


附:RotateFlipType 枚举

namespace System.Drawing
{
    // 摘要: 
    //     指定图像的旋转量和用于翻转图像的轴。
    public enum RotateFlipType
    {
        // 摘要: 
        //     指定后接水平翻转和垂直翻转的 180 度顺时针旋转。
        Rotate180FlipXY = 0,
        //
        // 摘要: 
        //     指定不进行顺时针旋转和翻转。
        RotateNoneFlipNone = 0,
        //
        // 摘要: 
        //     指定后接水平翻转和垂直翻转的 270 度顺时针旋转。
        Rotate270FlipXY = 1,
        //
        // 摘要: 
        //     指定不进行翻转的 90 度顺时针旋转。
        Rotate90FlipNone = 1,
        //
        // 摘要: 
        //     指定不进行翻转的 180 度顺时针旋转。
        Rotate180FlipNone = 2,
        //
        // 摘要: 
        //     指定没有后跟水平翻转和垂直翻转的顺时针旋转。
        RotateNoneFlipXY = 2,
        //
        // 摘要: 
        //     指定不进行翻转的 270 度顺时针旋转。
        Rotate270FlipNone = 3,
        //
        // 摘要: 
        //     指定后接水平翻转和垂直翻转的 90 度顺时针旋转。
        Rotate90FlipXY = 3,
        //
        // 摘要: 
        //     指定后接垂直翻转的 180 度顺时针旋转。
        Rotate180FlipY = 4,
        //
        // 摘要: 
        //     指定没有后跟水平翻转的顺时针旋转。
        RotateNoneFlipX = 4,
        //
        // 摘要: 
        //     指定后接水平翻转的 90 度顺时针旋转。
        Rotate90FlipX = 5,
        //
        // 摘要: 
        //     指定后接垂直翻转的 270 度顺时针旋转。
        Rotate270FlipY = 5,
        //
        // 摘要: 
        //     指定没有后跟垂直翻转的顺时针旋转。
        RotateNoneFlipY = 6,
        //
        // 摘要: 
        //     指定后接水平翻转的 180 度顺时针旋转。
        Rotate180FlipX = 6,
        //
        // 摘要: 
        //     指定后接垂直翻转的 90 度顺时针旋转。
        Rotate90FlipY = 7,
        //
        // 摘要: 
        //     指定后接水平翻转的 270 度顺时针旋转。
        Rotate270FlipX = 7,
    }
}

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值