WPF使用System.Windows.Media.Imaging 旋转图片需要注意的问题
如果非圆形图像,旋转的时候需要计算旋转后的画布尺寸,不然的话会导致旋转后显示的图像不全,最后将将图像绘制在新的中心位置。
// 修改图片大小、旋转角度、透明度 (印章)(预览)
public static BitmapSource ModifyImage(string imagePath, double newWidth, double newHeight, double rotationAngle, double stampTransparency)
{
// 加载图像
BitmapImage bitmap = new BitmapImage(new Uri(imagePath));
// 调整图像尺寸
TransformedBitmap resizedBitmap = new TransformedBitmap(bitmap, new ScaleTransform(newWidth / bitmap.PixelWidth, newHeight / bitmap.PixelHeight));
// 计算旋转后的画布尺寸
double radians = rotationAngle * Math.PI / 180;
double sin = Math.Abs(Math.Sin(radians));
double cos = Math.Abs(Math.Cos(radians));
double rotatedWidth = resizedBitmap.PixelWidth * cos + resizedBitmap.PixelHeight * sin;
double rotatedHeight = resizedBitmap.PixelWidth * sin + resizedBitmap.PixelHeight * cos;
// 创建一个新的 DrawingVisual
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
// 计算新的中心点
double centerX = rotatedWidth / 2;
double centerY = rotatedHeight / 2;
// 创建旋转变换
RotateTransform rotateTransform = new RotateTransform(rotationAngle, centerX, centerY);
// 设置透明度
drawingContext.PushOpacity(stampTransparency);
// 应用旋转并绘制图像
drawingContext.PushTransform(rotateTransform);
// 将图像绘制在新的中心位置
drawingContext.DrawImage(resizedBitmap, new Rect((rotatedWidth - resizedBitmap.PixelWidth) / 2, (rotatedHeight - resizedBitmap.PixelHeight) / 2, resizedBitmap.PixelWidth, resizedBitmap.PixelHeight));
// 弹出变换和透明度设置
drawingContext.Pop();
drawingContext.Pop();
}
// 将Visual渲染为BitmapSource,使用旋转后的画布尺寸
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)Math.Ceiling(rotatedWidth), (int)Math.Ceiling(rotatedHeight), bitmap.DpiX, bitmap.DpiY, PixelFormats.Pbgra32);
renderTargetBitmap.Render(drawingVisual);
return renderTargetBitmap;
}
// 修改图片旋转角度、透明度(印章)(PDF)
public static BitmapSource ModifyImage(string imagePath, double rotationAngle, double stampTransparency)
{
// 加载图像
BitmapImage bitmap = new BitmapImage(new Uri(imagePath));
// 计算旋转后的画布尺寸
double radians = rotationAngle * Math.PI / 180;
double sin = Math.Abs(Math.Sin(radians));
double cos = Math.Abs(Math.Cos(radians));
double newWidth = bitmap.PixelWidth * cos + bitmap.PixelHeight * sin;
double newHeight = bitmap.PixelWidth * sin + bitmap.PixelHeight * cos;
// 创建一个新的 DrawingVisual
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
// 计算新的中心点
double centerX = newWidth / 2;
double centerY = newHeight / 2;
// 创建旋转变换
RotateTransform rotateTransform = new RotateTransform(rotationAngle, centerX, centerY);
// 设置透明度
drawingContext.PushOpacity(stampTransparency);
// 应用旋转并绘制图像
drawingContext.PushTransform(rotateTransform);
// 将图像绘制在新的中心位置
drawingContext.DrawImage(bitmap, new Rect((newWidth - bitmap.PixelWidth) / 2, (newHeight - bitmap.PixelHeight) / 2, bitmap.PixelWidth, bitmap.PixelHeight));
// 弹出变换和透明度设置
drawingContext.Pop();
drawingContext.Pop();
}
// 将Visual渲染为BitmapSource
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)Math.Ceiling(newWidth), (int)Math.Ceiling(newHeight), bitmap.DpiX, bitmap.DpiY, PixelFormats.Pbgra32);
renderTargetBitmap.Render(drawingVisual);
return renderTargetBitmap;
}