C#制作圖像旋轉(zhuǎn)的程序范例

2010-08-28 10:49:44來(lái)源:西部e網(wǎng)作者:

private void RotateTransform_Click(object sender, System.EventArgs e)
        
{
            Graphics graphics
=this.CreateGraphics();
            graphics.Clear(Color.White);

            
//裝入圖片
            Bitmap image=new Bitmap("nemo.bmp");

            
//獲取當(dāng)前窗口的中心點(diǎn)
            Rectangle rect=new Rectangle(0,0,this.ClientSize.Width,this.ClientSize.Height);
            PointF center
=new PointF(rect.Width/2,rect.Height/2);

            
float offsetX=0;
            
float offsetY=0;
            offsetX
=center.X-image.Width/2;
            offsetY
=center.Y-image.Height/2;
            
//構(gòu)造圖片顯示區(qū)域:讓圖片的中心點(diǎn)與窗口的中心點(diǎn)一致
            RectangleF picRect=new RectangleF(offsetX,offsetY,image.Width,image.Height);
            PointF Pcenter
=new PointF(picRect.X+picRect.Width/2,
                picRect.Y
+picRect.Height/2);
    
            
//讓圖片繞中心旋轉(zhuǎn)一周
            for(int i=0;i<361;i+=10)
            
{
                
// 繪圖平面以圖片的中心點(diǎn)旋轉(zhuǎn)
                graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
                graphics.RotateTransform(i);
                
//恢復(fù)繪圖平面在水平和垂直方向的平移
                graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
                
//繪制圖片并延時(shí)
                graphics.DrawImage(image,picRect);    
                Thread.Sleep(
100);
                
//重置繪圖平面的所有變換
                graphics.ResetTransform();
            }

        }

關(guān)鍵詞:C#

贊助商鏈接: