我将展示一些(脏)代码,我完全不了解,因为我是C#的新手。
根据我的理解,任何要绘制的控件都会被分解为9个矩形,然后通过设置Rect的Fill
属性来单独绘制每个矩形
我将隐藏每个矩形的代码以保持代码的可管理性,但是,我希望你能得到函数试图实现的功能。
现在,我需要解决的问题是我需要打印两个图像,一个在另一个上面。
例如,我需要一个实心矩形顶部的实心圆。所以,我可以看到整个圆圈,但只能看到矩形的角落。我不希望圆圈透明。原始的circle.png没有矩形边框。它的边界仅沿着圆的边界。因此,理想情况下,整个圆圈不应与矩形重叠。
但是,如果我将下面给出的函数调用两次,一次用于矩形,然后用于圆,则整个矩形重叠。我不想要那个。我该怎么办?
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);
}
}
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;
}
if (props._TextHandle != -1)
PaintText(gr, props._Text, props);
}