Silverlight编程绘图(从Windows窗体转换到Silverlight)



我有一个世界(一个PictureBox和一个位图与大小(行*列)和创建图形从位图在Windows窗体)以下是代码,

    bmp = new Bitmap(columns*32, rows*32);
    graphics = Graphics.FromImage(bmp);
    pictureBox1.Size = new Size(columns * 32, rows * 32);
    pictureBox1.Image = bmp;

这是我用来填充图形在Windows窗体,

    private void drawImageInBox(Image src, int boxX, int boxY, int Offset = 0)
    {
        graphics.DrawImage(src, new Point((boxY * 32) + Offset, (boxX * 32) + Offset));
    }

和当所有的操作完成后,我只是刷新PictureBox,

    pictureBox1.Refresh();

我如何在Silverlight xaml中实现这一点?

您将需要这样的内容:

在你的XAML:

<Grid x:Name="world_grid" Width="100" Height="100">
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />   <!--Repeat this for however many rows you need -->
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" /> <!--Same here, for columns-->
  </Grid.ColumnDefinitions>
</Grid>

在你的代码后面:

for each image you need to add:
  Image img = new Image();
  img.Source = new Uri("some/dir/img.png"); // coding in-place, can't remember syntax
  world_grid.Children.Add(img);
  Grid.SetRow(img, 0); // places this image in row 0
  Grid.SetColumn(img, 5); // places this image in column 5

一个非常重要的提示:每次你"刷新"你的"世界"时,确保在重新添加它们之前删除适当的图像!更好的是,您可以在某个数组中跟踪这些图像,并更改它们的行/列。如果你需要,你可以设置图像的可见性为隐藏/折叠

最新更新