c# - 旋转时图像消失

时间:2013-12-05 21:03:31

标签: c# winforms

我正在尝试旋转图像,但每次转动后图像质量都会下降,最终图像会消失。如何解决这个问题?谢谢你的回答。

代码:

public static Bitmap RotateImageByAngle( Image oldBitmap, float angle )
{
    const PixelFormat pf = PixelFormat.Format32bppArgb;
    var newBitmap = new Bitmap( oldBitmap.Width, oldBitmap.Height, pf );
    newBitmap.SetResolution( oldBitmap.HorizontalResolution, oldBitmap.VerticalResolution );
    var gfx = Graphics.FromImage( newBitmap );
    gfx.TranslateTransform( (float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2 );
    gfx.RotateTransform( angle );
    gfx.TranslateTransform( -(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2 );
    gfx.SmoothingMode = SmoothingMode.HighQuality;
    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
    gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
    gfx.DrawImage( oldBitmap, new Rectangle( new Point( 0, 0 ), new Size( oldBitmap.Width, oldBitmap.Height ) ) );
    gfx.Dispose();
    return newBitmap;
}
...

private void timer1_Tick( object sender, EventArgs e )
{
    this.pictureBox1.Image = RotateImageByAngle( this.pictureBox1.Image, 10 );
}

结果: enter image description here

0 个答案:

没有答案