图片操作 bitmap 旋转 bitmap左右镜像
public static Bitmap RotateImage ( Bitmap image, int angle)
{
try
{
Bitmap rotatedImage = new Bitmap ( image. Height, image. Width) ;
using ( Graphics g = Graphics. FromImage ( rotatedImage) )
{
g. TranslateTransform ( rotatedImage. Width / 2 , rotatedImage. Height / 2 ) ;
g. RotateTransform ( angle) ;
g. DrawImage ( image, - image. Width / 2 , - image. Height / 2 , image. Width, image. Height) ;
return rotatedImage;
}
}
catch ( AccessViolationException ave)
{
return null ;
}
}
public static Bitmap RotateImage2 ( Bitmap originalImage, float angle)
{
int width = originalImage. Width;
int height = originalImage. Height;
double radians = angle * Math. PI / 180.0 ;
double sin = Math. Abs ( Math. Sin ( radians) ) ;
double cos = Math. Abs ( Math. Cos ( radians) ) ;
int newWidth = ( int ) Math. Floor ( width * cos + height * sin) ;
int newHeight = ( int ) Math. Floor ( width * sin + height * cos) ;
Bitmap rotatedImage = new Bitmap ( newWidth, newHeight) ;
using ( Graphics g = Graphics. FromImage ( rotatedImage) )
{
g. InterpolationMode = InterpolationMode. HighQualityBicubic;
g. SmoothingMode = SmoothingMode. HighQuality;
g. PixelOffsetMode = PixelOffsetMode. HighQuality;
g. CompositingQuality = CompositingQuality. HighQuality;
Point center = new Point ( newWidth / 2 , newHeight / 2 ) ;
g. TranslateTransform ( center. X, center. Y) ;
g. RotateTransform ( angle) ;
g. TranslateTransform ( - center. X, - center. Y) ;
g. DrawImage ( originalImage, new Point ( ( newWidth - width) / 2 , ( newHeight - height) / 2 ) ) ;
}
return rotatedImage;
}
public static Bitmap BitmapImage2 ( Bitmap originalImage)
{
{
Bitmap flippedImage = new Bitmap ( originalImage. Width, originalImage. Height) ;
using ( Graphics g = Graphics. FromImage ( flippedImage) )
{
g. DrawImage ( originalImage, new Rectangle ( 0 , 0 , flippedImage. Width, flippedImage. Height) ,
new Rectangle ( originalImage. Width, 0 , - originalImage. Width, originalImage. Height) ,
GraphicsUnit. Pixel) ;
}
return flippedImage;
}
}
public static Bitmap BitmapImage ( Bitmap originalBitmap)
{
Bitmap mirroredBitmap = new Bitmap ( originalBitmap. Width, originalBitmap. Height) ;
for ( int y = 0 ; y < originalBitmap. Height; y++ )
{
for ( int x = 0 ; x < originalBitmap. Width; x++ )
{
Color originalColor = originalBitmap. GetPixel ( x, y) ;
int mirroredX = originalBitmap. Width - 1 - x;
mirroredBitmap. SetPixel ( mirroredX, y, originalColor) ;
}
}
return mirroredBitmap;
}