柒步的专栏

柒步成师!

C# winform窗口图片旋转

//可以实现任意角度的旋转,旋转出现的角落空白可以选择填充的颜色。

using System;

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

namespace bitmaprerote
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
        {
            int w = bmp.Width + 2;
            int h = bmp.Height + 2;

            PixelFormat pf;

            if (bkColor == Color.Transparent)
            {
                pf = PixelFormat.Format32bppArgb;
            }
            else
            {
                pf = bmp.PixelFormat;
            }

            Bitmap tmp = new Bitmap(w, h, pf);
            Graphics g = Graphics.FromImage(tmp);
            g.Clear(bkColor);
            g.DrawImageUnscaled(bmp, 1, 1);
            g.Dispose();

            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(new RectangleF(0f, 0f, w, h));
            Matrix mtrx = new Matrix();
            mtrx.Rotate(angle);
            RectangleF rct = path.GetBounds(mtrx);

            Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
            g = Graphics.FromImage(dst);
            g.Clear(bkColor);
            g.TranslateTransform(-rct.X, -rct.Y);
            g.RotateTransform(angle);
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.DrawImageUnscaled(tmp, 0, 0);
            g.Dispose();
            tmp.Dispose();
            return dst;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap map = new Bitmap("F:\\hj.jpg");
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            map = KiRotate(map, 30, Color.Pink);
            pictureBox1.Image = map;
        }
    }
}
阅读更多
文章标签: C# winform 图片 旋转
个人分类: 功能开发
想对作者说点什么? 我来说一句

C#图片在PictureBox中进行旋转

2012年07月26日 37KB 下载

C#WinForm窗口变化特效15种

2014年11月03日 880KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭