将椭圆的链接列表更改为 C# 中的图像
Changing a linked list of Ellipses to images in c#
我用 c# wpf 制作了一个简单的贪吃蛇游戏 forms.The 问题是我将食物制作为椭圆,我想将其更改为苹果图像。
这是食物 class,我在其中指定了食物的 type/shape:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
namespace WpfApp1
{
class Food
{
public double x, y;
public Ellipse e = new Ellipse();
public Food(double x, double y)
{
this.x = x;
this.y = y;
}
public void setFoodPosition()
{
Random r = new Random();
if (Settings.size != 0)
e.Width = e.Height = Settings.g.Size;
else
e.Width = e.Height = 10;
e.Stroke = Brushes.Black;
e.Fill = new SolidColorBrush(Color.FromRgb((byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255))); ;
Canvas.SetLeft(e, x);
Canvas.SetTop(e, y);
}
}
}
有人知道我是怎么做到的吗?
将椭圆更改为没有描边的矩形,并用代表苹果的画笔填充它。
Apple 图片来自 xamalot
class Food
{
public double x, y;
public Rectangle e = new Rectangle();
private SolidColorBrush brush1 = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FFB5121B"));
private SolidColorBrush brush2 = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF4F6E18"));
private DrawingBrush apple;
private double size = 20.0;
public Food(double x, double y)
{
this.x = x;
this.y = y;
GeometryDrawing a1 = new GeometryDrawing(brush1, null, Geometry.Parse("F1M513.300,368.290C513.300,368.290 530.719,179.903 449.429,135.387 368.139,90.871 273.945,91.516 273.945,91.516 273.945,91.516 228.783,79.903 216.526,80.548 204.269,81.193 125.559,84.419 68.784,142.484 12.009,200.549 4.268,237.322 6.849,285.064 9.430,332.806 34.590,438.613 34.590,438.613 34.590,438.613 70.074,516.677 76.526,521.839 82.978,527.001 122.333,570.871 149.429,576.678 176.525,582.485 234.590,580.549 248.138,578.614 261.686,576.679 339.106,581.194 366.848,576.033 394.590,570.872 475.881,490.870 490.720,461.193 505.559,431.516 513.300,368.290 513.300,368.290z"));
GeometryDrawing a2 = new GeometryDrawing(brush2, null, Geometry.Parse("F1M435.881,122.484L437.171,113.452 462.332,105.710 463.623,92.807C463.623,92.807 495.881,67.000 497.171,55.388 498.461,43.776 505.559,20.549 505.559,20.549L479.107,16.033C479.107,16.033 424.269,12.807 421.688,12.807 419.107,12.807 395.881,14.742 395.881,14.742L337.171,28.936 290.720,72.807 285.559,78.613 272.656,125.710 253.946,143.129 251.365,121.839C251.365,121.839 242.978,94.097 238.462,94.097 233.946,94.097 232.655,90.871 232.655,90.871L229.429,90.871 216.526,99.258 201.687,111.516 208.139,123.129C208.139,123.129 222.977,134.097 226.203,140.548 229.429,146.999 237.171,163.774 233.945,170.225 230.719,176.676 222.332,192.161 222.332,192.161L241.041,200.548C241.041,200.548 247.492,207.645 260.395,196.677 273.298,185.709 257.169,175.387 257.169,175.387 257.169,175.387 251.362,165.710 261.040,159.903 270.718,154.096 304.267,143.774 314.589,144.419 324.911,145.064 350.718,148.290 361.040,149.580 371.362,150.870 435.881,122.484 435.881,122.484z"));
GeometryDrawing a3 = new GeometryDrawing(Brushes.Black, null, Geometry.Parse("F1M128.784,96.677C148.139,88.935 174.369,81.601 174.369,81.601 192.987,77.160 211.565,75.052 230.540,78.296 243.012,80.428 260.740,87.167 273.042,86.000 279.480,78.644 278.052,68.304 283.253,60.448 289.458,51.076 294.940,59.933 294.042,67.000 298.067,54.331 338.001,6.529 351.082,18.709 355.593,22.908 420.671,2.572 431.042,0.000 431.254,3.071 430.920,6.071 430.042,9.000 447.617,12.292 466.476,8.608 484.313,10.769 491.850,11.682 502.369,18.105 509.042,12.000 510.861,24.711 509.113,25.330 507.578,37.200 505.575,52.679 495.643,69.019 484.589,79.747 481.237,83.199 477.389,85.950 473.042,88.000 476.148,95.075 479.028,93.481 472.860,101.040 465.707,109.805 455.381,113.436 445.042,117.000 443.998,125.386 466.546,138.834 471.035,145.778 474.307,150.839 498.550,187.608 495.042,187.000 507.285,204.943 506.970,226.532 513.235,245.733 522.462,274.010 518.921,309.492 517.366,338.795 516.019,364.167 516.886,390.178 509.973,413.289 507.449,421.729 494.598,479.776 483.041,479.000 465.660,528.203 414.718,579.661 361.111,583.683 329.023,586.090 297.163,578.453 265.041,583.000 228.602,589.404 195.545,594.889 159.306,584.585 109.315,570.372 66.443,528.234 48.386,479.739 36.691,448.332 21.452,420.930 14.912,387.631 10.008,362.661 8.375,340.614 3.621,315.216 -4.386,272.435 0.070,221.175 25.211,187.212 35.862,172.824 41.077,151.323 58.022,142.155 67.303,137.134 76.285,126.810 85.433,120.915 85.434,120.915 109.429,104.419 128.784,96.677z M414.042,64.000C426.498,46.435 406.256,32.395 410.042,15.000 397.805,14.493 376.430,18.708 370.022,30.011 365.638,37.745 365.836,46.995 363.872,55.425 361.380,66.117 354.186,75.110 352.042,86.000 372.512,78.557 395.099,75.228 414.042,64.000 M424.042,44.000C426.543,35.314 430.940,27.446 434.042,19.000 426.020,18.754 420.768,21.745 414.042,16.000 411.001,25.542 419.895,36.289 424.042,44.000 M486.042,19.000C472.684,19.179 444.659,13.770 437.010,24.751 430.706,33.800 422.828,52.810 422.042,64.000 422.318,61.028 457.493,47.482 462.096,44.739 472.277,38.671 481.678,31.404 490.042,23.000 490.150,23.019 486.233,18.620 486.042,19.000 M440.042,61.000C452.222,62.766 467.215,65.806 479.105,61.050 494.224,55.002 490.026,34.660 497.042,23.000 480.410,39.150 461.330,51.814 440.042,61.000 M346.042,85.000C360.129,68.533 348.792,47.819 340.042,32.000 326.577,39.407 310.909,52.613 309.903,68.758 309.348,77.656 310.249,86.590 309.428,95.476 308.989,100.234 302.330,109.552 304.041,114.000 312.835,99.033 334.394,96.711 346.042,85.000 M350.042,44.000C354.103,48.646 353.819,55.803 357.042,61.000 359.240,51.294 361.521,41.627 364.042,32.000 357.967,34.124 352.835,38.096 350.042,44.000 M450.042,66.000C455.537,84.095 470.575,77.818 478.042,66.000 471.350,60.585 458.358,70.389 450.042,66.000 M454.042,87.000C455.526,85.148 457.193,83.482 459.042,82.000 445.923,68.860 442.985,62.996 423.392,67.247 405.870,71.048 390.012,79.535 373.042,85.000 393.934,85.236 445.185,125.197 461.042,94.000 458.086,92.294 455.752,89.961 454.042,87.000 M298.042,84.000C301.484,86.409 303.150,89.742 303.042,94.000 309.970,90.194 305.458,75.576 304.042,70.000 300.866,74.154 298.866,78.820 298.042,84.000 M253.042,167.000C255.620,156.719 277.769,140.995 285.505,133.386 289.170,129.709 292.900,126.098 296.694,122.554 305.692,114.337 297.166,117.338 300.555,108.030 304.820,96.323 297.702,85.367 288.041,77.999 283.160,91.979 279.528,106.245 277.362,120.899 275.648,132.500 271.666,134.028 266.362,143.800 266.022,144.426 255.032,154.911 255.041,154.999 242.339,144.154 252.806,121.622 241.041,108.999 239.498,119.949 235.243,128.783 223.041,129.999 230.159,139.753 239.442,156.768 237.195,169.056 236.041,175.365 232.919,181.129 231.137,187.251 228.116,197.635 243.199,198.698 251.041,201.000 255.475,189.287 250.187,178.388 253.042,167.000 M265.042,118.000C269.094,104.222 280.259,94.104 259.664,90.569 244.997,88.051 225.689,88.648 211.924,83.869 203.397,80.909 180.969,88.489 172.042,91.000 151.675,96.730 132.840,106.511 114.042,116.000 59.130,143.718 15.553,199.202 11.810,265.090 10.780,283.229 37.230,442.912 46.748,450.542 46.493,450.617 75.058,505.521 78.999,511.549 98.081,540.728 124.864,561.761 154.786,573.244 185.512,585.036 222.415,572.249 254.209,572.179 287.252,572.107 323.348,579.623 356.163,572.754 409.424,561.606 459.076,499.925 483.924,454.159 499.973,424.601 504.615,389.266 506.601,355.349 508.976,314.777 510.741,268.100 499.309,229.172 491.641,203.060 484.325,173.871 465.068,153.539 457.862,145.930 445.945,137.601 436.101,134.470 422.571,130.166 410.572,144.028 396.597,144.031 392.777,144.031 382.531,152.867 374.192,153.647 361.430,154.841 348.647,152.575 336.316,149.477 312.818,143.572 284.786,153.807 265.044,167.000 265.087,170.391 260.293,187.917 269.044,185.000 277.091,182.317 285.950,184.333 294.044,182.000 305.589,178.673 322.379,168.044 330.044,177.000 312.169,181.677 292.999,183.728 276.811,193.246 257.709,204.477 252.581,208.797 233.053,198.284 224.684,193.779 215.928,191.070 205.814,188.840 194.013,186.238 162.420,187.285 154.045,179.000 166.925,178.886 189.962,181.909 199.045,184.000 234.266,192.062 241.788,151.281 216.045,133.000 209.051,128.034 205.179,127.053 200.594,118.534 192.155,102.850 209.268,103.866 218.033,96.492 245.519,73.367 251.513,109.054 258.045,130.001 267.876,129.306 263.289,123.961 265.042,118.000 M404.042,131.000C417.720,127.298 408.380,124.138 401.849,117.237 391.482,106.280 391.701,92.772 373.814,89.802 361.629,87.780 348.714,90.862 337.491,95.545 332.421,97.660 308.279,109.915 310.043,117.001 320.520,113.944 337.908,121.959 347.446,126.279 362.393,133.047 385.319,136.065 404.042,131.000 M231.042,91.000C225.165,95.639 189.601,111.553 209.254,117.255 227.381,122.514 245.094,103.026 231.042,91.000 M424.042,121.000C420.158,119.020 416.825,116.353 414.042,113.000 419.381,110.729 427.969,113.629 431.042,107.000 418.474,105.041 407.407,98.614 395.042,96.000 398.688,106.570 409.105,129.552 424.042,121.000 M216.042,124.000C221.159,124.441 233.190,128.531 232.042,118.000 229.843,121.464 215.554,118.883 216.042,124.000z M289.042,140.000C309.452,138.049 328.829,142.222 349.042,141.000 339.229,118.123 296.925,112.520 289.042,140.000 M364.042,142.000C366.373,146.741 373.152,140.349 375.042,139.000 365.356,138.139 356.896,132.194 347.042,132.000 348.719,138.333 357.071,149.190 364.042,142.000"));
DrawingGroup innerGroup = new DrawingGroup();
innerGroup.Children.Add(a3);
DrawingGroup outerGroup = new DrawingGroup();
outerGroup.Children.Add(a1);
outerGroup.Children.Add(a2);
outerGroup.Children.Add(innerGroup);
apple = new DrawingBrush();
apple.Drawing = outerGroup;
}
public void SetFoodPosition()
{
Random r = new Random();
if (size != 0)
e.Width = e.Height = size;
else
e.Width = e.Height = 10;
//e.Stroke = Brushes.Black;
e.Fill = apple;// new SolidColorBrush(Color.FromRgb((byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255))); ;
Canvas.SetLeft(e, x);
Canvas.SetTop(e, y);
}
}
如果图像文件中有苹果图像,则应将该文件添加到 Visual Studio 项目(例如,在名为 Images
的项目文件夹中)并将其构建操作设置为Resource
.
然后您将通过 Resource File Pack URI 加载图像,并将其与 ImageBrush 一起用作矩形的填充画笔:
private Rectangle rect = new Rectangle();
...
rect.Fill = new ImageBrush(
new BitmapImage(new Uri("pack://application:,,,/Images/Apple.png")));
或者您使用图像元素而不是矩形:
private Image image = new Image();
...
image.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Apple.png"));
我用 c# wpf 制作了一个简单的贪吃蛇游戏 forms.The 问题是我将食物制作为椭圆,我想将其更改为苹果图像。 这是食物 class,我在其中指定了食物的 type/shape:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
namespace WpfApp1
{
class Food
{
public double x, y;
public Ellipse e = new Ellipse();
public Food(double x, double y)
{
this.x = x;
this.y = y;
}
public void setFoodPosition()
{
Random r = new Random();
if (Settings.size != 0)
e.Width = e.Height = Settings.g.Size;
else
e.Width = e.Height = 10;
e.Stroke = Brushes.Black;
e.Fill = new SolidColorBrush(Color.FromRgb((byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255))); ;
Canvas.SetLeft(e, x);
Canvas.SetTop(e, y);
}
}
}
有人知道我是怎么做到的吗?
将椭圆更改为没有描边的矩形,并用代表苹果的画笔填充它。
Apple 图片来自 xamalot
class Food
{
public double x, y;
public Rectangle e = new Rectangle();
private SolidColorBrush brush1 = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FFB5121B"));
private SolidColorBrush brush2 = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF4F6E18"));
private DrawingBrush apple;
private double size = 20.0;
public Food(double x, double y)
{
this.x = x;
this.y = y;
GeometryDrawing a1 = new GeometryDrawing(brush1, null, Geometry.Parse("F1M513.300,368.290C513.300,368.290 530.719,179.903 449.429,135.387 368.139,90.871 273.945,91.516 273.945,91.516 273.945,91.516 228.783,79.903 216.526,80.548 204.269,81.193 125.559,84.419 68.784,142.484 12.009,200.549 4.268,237.322 6.849,285.064 9.430,332.806 34.590,438.613 34.590,438.613 34.590,438.613 70.074,516.677 76.526,521.839 82.978,527.001 122.333,570.871 149.429,576.678 176.525,582.485 234.590,580.549 248.138,578.614 261.686,576.679 339.106,581.194 366.848,576.033 394.590,570.872 475.881,490.870 490.720,461.193 505.559,431.516 513.300,368.290 513.300,368.290z"));
GeometryDrawing a2 = new GeometryDrawing(brush2, null, Geometry.Parse("F1M435.881,122.484L437.171,113.452 462.332,105.710 463.623,92.807C463.623,92.807 495.881,67.000 497.171,55.388 498.461,43.776 505.559,20.549 505.559,20.549L479.107,16.033C479.107,16.033 424.269,12.807 421.688,12.807 419.107,12.807 395.881,14.742 395.881,14.742L337.171,28.936 290.720,72.807 285.559,78.613 272.656,125.710 253.946,143.129 251.365,121.839C251.365,121.839 242.978,94.097 238.462,94.097 233.946,94.097 232.655,90.871 232.655,90.871L229.429,90.871 216.526,99.258 201.687,111.516 208.139,123.129C208.139,123.129 222.977,134.097 226.203,140.548 229.429,146.999 237.171,163.774 233.945,170.225 230.719,176.676 222.332,192.161 222.332,192.161L241.041,200.548C241.041,200.548 247.492,207.645 260.395,196.677 273.298,185.709 257.169,175.387 257.169,175.387 257.169,175.387 251.362,165.710 261.040,159.903 270.718,154.096 304.267,143.774 314.589,144.419 324.911,145.064 350.718,148.290 361.040,149.580 371.362,150.870 435.881,122.484 435.881,122.484z"));
GeometryDrawing a3 = new GeometryDrawing(Brushes.Black, null, Geometry.Parse("F1M128.784,96.677C148.139,88.935 174.369,81.601 174.369,81.601 192.987,77.160 211.565,75.052 230.540,78.296 243.012,80.428 260.740,87.167 273.042,86.000 279.480,78.644 278.052,68.304 283.253,60.448 289.458,51.076 294.940,59.933 294.042,67.000 298.067,54.331 338.001,6.529 351.082,18.709 355.593,22.908 420.671,2.572 431.042,0.000 431.254,3.071 430.920,6.071 430.042,9.000 447.617,12.292 466.476,8.608 484.313,10.769 491.850,11.682 502.369,18.105 509.042,12.000 510.861,24.711 509.113,25.330 507.578,37.200 505.575,52.679 495.643,69.019 484.589,79.747 481.237,83.199 477.389,85.950 473.042,88.000 476.148,95.075 479.028,93.481 472.860,101.040 465.707,109.805 455.381,113.436 445.042,117.000 443.998,125.386 466.546,138.834 471.035,145.778 474.307,150.839 498.550,187.608 495.042,187.000 507.285,204.943 506.970,226.532 513.235,245.733 522.462,274.010 518.921,309.492 517.366,338.795 516.019,364.167 516.886,390.178 509.973,413.289 507.449,421.729 494.598,479.776 483.041,479.000 465.660,528.203 414.718,579.661 361.111,583.683 329.023,586.090 297.163,578.453 265.041,583.000 228.602,589.404 195.545,594.889 159.306,584.585 109.315,570.372 66.443,528.234 48.386,479.739 36.691,448.332 21.452,420.930 14.912,387.631 10.008,362.661 8.375,340.614 3.621,315.216 -4.386,272.435 0.070,221.175 25.211,187.212 35.862,172.824 41.077,151.323 58.022,142.155 67.303,137.134 76.285,126.810 85.433,120.915 85.434,120.915 109.429,104.419 128.784,96.677z M414.042,64.000C426.498,46.435 406.256,32.395 410.042,15.000 397.805,14.493 376.430,18.708 370.022,30.011 365.638,37.745 365.836,46.995 363.872,55.425 361.380,66.117 354.186,75.110 352.042,86.000 372.512,78.557 395.099,75.228 414.042,64.000 M424.042,44.000C426.543,35.314 430.940,27.446 434.042,19.000 426.020,18.754 420.768,21.745 414.042,16.000 411.001,25.542 419.895,36.289 424.042,44.000 M486.042,19.000C472.684,19.179 444.659,13.770 437.010,24.751 430.706,33.800 422.828,52.810 422.042,64.000 422.318,61.028 457.493,47.482 462.096,44.739 472.277,38.671 481.678,31.404 490.042,23.000 490.150,23.019 486.233,18.620 486.042,19.000 M440.042,61.000C452.222,62.766 467.215,65.806 479.105,61.050 494.224,55.002 490.026,34.660 497.042,23.000 480.410,39.150 461.330,51.814 440.042,61.000 M346.042,85.000C360.129,68.533 348.792,47.819 340.042,32.000 326.577,39.407 310.909,52.613 309.903,68.758 309.348,77.656 310.249,86.590 309.428,95.476 308.989,100.234 302.330,109.552 304.041,114.000 312.835,99.033 334.394,96.711 346.042,85.000 M350.042,44.000C354.103,48.646 353.819,55.803 357.042,61.000 359.240,51.294 361.521,41.627 364.042,32.000 357.967,34.124 352.835,38.096 350.042,44.000 M450.042,66.000C455.537,84.095 470.575,77.818 478.042,66.000 471.350,60.585 458.358,70.389 450.042,66.000 M454.042,87.000C455.526,85.148 457.193,83.482 459.042,82.000 445.923,68.860 442.985,62.996 423.392,67.247 405.870,71.048 390.012,79.535 373.042,85.000 393.934,85.236 445.185,125.197 461.042,94.000 458.086,92.294 455.752,89.961 454.042,87.000 M298.042,84.000C301.484,86.409 303.150,89.742 303.042,94.000 309.970,90.194 305.458,75.576 304.042,70.000 300.866,74.154 298.866,78.820 298.042,84.000 M253.042,167.000C255.620,156.719 277.769,140.995 285.505,133.386 289.170,129.709 292.900,126.098 296.694,122.554 305.692,114.337 297.166,117.338 300.555,108.030 304.820,96.323 297.702,85.367 288.041,77.999 283.160,91.979 279.528,106.245 277.362,120.899 275.648,132.500 271.666,134.028 266.362,143.800 266.022,144.426 255.032,154.911 255.041,154.999 242.339,144.154 252.806,121.622 241.041,108.999 239.498,119.949 235.243,128.783 223.041,129.999 230.159,139.753 239.442,156.768 237.195,169.056 236.041,175.365 232.919,181.129 231.137,187.251 228.116,197.635 243.199,198.698 251.041,201.000 255.475,189.287 250.187,178.388 253.042,167.000 M265.042,118.000C269.094,104.222 280.259,94.104 259.664,90.569 244.997,88.051 225.689,88.648 211.924,83.869 203.397,80.909 180.969,88.489 172.042,91.000 151.675,96.730 132.840,106.511 114.042,116.000 59.130,143.718 15.553,199.202 11.810,265.090 10.780,283.229 37.230,442.912 46.748,450.542 46.493,450.617 75.058,505.521 78.999,511.549 98.081,540.728 124.864,561.761 154.786,573.244 185.512,585.036 222.415,572.249 254.209,572.179 287.252,572.107 323.348,579.623 356.163,572.754 409.424,561.606 459.076,499.925 483.924,454.159 499.973,424.601 504.615,389.266 506.601,355.349 508.976,314.777 510.741,268.100 499.309,229.172 491.641,203.060 484.325,173.871 465.068,153.539 457.862,145.930 445.945,137.601 436.101,134.470 422.571,130.166 410.572,144.028 396.597,144.031 392.777,144.031 382.531,152.867 374.192,153.647 361.430,154.841 348.647,152.575 336.316,149.477 312.818,143.572 284.786,153.807 265.044,167.000 265.087,170.391 260.293,187.917 269.044,185.000 277.091,182.317 285.950,184.333 294.044,182.000 305.589,178.673 322.379,168.044 330.044,177.000 312.169,181.677 292.999,183.728 276.811,193.246 257.709,204.477 252.581,208.797 233.053,198.284 224.684,193.779 215.928,191.070 205.814,188.840 194.013,186.238 162.420,187.285 154.045,179.000 166.925,178.886 189.962,181.909 199.045,184.000 234.266,192.062 241.788,151.281 216.045,133.000 209.051,128.034 205.179,127.053 200.594,118.534 192.155,102.850 209.268,103.866 218.033,96.492 245.519,73.367 251.513,109.054 258.045,130.001 267.876,129.306 263.289,123.961 265.042,118.000 M404.042,131.000C417.720,127.298 408.380,124.138 401.849,117.237 391.482,106.280 391.701,92.772 373.814,89.802 361.629,87.780 348.714,90.862 337.491,95.545 332.421,97.660 308.279,109.915 310.043,117.001 320.520,113.944 337.908,121.959 347.446,126.279 362.393,133.047 385.319,136.065 404.042,131.000 M231.042,91.000C225.165,95.639 189.601,111.553 209.254,117.255 227.381,122.514 245.094,103.026 231.042,91.000 M424.042,121.000C420.158,119.020 416.825,116.353 414.042,113.000 419.381,110.729 427.969,113.629 431.042,107.000 418.474,105.041 407.407,98.614 395.042,96.000 398.688,106.570 409.105,129.552 424.042,121.000 M216.042,124.000C221.159,124.441 233.190,128.531 232.042,118.000 229.843,121.464 215.554,118.883 216.042,124.000z M289.042,140.000C309.452,138.049 328.829,142.222 349.042,141.000 339.229,118.123 296.925,112.520 289.042,140.000 M364.042,142.000C366.373,146.741 373.152,140.349 375.042,139.000 365.356,138.139 356.896,132.194 347.042,132.000 348.719,138.333 357.071,149.190 364.042,142.000"));
DrawingGroup innerGroup = new DrawingGroup();
innerGroup.Children.Add(a3);
DrawingGroup outerGroup = new DrawingGroup();
outerGroup.Children.Add(a1);
outerGroup.Children.Add(a2);
outerGroup.Children.Add(innerGroup);
apple = new DrawingBrush();
apple.Drawing = outerGroup;
}
public void SetFoodPosition()
{
Random r = new Random();
if (size != 0)
e.Width = e.Height = size;
else
e.Width = e.Height = 10;
//e.Stroke = Brushes.Black;
e.Fill = apple;// new SolidColorBrush(Color.FromRgb((byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255))); ;
Canvas.SetLeft(e, x);
Canvas.SetTop(e, y);
}
}
如果图像文件中有苹果图像,则应将该文件添加到 Visual Studio 项目(例如,在名为 Images
的项目文件夹中)并将其构建操作设置为Resource
.
然后您将通过 Resource File Pack URI 加载图像,并将其与 ImageBrush 一起用作矩形的填充画笔:
private Rectangle rect = new Rectangle();
...
rect.Fill = new ImageBrush(
new BitmapImage(new Uri("pack://application:,,,/Images/Apple.png")));
或者您使用图像元素而不是矩形:
private Image image = new Image();
...
image.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Apple.png"));