Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 照片矩阵_C#_Winforms - Fatal编程技术网

C# 照片矩阵

C# 照片矩阵,c#,winforms,C#,Winforms,因此,我有一个代码,通过resourcesimage foodWorld=resources.orange将图像注入到我的项目中,我想用这张照片制作一个矩阵,它可以如下所示: 我有这个代码,但我不知道如何绘制矩阵。此外,我不知道这是否是正确的绘制方式: this.Width = 400; this.Height = 300; Bitmap b = new Bitmap(this.Width, this.Height); for(int i = 0; i < this.Height;

因此,我有一个代码,通过resources
image foodWorld=resources.orange将图像注入到我的项目中,我想用这张照片制作一个矩阵,它可以如下所示:

我有这个代码,但我不知道如何绘制矩阵。此外,我不知道这是否是正确的绘制方式:

this.Width = 400;
this.Height = 300;

Bitmap b = new Bitmap(this.Width, this.Height);

for(int i = 0; i < this.Height; i++)
{
    for(int j = 0; j < this.Width; j ++)
    {
        //fill the matrix
    }
}
this.Width=400;
这个。高度=300;
位图b=新位图(this.Width,this.Height);
for(int i=0;i
我不太熟悉WinForms,但在WPF中,我会这样做:

var columns = 15;
var rows = 10;

var imageWidth = 32;
var imageHeight = 32;

var grid = new Grid();
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < columns; j++)
    {
        //Get the image in your project; I'm not sure how this is done in WinForms
        var b = new Bitmap(imageWidth, imageHeight);

        //Display it
        var pictureBox = new PictureBox();
        pictureBox.Image = b;

        //Set the position
        Grid.SetColumn(j, pictureBox);
        Grid.SetRow(i, pictureBox);

        //Insert into the "matrix"
        grid.Children.Add(pictureBox);
    }
}
var列=15;
var行=10;
var-imageWidth=32;
var-imageHeight=32;
var grid=new grid();
对于(int i=0;i
对于移动的Pacman,重复上述步骤,但仅针对一张图像。存储当前位置的参考,并在按下某些键时

  • 对其边距设置动画,直到它出现在相邻单元格中(例如,如果每个单元格宽16像素,pacman应位于任何给定单元格的中心,则将右侧边距设置动画16像素,使其进入右侧单元格,依此类推)
  • 移动到另一个单元格后,根据上次移动的方向设置新行和新列
  • 如果在新位置有水果,则在该位置获取水果并将其从
    网格中移除。您可以使用
    myGrid来获得它。假设
    currentRow
    currentColumn
    都是以零为基础的子项[currentRow*totalColumns+currentColumn]
  • 对必须移动到的每个单元格重复此操作

这确实意味着矩阵将有一个固定的大小,但在WPF中,有一个
Viewbox
,这对于这些类型的场景来说很方便。另外,将pacman的z指数设置为大于水果,使其始终位于顶部。

执行时会发生什么?您的“填写矩阵”代码是什么?请记住,您需要计算每个图标的大小,否则它将被放置在彼此内部这是什么?是否有120000个元素或150个元素,如您所附的图片所示?图像的尺寸是多少?知道我该怎么做吗?一般来说,我如何制作这样一个照片矩阵使用如下:
使用(Graphics g=Graphics.FromImage(b){..yourloops.}
在代码中计算位置和大小以及目标矩形和源矩形。然后写入
g.DrawImage(yourorangebmp,destRect,srcRect,GraphisUnit.Pixels)
Done。注意,atm循环次数为300x400次。您需要更正这一点。WPF中可能存在重复项控件绑定到某个集合,而无需任何代码。我同意这是理想的方法;但是,OP没有使用WPF,因此在这里演示使用数据绑定的示例不是很有用。WPF是n我的例子是基于OP已经知道的内容与WinForms一起使用的。我从未声称这个例子是基于WPF的,考虑到WPF没有
PictureBox
,这应该是显而易见的。根据我使用WPF的经验,我填补了空白,目的是为WinForms提供一个潜在的解决方案。