회전/ Format32bppArgb/ GraphicsPath/ AddRectAngle/ Matrix()/ Rotate/ GetBounds
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;
using System.Drawing.Imaging;
namespace Rotation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static Bitmap BitmapRotate(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 button1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(@"C:\Home\img\yui11.png");
int w = bmp.Width;
int h = bmp.Height;
Pen pn = new Pen(Color.Yellow, 1f);
Graphics g = this.CreateGraphics();
g.DrawImageUnscaled(bmp, 5, 5);
Bitmap tmp = BitmapRotate(bmp, 12, this.BackColor);
g.DrawImageUnscaled(tmp, w + 10, 5);
g.DrawRectangle(pn, new Rectangle(w + 10, 5, tmp.Width, tmp.Height));
tmp.Dispose();
tmp = BitmapRotate(bmp, 192, this.BackColor);
g.DrawImageUnscaled(tmp, 5, h + 30);
g.DrawRectangle(pn, new Rectangle(5, h + 30, tmp.Width, tmp.Height));
tmp.Dispose();
tmp = BitmapRotate(bmp, 282, this.BackColor);
g.DrawImageUnscaled(tmp, h + 30, h + 50);
g.DrawRectangle(pn, new Rectangle(h + 30, h + 50, tmp.Width, tmp.Height));
tmp.Dispose();
g.Dispose();
pn.Dispose();
bmp.Dispose();
}
}
}
댓글
댓글 쓰기