简体   繁体   English

C#Windows Phone-创建圆形蒙版

[英]C# Windows Phone - Creating a round mask

I need to mask dynamically created images, so that they will be shown as circles. 我需要遮盖动态创建的图像,以便将它们显示为圆圈。 Pictures can be square, but are usually rectangles... so the circle that will be shown can be taken from the center of it...so the shown circle must be inscribed in the picture and centered in the center of it. 图片可以是正方形,但通常是矩形...因此可以从图片的中心截取要显示的圆圈...因此,所显示的圆圈必须刻在图片中并居中居中。

This is the code I'm using right now: 这是我现在正在使用的代码:

//Setting up the image
Image image = new Image();
image.Height = 70;
image.Width = 70;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri("http://url-of-the-image", UriKind.Absolute);
image.CacheMode = new BitmapCache();
image.Source = bitmapImage;
image.Stretch = Stretch.UniformToFill;
image.VerticalAlignment = System.Windows.VerticalAlignment.Center;

//Setting up the mask
RadialGradientBrush opacityMask = new RadialGradientBrush();
GradientStop gs1 = new GradientStop();
GradientStop gs2 = new GradientStop();
GradientStop gs3 = new GradientStop();
gs1.Color = Color.FromArgb(255, 0, 0, 0);
gs1.Offset = 0.0;
gs2.Color = Color.FromArgb(255, 0, 0, 0);
gs2.Offset = 0.999;
gs3.Color = Color.FromArgb(0, 0, 0, 0);
gs3.Offset = 1.0;
opacityMask.GradientStops.Add(gs1);
opacityMask.GradientStops.Add(gs2);
opacityMask.GradientStops.Add(gs3);
image.OpacityMask = opacityMask;

//Showing the image
panel.Children.Add(image);

This all works fine, but when the pictures are rectangular and not square, this creates an ellipse instead of a circle... any idea on how can I force it to create a circle? 这一切都很好,但是当图片是矩形而不是正方形时,会创建一个椭圆而不是一个圆形...关于如何强制其创建一个圆形的任何想法?

I also tried to specify some more parameters, but doesn't seem to help: 我也尝试指定更多参数,但似乎无济于事:

opacityMask.Center = new Point(0.5, 0.5);
opacityMask.RadiusX = 0.5;
opacityMask.RadiusY = 0.5;

Okay, today i tried again to fix this, and i came out with a solution. 好的,今天我再次尝试解决此问题,然后提出了解决方案。 It's not the best, more clean solution ever...but works :) 这不是有史以来最好的,更干净的解决方案...但是可以:)

I basically wrapped the picture (not masked) into a StackPanel and then applied the mask to the StackPanel instead ;) 我基本上将图片(未遮罩)包装到StackPanel中,然后将遮罩应用于StackPanel;)

This is how it looks like (the only lines that change from the original are the the last few ones): 这是这样的样子(与原始行唯一不同的是最后几行):

//Setting up the image
Image image = new Image();
image.Height = 70;
image.Width = 70;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri("http://url-of-the-image", UriKind.Absolute);
image.CacheMode = new BitmapCache();
image.Source = bitmapImage;
image.Stretch = Stretch.UniformToFill;
image.VerticalAlignment = System.Windows.VerticalAlignment.Center;

//Setting up the mask
RadialGradientBrush opacityMask = new RadialGradientBrush();
GradientStop gs1 = new GradientStop();
GradientStop gs2 = new GradientStop();
GradientStop gs3 = new GradientStop();
gs1.Color = Color.FromArgb(255, 0, 0, 0);
gs1.Offset = 0.0;
gs2.Color = Color.FromArgb(255, 0, 0, 0);
gs2.Offset = 0.999;
gs3.Color = Color.FromArgb(0, 0, 0, 0);
gs3.Offset = 1.0;
opacityMask.GradientStops.Add(gs1);
opacityMask.GradientStops.Add(gs2);
opacityMask.GradientStops.Add(gs3);

//Setting up the StackPanel
StackPanel sp = new StackPanel();
sp.OpacityMask = opacityMask;

//Showing the image
sp.Children.Add(image);
panel.Children.Add(sp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM