如何在绘图上下文工具上显示dropshadow

本文关键字:显示 dropshadow 工具 上下文 绘图 | 更新日期: 2023-09-27 18:28:48

实际上,我使用drawingContext.DrawRectangle方法在画布上绘制矩形。。我想在矩形上添加阴影效果。。

drawingContext.DrawRectangle(new SolidColorBrush(graphicsObjectFillColor),
                             new Pen(new SolidColorBrush(ObjectColor), ActualLineWidth),
                             Rectangle);

或者我用这个来添加阴影。。

 DropShadowEffect effect = new DropShadowEffect();
 effect = new DropShadowEffect { Color = Colors.Black, Direction = -45, Opacity = 0.5, ShadowDepth = 4};
 this.Effect = effect;

阴影正在显现但加上在图像上绘制所有工具的时间tha shodow没有动摇我正在使用

                    DrawingVisual vs = new DrawingVisual();
                    DrawingContext dc = vs.RenderOpen();
                    // Draw image
                    dc.DrawImage(image.Source, rect);
                    double scale = width / image.Source.Width;
                    // Keep old existing actual scale and set new actual scale.
                    double oldActualScale = drawingCanvas.ActualScale;
                    drawingCanvas.ActualScale = oldActualScale;
                    // Remove clip in the canvas - we set our own clip.
                    drawingCanvas.RemoveClip();
                    // Prepare drawing context to draw graphics
                    rect = new Rect(left, top, width, height);
                    dc.PushClip(new RectangleGeometry(rect));
                    double horizontalScale = Math.Abs((positionDrawingCanvas.X) - (positionImage.X));
                    double verticalScale = Math.Abs((positionDrawingCanvas.Y) - (positionImage.Y));
                    double difX = 0.0;
                    double difY = 0.0;
                    //if (horizontalScale != 0 && verticalScale != 0)
                    //{
                    //     //horizontalScale = Math.Abs((positionDrawingCanvas.X + Math.Abs((positionImage.X / sliderScale.Value - positionImage.X))) - (positionImage.X));
                    //     //verticalScale = Math.Abs((positionDrawingCanvas.Y + Math.Abs((positionImage.Y / sliderScale.Value - positionImage.Y))) - (positionImage.Y));
                    //    difX = (positionImage.X - positionImage.X / sliderScale.Value);
                    //    difY = (positionImage.Y - positionImage.Y / sliderScale.Value);
                    //}
                    dc.PushTransform(new TranslateTransform(difX + left - horizontalScale, difY+top - verticalScale));
                    dc.PushTransform(new ScaleTransform(1, 1));
                    // Ask canvas to draw overlays
                    drawingCanvas.Draw(dc);
                    // Restore old actual scale.
                    drawingCanvas.ActualScale = oldActualScale;
                    // Restore clip
                    drawingCanvas.RefreshClip();
                    dc.Pop();
                    dc.Pop();
                    dc.Pop();
                    dc.Close();
                    width = (Utilityhelper.GetDIPIndependentHorizontal(rect.Width));
                    height = (Utilityhelper.GetDIPIndependentVertical(rect.Height));
                    bmp = new RenderTargetBitmap((int)width, (int)(height), Utilityhelper.graphics.DpiX, Utilityhelper.graphics.DpiY, PixelFormats.Default);
                    //bmp = new RenderTargetBitmap((int)(scale * (rect.Width)), (int)(scale * (rect.Height)), scale * 96, scale * 96, PixelFormats.Default);
                    bmp.Render(vs);
                    sliderScale.Value = oldScale;
                    //imageBackground.Stretch = Stretch.Uniform;
                    //drawingCanvas.Width = (Utilityhelper.GetDIPDependentHorizontal(drawingCanvas.Width));
                    //drawingCanvas.Height = (Utilityhelper.GetDIPDependentVertical(drawingCanvas.Height));
                    return bmp;

如何在绘图上下文工具上显示dropshadow

您只需更改渲染方法

RenderTargetBitmap bmp = new RenderTargetBitmap((int)width, (int)(height), DpiX, DpiY, PixelFormats.Default);
BitmapSource source = null;
if (bmp != null)
{
    bmp.Render(image);
    bmp.Render(drawingCanvas);
    source = bmp;
}

以下是如何在wpf:中从视觉中获取图像(位图)

        // render content into image
        var render = new RenderTargetBitmap((int)ContentPresenter.RenderSize.Width, (int)ContentPresenter.RenderSize.Height, 96, 96, PixelFormats.Default);
        render.Render(ContentPresenter);
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(render));
        using (var stream = new MemoryStream())
        {
            // create bitmap image
            encoder.Save(stream);
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = stream;
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.EndInit();
            bitmap.Freeze();
            // use BitmapImage
            ...
        }

CCD_ 1具有一定的可视性。