簡體   English   中英

在二維數組中顯示圖像的多個實例

[英]Displaying mutiple instances of an image in a 2d array

這就是我決定從一個名為btnCreate的winForm&1按鈕開始的過程,我有2個圖像35px X 35px floor.bmp和wall.bmp

public partial class Form1 : Form
{
    int x;
    int y;
    int[] Row0 = new int[10] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    int[] Row1 = new int[10] { 1, 1, 1, 1, 0, 0, 1, 1, 0, 1 };
    int[] Row2 = new int[10] { 1, 1, 0, 0, 0, 0, 1, 1, 0, 1 };
    int[] Row3 = new int[10] { 1, 1, 1, 1, 0, 1, 1, 1, 0, 1 };
    int[] Row4 = new int[10] { 1, 0, 0, 1, 0, 1, 0, 0, 0, 1 };
    int[] Row5 = new int[10] { 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 };
    int[] Row6 = new int[10] { 1, 1, 0, 0, 0, 0, 1, 1, 0, 1 };
    int[] Row7 = new int[10] { 1, 1, 1, 1, 0, 0, 1, 1, 0, 1 };
    int[] Row8 = new int[10] { 1, 1, 0, 0, 0, 0, 1, 1, 0, 1 };
    int[] Row9 = new int[10] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    int[,] LevelA = new int[10,10];

    private void Form1_Load(object sender, EventArgs e)
    {
        for (x = 0; x < 10; x++)
        {
            //load all arrays 
            LevelA[0, x] = Row0[x];
            LevelA[1, x] = Row1[x];
            LevelA[2, x] = Row2[x];
            LevelA[3, x] = Row3[x];
            LevelA[4, x] = Row4[x];
            LevelA[5, x] = Row5[x];
            LevelA[6, x] = Row6[x];
            LevelA[7, x] = Row7[x];
            LevelA[8, x] = Row8[x];
            LevelA[9, x] = Row9[x];
        }
    }

    private void btnCreate_Click(object sender, EventArgs e)
    {
        for (int X = 0; X < 10; X++)
        {
            for (int Y = 0; Y < 10; Y++)
            {
                //the following is the idea of what I would like to accomplish
                //if (LevelA[Y,X] == 0)
                //{
                    //Bitmap myBmp = new Bitmap(Image.FromFile(@"C:\My Documents\floor2.bmp"));
                    //myBmp.Top == Y * 35;
                    //myBmp.left == X * 35;
                //}
            }
        }
    }
}

上面的代碼是否也可以按我的意圖工作? 還是for循環的末尾僅顯示最后一個圖塊,因為它將繼續用新的圖塊替換(破壞)前一個圖塊? 在女巫的情況下,我需要100 x 10乘10網格的myBmp嗎?

您可以通過創建Bitmap對象來獲取圖像文件的Bitmap

 Bitmap bmp = new Bitmap(Image.FromFile(@"D:\MyImageFile.bmp"));

只要擁有Graphics實例,就可以將其放置或繪制在任何地方。 您可以在此處找到asimple示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM