MSDN का कहना है, कि कोई धब्बा बिटमैप प्रभाव लागू कर सकता है जिसके द्वारा तैयार किया गया सामान है DrawingContext
का उपयोग करते हुए PushEffect
तरीका। लेकिन, दोनों PushEffect
तथा *BitmapEffect
s अप्रचलित के रूप में चिह्नित हैं।
मैं ड्रॉइंग को कैसे लागू कर सकता हूं जो ड्रॉइंग कॉन्टेक्स्ट द्वारा तैयार किया गया है?
उत्तर:
जवाब के लिए 5 № 1हेयर यू गो
अपने के निर्माता में UserControl
ठीक Effect
के एक उदाहरण के लिए संपत्ति BlurEffect
और जो पूरे रेंडर को धुंधला कर देगा।
public UserControl()
{
InitializeComponent();
Effect = new BlurEffect() { Radius = 10 };
}
चयनात्मक धुंधला
मैंने लीवरेजिंग द्वारा सेलेक्टिव ब्लर हासिल करने का प्रयास किया RenderTargetBitmap वर्ग
मैंने सरलीकृत उपयोग के लिए एक एक्सटेंशन विधि बनाई है
Extension class
public static class DrawingContextExtension
{
public static void RenderBlurred(this DrawingContext dc, int width, int height, Rect targetRect, double blurRadius, Action<DrawingContext> action)
{
Rect elementRect = new Rect(0, 0, width, height);
BlurredElement element = new BlurredElement(action)
{
Width = width,
Height = height,
Effect = new BlurEffect() { Radius = blurRadius }
};
element.Arrange(elementRect);
RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
rtb.Render(element);
dc.DrawImage(rtb, targetRect);
}
class BlurredElement : FrameworkElement
{
Action<DrawingContext> action;
public BlurredElement(Action<DrawingContext> action)
{
this.action = action;
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
action(drawingContext);
}
}
}
उदाहरण कोड
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
int boxSize = 20;
Pen pen = new Pen(new SolidColorBrush(Colors.Black), 1);
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Rect targetRect = new Rect(i * boxSize, j * boxSize, boxSize, boxSize);
if (j % 2 == 0)
{
Rect elementRect = new Rect(0, 0, boxSize, boxSize);
double blurRadius = 5;
drawingContext.RenderBlurred(boxSize, boxSize, targetRect, blurRadius, dc => dc.DrawRectangle(new SolidColorBrush(Colors.Transparent), pen, elementRect));
}
else
{
drawingContext.DrawRectangle(new SolidColorBrush(Colors.Transparent), pen, targetRect);
}
}
}
}
परिणाम
ऊपर के उदाहरण में हर विषम पंक्ति में आयतें धुंधली हैं