首页 新闻 赞助 找找看

C# 如何实现画布的旋转

0
悬赏园豆:50 [已解决问题] 解决于 2013-05-30 16:35

我想在右键点击pictrueBox时旋转画布90度,请问如何实现?

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.IO;
 10 
 11 namespace MonthlyReimbursementTable
 12 {
 13     public partial class PicSee : Form
 14     {
 15         public PicSee()
 16         {
 17             InitializeComponent();
 18             this.StartPosition = FormStartPosition.CenterScreen;
 19             this.PicLook.BorderStyle = BorderStyle.FixedSingle;
 20             this.PicLook.BackColor = Color.DarkGray;
 21             this.PicLook.MouseWheel += new MouseEventHandler(PicLook_MouseWheel);
 22         }
 23         Bitmap m_bmp;               //画布中的图像
 24         Point m_ptCanvas;           //画布原点在设备上的坐标
 25         Point m_ptCanvasBuf;        //重置画布坐标计算时用的临时变量
 26         Point m_ptBmp;              //图像位于画布坐标系中的坐标
 27         float m_nScale = 1.0F;      //缩放比例
 28 
 29         Point m_ptMouseDown;        //鼠标点下是在设备坐标上的坐标
 30 
 31         string m_strMousePt;        //鼠标当前位置对应的坐标
 32         private void PicSee_Load(object sender, EventArgs e)
 33         {
 34             Form1 f = (Form1)this.Owner;
 35             int id = f.SentData();
 36             MonthlyReimbursementTableEntities mr = new MonthlyReimbursementTableEntities();
 37             var aa = mr.MonthTable.Where(d => d.ID == id).FirstOrDefault();
 38             Stream str1 = new MemoryStream(aa.Pictrue );
 39             Image img1 = Image.FromStream(str1);
 40             
 41             toolTip1.ShowAlways = true;
 42             string Str1 = "双击可关闭";
 43             toolTip1.SetToolTip(PicLook , Str1);
 44             m_bmp = (Bitmap)img1;
 45             m_ptCanvas = new Point(PicLook.Width / 2, PicLook.Height / 2);
 46             m_ptBmp = new Point(-(m_bmp.Width / 2), -(m_bmp.Height / 2));
 47             
 48         }
 49 
 50        
 51 
 52         private void PicLook_DoubleClick(object sender, EventArgs e)
 53         {
 54             this.Close();
 55         }
 56 
 57         private void PicLook_Paint(object sender, PaintEventArgs e)
 58         {
 59             Graphics g = e.Graphics;
 60             g.TranslateTransform(m_ptCanvas.X, m_ptCanvas.Y);       //设置坐标偏移
 61             g.ScaleTransform(m_nScale, m_nScale);                   //设置缩放比
 62             g.DrawImage(m_bmp, m_ptBmp);                            //绘制图像
 63 
 64 
 65             //计算屏幕左上角 和 右下角 对应画布上的坐标
 66             Size szTemp = PicLook.Size - (Size)m_ptCanvas;
 67             PointF ptCanvasOnShowRectLT = new PointF(
 68                 -m_ptCanvas.X / m_nScale, -m_ptCanvas.Y / m_nScale);
 69             PointF ptCanvasOnShowRectRB = new PointF(
 70                 szTemp.Width / m_nScale, szTemp.Height / m_nScale);
 71         }
 72 
 73         private void PicLook_MouseDown(object sender, MouseEventArgs e)
 74         {
 75             if (e.Button == MouseButtons.Left)
 76             {      //如果中键点下    初始化计算要用的临时数据
 77                 m_ptMouseDown = e.Location;
 78                 m_ptCanvasBuf = m_ptCanvas;
 79             }
 80             PicLook.Focus();
 81         }
 82 
 83         private void PicLook_MouseMove(object sender, MouseEventArgs e)
 84         {
 85             if (e.Button == MouseButtons.Left)
 86             {      //移动过程中 中键点下 重置画布坐标系
 87                 //我总感觉这样写不妥 但却是方便计算  如果多次这样搞的话 还是重载操作符吧
 88                 m_ptCanvas = (Point)((Size)m_ptCanvasBuf + ((Size)e.Location - (Size)m_ptMouseDown));
 89                 PicLook.Invalidate();
 90             }
 91             //计算 右上角显示的坐标信息
 92             //计算鼠标当前点对应画布中的坐标
 93             SizeF szSub = (Size)e.Location - (Size)m_ptCanvas;  
 94             szSub.Width /= m_nScale;
 95             szSub.Height /= m_nScale;
 96             Size sz = TextRenderer.MeasureText(m_strMousePt, this.Font);    //获取上一次的区域并重绘
 97             PicLook.Invalidate(new Rectangle(PicLook.Width - sz.Width, 0, sz.Width, sz.Height));
 98             m_strMousePt = e.Location.ToString() + "\n" + ((Point)(szSub.ToSize())).ToString();
 99             sz = TextRenderer.MeasureText(m_strMousePt, this.Font);         //绘制新的区域
100             PicLook.Invalidate(new Rectangle(PicLook.Width - sz.Width, 0, sz.Width, sz.Height));
101         }
102         private void PicLook_MouseWheel(object sender, MouseEventArgs e)
103         {
104             if (m_nScale <= 0.3 && e.Delta <= 0) return;        //缩小下线
105             if (m_nScale >= 4.9 && e.Delta >= 0) return;        //放大上线
106             //获取 当前点到画布坐标原点的距离
107             SizeF szSub = (Size)m_ptCanvas - (Size)e.Location;
108             //当前的距离差除以缩放比还原到未缩放长度
109             float tempX = szSub.Width / m_nScale;           //这里
110             float tempY = szSub.Height / m_nScale;          //将画布比例
111             //还原上一次的偏移                               //按照当前缩放比还原到
112             m_ptCanvas.X -= (int)(szSub.Width - tempX);     //没有缩放
113             m_ptCanvas.Y -= (int)(szSub.Height - tempY);    //的状态
114             //重置距离差为  未缩放状态                       
115             szSub.Width = tempX;
116             szSub.Height = tempY;
117             m_nScale += e.Delta > 0 ? 0.2F : -0.2F;
118             //重新计算 缩放并 重置画布原点坐标
119             m_ptCanvas.X += (int)(szSub.Width * m_nScale - szSub.Width);
120             m_ptCanvas.Y += (int)(szSub.Height * m_nScale - szSub.Height);
121             PicLook.Invalidate();
122         }
123 
124         
125     }
126 }
溪山行旅丶的主页 溪山行旅丶 | 初学一级 | 园豆:6
提问于:2013-05-30 14:58
< >
分享
最佳答案
0

给picturebox加入mouseup事件

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Image img = pictureBox1.Image;
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                pictureBox1.Image = img;
            }
        }
收获园豆:50
WuRang | 小虾三级 |园豆:1730 | 2013-05-30 15:14
pictureBox1上是没有图片的
 Image img = pictureBox1.Image;何解?
我要把画布整个旋转90度
有别的办法吗
 
溪山行旅丶 | 园豆:6 (初学一级) | 2013-05-30 15:22

@溪山行旅丶: 明确几个概念先,所谓画布指什么?Bitmap m_bmp?然后pictrueBox是用来做什么的?显示m_bmp? 或者你可以直接使用

//设置旋转中心

g.TranslateTransform(center.X, center.Y);
//旋转
g.RotateTransform(360 - angle);
//angle是角度
WuRang | 园豆:1730 (小虾三级) | 2013-05-30 15:28

@WuRang: 谢谢!我搞好了

溪山行旅丶 | 园豆:6 (初学一级) | 2013-05-30 16:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册