C# graphicspath 翻转

来源:互联网 发布:江大网络教育主页 编辑:程序博客网 时间:2024/05/01 08:34

C#中GraphicsPath旋转问题(发现网上与此相关的资料很少,我是从一个英文网站上找到的方法,具体原理还不是很明白,写出来大家分享一下)

准备:新建一窗体,添加一个PictureBox和一个btn

功能:点击btn实现图像翻转

备注:我用的是png格式的图

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace pathflip
{
    public partial class Form1 : Form
    {
        private GraphicsPath path;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Image image = this.pictureBox1.Image;
            image.RotateFlip(RotateFlipType.Rotate180FlipY);
            this.pictureBox1.Image = image;
            Matrix matrix = new Matrix(-1, 0, 0, 1, image.Width, 0);
            this.path.Transform(matrix);
            this.pictureBox1.Region = new Region(this.path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap bmp = (Bitmap)this.pictureBox1.Image;
            this.path = this.CalculateGrahicsPath(bmp);
            this.pictureBox1.Region = new Region(this.path);

        }
        private GraphicsPath CalculateGrahicsPath(Bitmap bitmap)//Region设定前,Graphicpaths获取函数
        {
            int iWidth = bitmap.Width;
            int iHeight = bitmap.Height;
            GraphicsPath graphicpath = new GraphicsPath();
            System.Drawing.Color color;
            for (int row = 0; row < iHeight; row++)
                for (int wid = 0; wid < iWidth; wid++)
                {
                    color = bitmap.GetPixel(wid, row);
                    //if (255 == color.A)
                    if (255 == color.A)
                        graphicpath.AddRectangle(new Rectangle(wid, row, 1, 1));

                }

            return graphicpath;
        }
    }
}