码迷,mamicode.com
首页 > Windows程序 > 详细

winform图片旋转

时间:2014-11-08 23:22:04      阅读:466      评论:0      收藏:0      [点我收藏+]

标签:winform   io   color   ar   os   sp   for   div   on   

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

02. 
03. 
04.using System;
05. 
06.using System.Drawing;
07.using System.Drawing.Drawing2D;
08.using System.Drawing.Imaging;
09.using System.Text;
10.using System.Windows.Forms;
11. 
12.namespace bitmaprerote
13.{
14.public partial class Form1 : Form
15.{
16.public Form1()
17.{
18.InitializeComponent();
19.}
20.public static Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
21.{
22.int w = bmp.Width + 2;
23.int h = bmp.Height + 2;
24. 
25.PixelFormat pf;
26. 
27.if (bkColor == Color.Transparent)
28.{
29.pf = PixelFormat.Format32bppArgb;
30.}
31.else
32.{
33.pf = bmp.PixelFormat;
34.}
35. 
36.Bitmap tmp = new Bitmap(w, h, pf);
37.Graphics g = Graphics.FromImage(tmp);
38.g.Clear(bkColor);
39.g.DrawImageUnscaled(bmp, 1, 1);
40.g.Dispose();
41. 
42.GraphicsPath path = new GraphicsPath();
43.path.AddRectangle(new RectangleF(0f, 0f, w, h));
44.Matrix mtrx = new Matrix();
45.mtrx.Rotate(angle);
46.RectangleF rct = path.GetBounds(mtrx);
47. 
48.Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
49.g = Graphics.FromImage(dst);
50.g.Clear(bkColor);
51.g.TranslateTransform(-rct.X, -rct.Y);
52.g.RotateTransform(angle);
53.g.InterpolationMode = InterpolationMode.HighQualityBilinear;
54.g.DrawImageUnscaled(tmp, 0, 0);
55.g.Dispose();
56.tmp.Dispose();
57.return dst;
58.}
59.private void button2_Click(object sender, EventArgs e)
60.{
61.Bitmap map = new Bitmap("F:\\hj.jpg");
62.this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
63.map = KiRotate(map, 30, Color.Pink);
64.pictureBox1.Image = map;
65.}
66.}
67.}

winform图片旋转

标签:winform   io   color   ar   os   sp   for   div   on   

原文地址:http://www.cnblogs.com/jliuwork/p/4084166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!