方法1:

  1. private void RotateFormCenter(PictureBox pb, float angle)
  2. {
  3. Image img = pb.Image;
  4. int newWidth = Math.Max(img.Height, img.Width);
  5. Bitmap bmp = new Bitmap(newWidth, newWidth);
  6. Graphics g = Graphics.FromImage(bmp);
  7. Matrix x = new Matrix();
  8. PointF point = new PointF(img.Width / 2f, img.Height / 2f);
  9. x.RotateAt(angle, point);
  10. g.Transform = x;
  11. g.DrawImage(img, 0, 0);
  12. g.Dispose();
  13. img = bmp;
  14. pb.Image = img;
  15. }

  该方法通过将图片转化为用于几何变换的3x3矩阵 对图片进行旋转。

  缺点:有时图片会越转越模糊。

方法2:

  1. private void RotateFormCenter(PictureBox pb, float angle)
  2. {
  3. Graphics graphics = pb.CreateGraphics();
  4. graphics.Clear(pb.BackColor);
  5. //装入图片
  6. Bitmap image = new Bitmap(pb.Image);
  7. //获取当前窗口的中心点
  8. Rectangle rect = new Rectangle(, , pb.Width, pb.Height);
  9. PointF center = new PointF(rect.Width / , rect.Height / );
  10. float offsetX = ;
  11. float offsetY = ;
  12. offsetX = center.X - image.Width / ;
  13. offsetY = center.Y - image.Height / ;
  14. //构造图片显示区域:让图片的中心点与窗口的中心点一致
  15. RectangleF picRect = new RectangleF(offsetX, offsetY, image.Width, image.Height);
  16. PointF Pcenter = new PointF(picRect.X + picRect.Width / ,
  17. picRect.Y + picRect.Height / );
  18. // 绘图平面以图片的中心点旋转
  19. graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
  20. graphics.RotateTransform(angle);
  21. //恢复绘图平面在水平和垂直方向的平移
  22. graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
  23. //绘制图片
  24. graphics.DrawImage(image, picRect);
  25. }

通过操作Graphics进行图像旋转,使用时需要注意图片是按原始大小进行居中旋转  PictureBox的SizeMode属性对这种方法无效。

Winform以任意角度旋转PictureBox中的图片的方法的更多相关文章

  1. JAVA对图片的任意角度旋转,以及镜像操作

    package relevantTest;/* * 该代码实现了对图像的水平镜像变换,垂直镜像变换,任意角度旋转,jtf的实时监控,以及对图像的缩放变换,以及按钮的若隐若现效果. * 在对图像进行任意 ...

  2. C# 保存PictureBox中的图片到数据库,并从数据库读取图片显示到PictrueBox,解决报错 “无效参数”

    下面是两段关键代码: /// <summary> /// 将一张图片转换为字节 /// </summary> /// <param name="img" ...

  3. 在Altium_Designer_PCB_中插入图片的方法

    详细请看PDF: http://files.cnblogs.com/files/BinB-W/在Altium_Designer_PCB_中插入图片的方法.pdf 配套文件: http://files. ...

  4. 在word中输入任意角度旋转图片

    Sub 图片旋转任意角度() Dim sha As Shape, isa As InlineShape Static s As Integer Application.ScreenUpdating = ...

  5. Java实现图片内容无损任意角度旋转

    转自:http://blog.csdn.net/heliang7/article/details/7309394 主要问题是如何在图片做旋转后计算出新图片的长宽. 在java 2d和基本math库的帮 ...

  6. CSS中背景图片定位方法

    转自:http://www.ruanyifeng.com/blog/2008/05/css_background_image_positioning.html 作者: 阮一峰 日期: 2008年5月 ...

  7. Java中显示图片的方法

    最近在做一个swing小项目,其中需要把存储在硬盘中的图片文件显示出来,总结了如下方法: 1. Graphics g = getGraphics();String name = "E:/Ca ...

  8. Dede 删除文档同时文章中的图片的方法

    首先,在"/include"目录下建立"extend.func.php"文件. 然后,将以下内容保存在"extend.func.php"文件 ...

  9. (转)HTML&CSS——background: url() no-repeat 0 -64px;CSS中背景图片定位方法

    http://blog.csdn.net/oscar92420aaa/article/details/51304067 CSS中背景图片的定位,困扰我很久了.今天总算搞懂了,一定要记下来. 在CSS中 ...

随机推荐

  1. 如何把apdu[decode_len]打印出来

    memcpy(data, &apdu[decode_len], apdu_len - decode_len);    int i = 0;    for(i=0;i<apdu_len;i ...

  2. deeplab hole algorithm

    最近看了几篇文章,其中均用到了hole algorithm. 最早用的就是deeplab的文章了,Semantic Image Segmentation with Deep Convolutional ...

  3. ASP.NET页面中去除VIEWSTATE视图状态乱码

    保存页的所有视图状态信息和控件状态信息. 基于SEO技术的开发,在没有接触MVC框架 Razor 引擎的时候,我们需要使用ASP.NET引擎,如果使用ASP.NET引擎的服务器端控件,那么在ASP.N ...

  4. git 常见命令

    查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id gi ...

  5. 对Hadoop体系的一点认识

    前言:Hadoop体系核心大多源自Google的思想,里面的思想的确很精彩!比如分布式计算,云的思想等,比起其他简单技术,更使得我想写这文章, 虽然这个东东在一般公司不可能用到! 首先由于hadoop ...

  6. PHP向mysql中插入数据的方法

    require "database.php"; $po_code = "YMWF2015-6-25-1"; $customer = "youmei&q ...

  7. Ceph剖析:定时器safetimer的实现

    定时器的作用是在指定的时间执行指定的动作.SafeTimer通过multimap数据结构维护定时项,定时项是时间和事件的Pair,定时项在map中按照定时时间从小到大排列.此外,SafeTimer使用 ...

  8. Android studio debug模式获取变量的值

    打断点.debug模式运行,Console界面旁边的Debugger界面,或者在变量上右键add to watches

  9. (转)linux下cp目录时排除一个或者多个目录的实现方法

    原文链接:http://www.jb51.net/LINUXjishu/88971.html 说明:/home目录里面有data目录,data目录里面有a.b.c.d.e五个目录,现在要把data目录 ...

  10. 20145316&20145229实验五:网络通信

    20145316&20145229实验五:网络通信 结对伙伴:20145316 博客链接:http://www.cnblogs.com/xxy745214935/p/6130897.html