2012-11-29 2 views
9

가능한 중복을 이미지 컷 :
Image splitting into 9 pieces9 개 조각으로 C#

내가 충분히 봤하지만 불행히도 도움을 찾지 못했습니다하지만. 이 Code Project Tutorial도 실제로 필요한 서비스를 제공하지 못했습니다.

저는 WinForm에 이미지와 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; 

당신을위한 예제 이미지입니다 :

enter image description here

이 내 그림 퍼즐 클래스 프로젝트의 일부입니다. 나는 포토샵 이미지로 해왔고, 지금은 동적으로 자르고 싶다.

미리 감사드립니다.

답변

13

첫째, 대신 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]; 
... 
7

이 코드로 시도해보십시오. 기본적으로 이미지 매트릭스를 만들고 (프로젝트에서 필요로하는 것처럼) 큰 이미지의 각 부분을 적절하게 그립니다. 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]; 
관련 문제