测试环境: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,
}
}