11

重複の可能性:
画像が 9 個に分割されている

私は十分にグーグルで検索しましたが、残念ながら助けを見つけることができませんでした。このコード プロジェクト チュートリアルも、私が実際に必要としているものを提供できませんでした。

WinForm に Image と 9 つの PictureBox があります。

Image img = Image.FromFile("media\\a.png"); // a.png has 312X312 width and height
//          some code help, to get
//          img1, img2, img3, img4, img5, img6, img7, img8, img9
//          having equal width and height
//          then...
pictureBox1.Image = img1;
pictureBox2.Image = img2;
pictureBox3.Image = img3;
pictureBox4.Image = img4;
pictureBox5.Image = img5;
pictureBox6.Image = img6;
pictureBox7.Image = img7;
pictureBox8.Image = img8;
pictureBox9.Image = img9;

これがあなたのための画像の例です:

ここに画像の説明を入力

これは私のピクチャー パズル クラス プロジェクトの一部です。私はフォトショップの画像を処理しましたが、動的にカットしたいと考えています。

前もって感謝します。

4

2 に答える 2

19

まず、img1、img2 を使用する代わりに、サイズ 9 の配列を使用します。次に、次のようなループをいくつか使用すると、はるかに簡単にこれを行うことができます。

var imgarray = new Image[9];
var img = Image.FromFile("media\\a.png");
for( int i = 0; i < 3; i++){
  for( int j = 0; j < 3; j++){
    var index = i*3+j;
    imgarray[index] = new Bitmap(104,104);
    var graphics = Graphics.FromImage(imgarray[index]);
    graphics.DrawImage( img, new Rectangle(0,0,104,104), new Rectangle(i*104, j*104,104,104), GraphicsUnit.Pixel);
    graphics.Dispose();
  }
}

次に、次のようにボックスに入力できます。

pictureBox1.Image = imgarray[0];
pictureBox2.Image = imgarray[1];
...
于 2012-11-29T12:33:55.313 に答える
7

このコードで試すことができます。基本的に、(プロジェクトで必要なような) 画像のマトリックスを作成しBitmap、大きな画像の各適切な部分を描画します。pictureBoxesに使用して、それらをマトリックスに入れることができるのと同じ概念。

Image img = Image.FromFile("media\\a.png"); // a.png has 312X312 width and height
int widthThird = (int)((double)img.Width / 3.0 + 0.5);
int heightThird = (int)((double)img.Height / 3.0 + 0.5);
Bitmap[,] bmps = new Bitmap[3,3];
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    {
        bmps[i, j] = new Bitmap(widthThird, heightThird);
        Graphics g = Graphics.FromImage(bmps[i, j]);
        g.DrawImage(img, new Rectangle(0, 0, widthThird, heightThird), new Rectangle(j * widthThird, i * heightThird, widthThird, heightThird), GraphicsUnit.Pixel);
        g.Dispose();
    }
pictureBox1.Image = bmps[0, 0];
pictureBox2.Image = bmps[0, 1];
pictureBox3.Image = bmps[0, 2];
pictureBox4.Image = bmps[1, 0];
pictureBox5.Image = bmps[1, 1];
pictureBox6.Image = bmps[1, 2];
pictureBox7.Image = bmps[2, 0];
pictureBox8.Image = bmps[2, 1];
pictureBox9.Image = bmps[2, 2];
于 2012-11-29T12:41:59.570 に答える