日期:2014-05-17  浏览次数:21255 次

c#图片在PictureBox内任意角度旋转问题
问题:不能以我的窗体中心旋转 旋转之后图片出了窗体,必须拉滚动条才能显示(PS:我图片较大,必须要)、
窗体上控件:panel1里有个PictureBox1
附上我得关键代码:
C# code
        //任意角度旋转
        private void RotateTransformButton_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap a = new Bitmap(pictureBox1.Image);//得到图片框中的图片
                pictureBox1.Image = Rotate(a, Convert.ToInt32(textBox1.Text));
                pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
                pictureBox1.Location = panel1.Location;
                pictureBox1.Refresh();//最后刷新图片框
            }
            catch { }
        }

        #region 图片旋转函数
        /// <summary>
        /// 以逆时针为方向对图像进行旋转
        /// </summary>
        /// <param name="b">位图流</param>
        /// <param name="angle">旋转角度[0,360](前台给的)</param>
        /// <returns></returns>
        public Bitmap Rotate(Bitmap b, int angle)
        {
            angle = angle % 360;

            //弧度转换
            double radian = angle * Math.PI / 180.0;
            double cos = Math.Cos(radian);
            double sin = Math.Sin(radian);

            //原图的宽和高
            int w = b.Width;
            int h = b.Height;
            int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
            int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));

            //目标位图
            Bitmap dsImage = new Bitmap(W, H);
            Graphics g = Graphics.FromImage(dsImage);            

            g.InterpolationMode = InterpolationMode.Bilinear;

            g.SmoothingMode = SmoothingMode.HighQuality;

            //计算偏移量
            Point Offset = new Point((W - w) / 2, (H - h) / 2);

            //构造图像显示区域:让图像的中心与窗口的中心点一致
            Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
            Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
            g.TranslateTransform(center.X, center.Y);
            g.RotateTransform(360 - angle);

            //恢复图像在水平和垂直方向的平移
            g.TranslateTransform(-center.X, -center.Y);
            g.DrawImage(b, rect);

            //重至绘图的所有变换
            g.ResetTransform();

            g.Save();
            g.Dispose();
            return dsImage;
        }
        #endregion 图片旋转函数


------解决方案--------------------
C# code

        Point[] ps;
        public Form1()
        {
            InitializeComponent();
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            int x1 = (pictureBox1.Width - pictureBox1.Image.Width) / 2;
            int x2 = (pictureBox1.Width + pictureBox1.Image.Width) / 2;
            int y1 = (pictureBox1.Height - pictureBox1.Image.Height) / 2;
            int y2 = (pictureBox1.Height + pictureBox1.Image.Height) / 2;
            ps = new Point[] { new Point(x1, y1), new Point(x2, y1), new Point(x1, y2) };
        }

        private void button1_Click(object sender, EventArgs e)
        {
            rotate(Math.PI / 90, ps);
        }
        void rotate(double angle , Point[] ps)
        {
            Graphics g = pictureBox1.CreateGraphics();
            float cos = (float)Math.Cos(angle);
            float sin = (float)Math.Sin(angle);
            float centerx = pictureBox1.Width / 2;
            float centery = pictureBox1.Height / 2;
            Matrix m = new Matrix(cos, sin, -sin, cos, centerx + centery * sin - centerx * cos, centery - centery * cos - centerx * sin);
            m.TransformPoints(ps);
            g.Clear(pictureBox1.BackColor);
            g.DrawImage(pictureBox1.Image, ps);
            g.Dispose();
        }