我有一张背景图片和一张前景图片。我想在另一个上面展示一个。前景图像可能无法覆盖背景图像的整个区域。
所以,我按照给定的顺序调用这些函数
PaintSliced(b2.rect, new Rect(p._BackImageUV.X, p._BackImageUV.Y, p._BackImageUV.Width, p._BackImageUV.Height), p._BackImage, p, p._Slice);
PaintSliced(b2.rect, new Rect(p._ForeImageUV.X, p._ForeImageUV.Y, p._ForeImageUV.Width, p._ForeImageUV.Height), p._ForeImage, p, p._Slice);
但是,前景图像(宽度和高度相等,但面积较小)覆盖整个背景图像。它基本上覆盖了整个矩形。
如何实现我需要的功能。我希望我对这个问题很清楚。
private void PaintSliced(Grid gr,Rect UVRectangle, String imgpath, UIControl_Prop props, UISlice sliced)
{
//gr.Background = null;
if (sliced.Equals(UISlice.yes) && props._BackImage != null && props._BackImage != "")
{
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(imgpath, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
//9 slice parameters have yet to be passed
//TODO implement a new property for 9 slicing parameters
//Default params are 20% of the actual size of the control
using (FileStream fileStream = new FileStream(imgpath, FileMode.Open, FileAccess.Read))
{
BitmapFrame frame = BitmapFrame.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
//Size s = new Size(frame.PixelWidth, frame.PixelHeight);
Size s = UVRectangle.Size;
Rectangle dTopLeft, dTopCenter, dTopRight, //Destination Rectangles
dMidLeft, dMidCenter, dMidRight,
dBottomLeft, dBottomCenter, dBottomRight;
Rect sTopLeft, sTopCenter, sTopRight, //Source Rectangles
sMidLeft, sMidCenter, sMidRight,
sBottomLeft, sBottomCenter, sBottomRight;
int side_control; //side used by the control
int side = side_control = (int)(s.Width < s.Height ? s.Width*0.2f : s.Height*0.2f);
if (2 * side > gr.Width || 2 * side > gr.Height)
side_control = (int)(gr.Width < gr.Height ? gr.Width / 2 : gr.Height / 2);
s = new Size(frame.PixelWidth, frame.PixelHeight);
ImageBrush ib1 = new ImageBrush(src);
/*Top Left*/
sTopLeft = new Rect(0 + UVRectangle.X / s.Width, 0 + UVRectangle.Y / s.Height, side / s.Width, side / s.Height);
dTopLeft = new Rectangle();
dTopLeft.VerticalAlignment = VerticalAlignment.Top;
dTopLeft.HorizontalAlignment = HorizontalAlignment.Left;
dTopLeft.Width = dTopLeft.Height = side_control;
//dTopLeft.Margin = new System.Windows.Thickness(0, 0, gr.Width - side_control, gr.Height - side_control);
ib1.Viewbox = sTopLeft;
dTopLeft.Fill = ib1;
gr.Children.Add(dTopLeft);
ImageBrush ib2 = new ImageBrush(src);
/*Top Center */
sTopCenter = new Rect((UVRectangle.X+ side) / s.Width, 0 + UVRectangle.Y / s.Height, (UVRectangle.Width - 2 * side) / s.Width, side / s.Height);
dTopCenter= new Rectangle();
dTopCenter.VerticalAlignment = VerticalAlignment.Top;
dTopCenter.HorizontalAlignment = HorizontalAlignment.Left;
dTopCenter.Height = side_control;
dTopCenter.Width = gr.Width - 2 * side_control;
dTopCenter.Margin = new System.Windows.Thickness(side_control, 0, 0, 0);
ib2.Viewbox = sTopCenter;
dTopCenter.Fill = ib2;
gr.Children.Add(dTopCenter);
ImageBrush ib3 = new ImageBrush(src);
/*Top Right*/
sTopRight = new Rect((UVRectangle.X + UVRectangle.Width - side) / s.Width, 0 + UVRectangle.Y / s.Height, side/s.Width, side/s.Height);
dTopRight= new Rectangle();
dTopRight.VerticalAlignment = VerticalAlignment.Top;
dTopRight.HorizontalAlignment = HorizontalAlignment.Left;
dTopRight.Height = side_control;
dTopRight.Width = side_control;
dTopRight.Margin = new System.Windows.Thickness(gr.Width - side_control, 0, 0, 0);
ib3.Viewbox = sTopRight;
dTopRight.Fill = ib3;
gr.Children.Add(dTopRight);
ImageBrush ib4 = new ImageBrush(src);
/*Middle Left*/
sMidLeft = new Rect(UVRectangle.X/s.Width, (UVRectangle.Y + side)/s.Height, side/ s.Width, (UVRectangle.Height - 2*side) / s.Height);
dMidLeft = new Rectangle();
dMidLeft.VerticalAlignment = VerticalAlignment.Top;
dMidLeft.HorizontalAlignment = HorizontalAlignment.Left;
dMidLeft.Height = gr.Height - 2 * side_control;
dMidLeft.Width = side_control;
dMidLeft.Margin = new System.Windows.Thickness(0, side_control, 0, 0);
ib4.Viewbox = sMidLeft;
dMidLeft.Fill = ib4;
gr.Children.Add(dMidLeft);
ImageBrush ib5 = new ImageBrush(src);
/*Middle Center*/
sMidCenter = new Rect((UVRectangle.X + side)/s.Width, (UVRectangle.Y + side)/s.Height, (UVRectangle.Width - 2 * side) / s.Width, (UVRectangle.Height- 2 * side) / s.Height);
dMidCenter = new Rectangle();
dMidCenter.VerticalAlignment = VerticalAlignment.Top;
dMidCenter.HorizontalAlignment = HorizontalAlignment.Left;
dMidCenter.Height = gr.Height - 2 * side_control;
dMidCenter.Width = gr.Width- 2 * side_control;
dMidCenter.Margin = new System.Windows.Thickness(side_control, side_control, 0, 0);
ib5.Viewbox = sMidCenter;
dMidCenter.Fill = ib5;
gr.Children.Add(dMidCenter);
ImageBrush ib6 = new ImageBrush(src);
/*Middle Right*/
sMidRight= new Rect((UVRectangle.X + UVRectangle.Width - side)/s.Width,(UVRectangle.Y +side)/s.Height, (side)/s.Width, (UVRectangle.Height - 2 * side) / s.Height);
dMidRight = new Rectangle();
dMidRight.VerticalAlignment = VerticalAlignment.Top;
dMidRight.HorizontalAlignment = HorizontalAlignment.Left;
dMidRight.Height = gr.Height - 2 * side_control;
dMidRight.Width = side_control;
dMidRight.Margin = new System.Windows.Thickness(gr.Width - side_control, side_control, 0, 0);
ib6.Viewbox = sMidRight;
dMidRight.Fill = ib6;
gr.Children.Add(dMidRight);
ImageBrush ib7 = new ImageBrush(src);
/*Bottom Left*/
sBottomLeft= new Rect(UVRectangle.X/s.Width, (UVRectangle.Y + UVRectangle.Height- side)/s.Height, side/s.Width, side/s.Height);
dBottomLeft = new Rectangle();
dBottomLeft.VerticalAlignment = VerticalAlignment.Top;
dBottomLeft.HorizontalAlignment = HorizontalAlignment.Left;
dBottomLeft.Height = side_control;
dBottomLeft.Width = side_control;
dBottomLeft.Margin = new System.Windows.Thickness(0, gr.Height - side_control, 0, 0);
ib7.Viewbox = sBottomLeft;
dBottomLeft.Fill = ib7;
gr.Children.Add(dBottomLeft);
ImageBrush ib8 = new ImageBrush(src);
/*Bottom Center*/
sBottomCenter = new Rect((UVRectangle.X + side) / s.Width, (UVRectangle.Y + UVRectangle.Height - side) / s.Height, (UVRectangle.Width - 2 * side) / s.Width, side / s.Height);
dBottomCenter = new Rectangle();
dBottomCenter.VerticalAlignment = VerticalAlignment.Top;
dBottomCenter.HorizontalAlignment = HorizontalAlignment.Left;
dBottomCenter.Height = side_control;
dBottomCenter.Width = gr.Width - 2 * side_control;
dBottomCenter.Margin = new System.Windows.Thickness(side_control, gr.Height - side_control, 0, 0);
ib8.Viewbox = sBottomCenter;
dBottomCenter.Fill = ib8;
gr.Children.Add(dBottomCenter);
ImageBrush ib9 = new ImageBrush(src);
/*Bottom Right*/
sBottomRight = new Rect((UVRectangle.X + UVRectangle.Width - side) / s.Width, (UVRectangle.Y + UVRectangle.Height - side) / s.Height, side / s.Width, side / s.Height);
dBottomRight = new Rectangle();
dBottomRight.VerticalAlignment = VerticalAlignment.Top;
dBottomRight.HorizontalAlignment = HorizontalAlignment.Left;
dBottomRight.Height = side_control;
dBottomRight.Width = side_control;
dBottomRight.Margin = new System.Windows.Thickness(gr.Width - side_control, gr.Height - side_control, 0, 0);
ib9.Viewbox = sBottomRight;
dBottomRight.Fill = ib9;
gr.Children.Add(dBottomRight);
}
}
else if(props._BackImage != null && props._BackImage != "")
{
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(imgpath, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
ImageBrush ib = new ImageBrush(src);
ib.Viewbox = new Rect(UVRectangle.X / src.PixelWidth, UVRectangle.Y / src.PixelHeight, UVRectangle.Width / src.PixelWidth, UVRectangle.Height / src.PixelHeight);
gr.Background = ib;
}
答案 0 :(得分:2)
据我所知,你只想要一些透明的图像。我很不清楚你为什么要做那些庞大(丑陋)的代码。
试试这个:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="800" Height="600">
<Grid>
<Image Source="http://upload.wikimedia.org/wikipedia/en/2/2d/SRU-Logo-Transparent.png"/>
<Image Source="http://upload.wikimedia.org/wikipedia/commons/c/ca/Triple-Spiral-4turns_green_transparent.png"/>
<Image Source="http://www.axdn.com/redist/axpssp_logo.png"/>
</Grid>
</Window>
代码背后:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
结果:
根据需要替换Source
元素的Image
属性(使用本地驱动器上的图像路径,或者更好地使用WPF应用程序中的paths to Resources
)。
请确保使用PNG
或其他支持透明度的图片格式(JPG在此处不起作用)。
根据需要设置Panel.ZIndex
附加属性,以定义Panel中UI元素的Z顺序(例如Grid
)。
习惯使用XAML与UI相关的事情而不是WPF中的过程代码。