【发布时间】:2020-01-31 19:40:42
【问题描述】:
我正在尝试获取图像并对其应用投影并另存为图像。
到目前为止,不使用第三方解决方案的唯一方法是在DrawingVisual 中使用DropShadowEffect:
var drawingVisual = new DrawingVisual();
drawingVisual.Effect = new DropShadowEffect
{
Color = Color.FromArgb(255, 0, 0, 0),
BlurRadius = 5,
Opacity = 1,
Direction = 45,
ShadowDepth = 6
};
using (var drawingContext = drawingVisual.RenderOpen())
{
var left = 0; //??
var top = 0; //??
var totalWidth = left + image.Width; //??
var totalHeight = top + image.Height; //??
//Background.
drawingContext.DrawRectangle(new SolidColorBrush(Colors.White), null, new Rect(0,0, totalWidth, totalHeight));
//Image.
drawingContext.DrawImage(image, new Rect(left, top, image.Width, image.Height));
}
var frameHeight = image.PixelHeight; //??
var frameWidth = image.PixelWidth; //??
//Converts the Visual (DrawingVisual) into a BitmapSource.
var bmp = new RenderTargetBitmap(frameWidth, frameHeight, imageDpi, imageDpi, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
//Creates a PngBitmapEncoder and adds the BitmapSource to the frames of the encoder.
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
//Saves the image into a file using the encoder.
using (Stream stream = File.Create(frame.Path))
encoder.Save(stream);
我不知道为给定的DropShadowEffect 检测所有边的精确像素偏移所需的数学。
有没有内置的方法来测量它还是我应该手动测量? 怎么可以手动完成?
【问题讨论】:
-
怎么样。在窗口中粘贴图像。应用您的效果并调整属性,直到它看起来像您想要的样子。
-
我无法使用 UI,这段代码将在任务中执行。
-
不是我的意思。在实验性的不同应用程序中尝试不同的设置,看看哪种设置效果最好。然后你知道在你那里的代码中使用什么。
标签: c# wpf effect drawingvisual dropshadoweffect