现在的位置: 首页 > 综合 > 正文

c# pictureBox 360度旋转

2013年04月22日 ⁄ 综合 ⁄ 共 798字 ⁄ 字号 评论关闭

public static Bitmap RotateImage(Image image, float angle)
{
if (image == null)
throw new ArgumentNullException("image");
float dx = image.Width / 2.0f;
float dy = image.Height / 2.0f;

Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics g = Graphics.FromImage(rotatedBmp);
g.TranslateTransform(dx, dy);
g.RotateTransform(angle);
g.TranslateTransform(-dx, -dy);
g.DrawImage(image, new PointF(0, 0));
return rotatedBmp;
}

 

 

void tm_Elapsed(object sender, EventArgs e)
{
if (isStart)
{
angle += 5;
if (angle >= 359) angle = 0;
RotateImage(pic1, Properties.Resources._1, angle);
}
}

private void RotateImage(PictureBox pb, Image img, float angle)
{
if (img == null || pb.Image == null)
return;
Image oldImage = pb.Image;
pb.Image = Utilities.RotateImage(img, angle);
if (oldImage != null)
{
oldImage.Dispose();
}
}

抱歉!评论已关闭.