c# winform任意角度圖片旋轉

 

public Image image;
        public Graphics g;
        private void button1_Click(object sender, EventArgs e) //執行旋轉
        {
            g = this.CreateGraphics();
            g.Clear(this.BackColor);
            image = pictureBox1.Image;

            Rectangle r=new Rectangle (1,1,this .ClientSize .Width ,this .ClientSize .Height );
            PointF center=new PointF (r.Width /2,r.Height /2);
            float offsetX = center.X - image.Width / 2;
            float offsetY=center .Y-image .Height /2;

            RectangleF picRect=new RectangleF (offsetX ,offsetY ,image .Width ,image .Height );
            PointF Pcenter=new PointF (picRect .X +picRect .Width /2,picRect .Y +picRect .Height /2);
            g.TranslateTransform (Pcenter .X ,Pcenter .Y );     //變換繪圖平面
            g.RotateTransform (Convert .ToSingle (textBox1 .Text )); //執行旋轉
            g.TranslateTransform (-Pcenter .X ,-Pcenter .Y );     //恢復繪圖平面
            g.DrawImage (image ,picRect );
            g.ResetTransform();
         
        }


        private void button2_Click_1(object sender, EventArgs e)   //打開圖片
        {
            openFileDialog1.ShowDialog();
            pictureBox1.Image = Image.FromFile(openFileDialog1.FileName );
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章