2012-02-04 5 views
2

비트 맵의 ​​특정 영역을 얻는 방법이 있는지 궁금한 분들이 계십니까? 나는 tileset 커터를 만들기 위해 노력하고 있는데,로드 된 타일셋을 반복하고 이미지를 xscale * yscale 이미지로 잘라서 개별적으로 저장해야합니다. 나는 현재 절단 과정을 위해이 루프를 사용하고있다.비트 맵의 ​​특정 사각형 영역 가져 오기

  int x_scale, y_scale, image_width, image_height; 

     image_width = form1.getWidth(); 
     image_height = form1.getHeight(); 
     x_scale = Convert.ToInt32(xs.Text); 
     y_scale = Convert.ToInt32(ys.Text); 

     for (int x = 0; x < image_width; x += x_scale) 
     { 
      for (int y = 0; y < image_height; y += y_scale) 
      { 
       Bitmap new_cut = form1.getLoadedBitmap();//get the already loaded bitmap 


      } 
     } 

그래서 비트 맵 new_cut의 일부를 "선택"하고 그 부분을 저장할 수있는 방법이 있습니까?

답변

4

LockBits 메서드를 사용하면 비트 맵 직사각형 영역에 대한 설명을 얻을 수 있습니다. 비슷한 것

// tile size 
var x_scale = 150; 
var y_scale = 150; 
// load source bitmap 
using(var sourceBitmap = new Bitmap(@"F:\temp\Input.png")) 
{ 
    var image_width = sourceBitmap.Width; 
    var image_height = sourceBitmap.Height; 
    for(int x = 0; x < image_width - x_scale; x += x_scale) 
    { 
     for(int y = 0; y < image_height - y_scale; y += y_scale) 
     { 
      // select source area 
      var sourceData = sourceBitmap.LockBits(
       new Rectangle(x, y, x_scale, y_scale), 
       System.Drawing.Imaging.ImageLockMode.ReadOnly, 
       sourceBitmap.PixelFormat); 
      // get bitmap for selected area 
      using(var tile = new Bitmap(
       sourceData.Width, 
       sourceData.Height, 
       sourceData.Stride, 
       sourceData.PixelFormat, 
       sourceData.Scan0)) 
      { 
       // save it 
       tile.Save(string.Format(@"F:\temp\tile-{0}x{1}.png", x, y)); 
      } 
      // unlock area 
      sourceBitmap.UnlockBits(sourceData); 
     } 
    } 
} 
+0

감사합니다, 큰 작품 : 많이 배웠습니다! –

0

Graphics 개체의 SetClip 메서드를 사용하여 이미지 영역을 새로운 이미지로 클립 할 수 있습니다.

오버로드 중 일부는 이미지에서 클립 대상으로 사용할 경계 상자를 나타내는 Rectangle 구조체를 사용합니다.

관련 문제