2013-06-25 144 views
0

我想这对那些更频繁地使用GDI的人来说很简单。我有一张我想旋转的图像。图像旋转与白色背景

样品图片:

before rotation

我申请我的旋转后,它看起来像如下:

enter image description here

这里使用的代码:

public Bitmap RotateBitmap(Bitmap bitmap, float angle) 
{ 
    using (Graphics graphics = Graphics.FromImage(bitmap)) 
    { 
     graphics.TranslateTransform((float)bitmap.Width/2, (float)bitmap.Height/2); 
     graphics.RotateTransform(angle); 
     graphics.TranslateTransform(-(float)bitmap.Width/2, -(float)bitmap.Height/2); 
     graphics.DrawImage(bitmap, new Point(0, 0)); 
    } 
    return bitmap; 
} 

什么我想摆脱o f是旋转之前保留的图像部分。在致电DrawImage之前,任何尝试使用DrawRectangleClear都会给我白色图像,这会产生某种意义。

回答

1

尝试以下操作:

public Bitmap RotateBitmap(Image bitmap, float angle) { 
    Bitmap result = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat); 
    using(Graphics graphics = Graphics.FromImage(result)) { 
     graphics.TranslateTransform((float)bitmap.Width/2f, (float)bitmap.Height/2f); 
     graphics.RotateTransform(angle); 
     graphics.TranslateTransform(-(float)bitmap.Width/2f, -(float)bitmap.Height/2f); 
     graphics.DrawImage(bitmap, Point.Empty); 
    } 
    return result; 
} 
+0

我已经试过了,'result'仍然包含未旋转图像。 –

+0

@SebastianEdelmeier请再次检查我的代码 - 此代码将旋转的图像绘制成新的位图。它和我预期的一样。 – DmitryG

+0

对不起,我的错!你完全正确 - 它按预期工作! –