2012-12-15 2 views
3

카드 한 장의 PNG가 1 이미지 (두 장의 이미지가 모두 하나의 이미지 파일로 결합되어 있습니다)입니다. 개별 카드를 필요한만큼 추출하거나 (예 : 시작할 때 개별 이미지 파일로 모두 추출) 어떻게합니까?. NET에서 이미지의 일부를 추출하십시오.

나는 어떤 행과 열을 가져야하는지 알 수있는 논리를 이해하고 있으며 실제 이미지 조작 코드는 문제가 있습니다.

Visual Studio 2010을 사용 중이며 VB를 사용하고 있습니다 (.NET 언어의 샘플 코드에서도 마찬가지입니다).

내가 여기에 이미지 자체를 게시 할 수 있지만하고는 예제 이미지

http://www.jfitz.com/cards/windows-playing-cards.png

감사합니다.

+1

일부 소스 코드를 보여주세요 ... 당신은 무엇을 시도? 정확히 작동하지 않는 것은 무엇입니까? – Yahia

답변

5

원본을로드하고 (0,0)에서 시작하여 크기가 100x100 인 자르기 버전을 만듭니다. 각 카드를 반복 할 논리를 작성하고 다음 행으로 이동할시기 등을 알아야합니다. 그러나 좌표와 치수를 알면 카드를 꺼내는 데 도움이됩니다. 거기에 저장하는 동안 설정하는 옵션이 많이 있습니다 그것은 질문의 범위를 외부에 약간 때문에

Bitmap cards = new Bitmap(@"C:\SomePath\"); 
Rectangle srcRect = new Rectangle(0, 0, 100, 100); 
Bitmap card = (Bitmap)cards.Clone(srcRect, cards.PixelFormat); 

BTW, 나는) card.Save (호출을 포함하지 않았다. 그러나 디스크에 저장하는 방법에 대한 정보는 see http://msdn.microsoft.com/en-us/library/ytz20d80.aspx을 참조하십시오.

+0

관심있는 사람이 있으면 여기를 클릭하십시오. 희미한 비트 맵 = 새 비트 맵 ("C : \ tpm \ card_4color.png") 희미한 srcRect 직사각형 = 새 직사각형 (0, 0, 100 , 100) 희박한 카드 As Bitmap = cards.Clone (srcRect, cards.PixelFormat) 감사합니다. Scott은 코드가 완벽하게 작동했습니다. – user1906484

+0

도와 주니 기쁘다. – Scott

+0

누군가 WinRT 버전을 제공 할 수 있습니까? – ezaspi

0

매우 지루했습니다. 도움이되는지 확인하십시오.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing; 

namespace CardDeck 
{ 
    public class CardCropper 
    { 
     private Bitmap _source; 
     private int _cardsPerRow; 
     private int _rowCount; 
     private int _cardCount; 
     private int _cardWidth; 
     private int _cardHeight; 

     public CardCropper(Bitmap source, int rowCount, int cardsPerRow) 
     { 
      _source = source; 
      _cardsPerRow = cardsPerRow; 
      _rowCount = rowCount; 
      _cardCount = _cardsPerRow * _rowCount; 
      _cardWidth = source.Width/_cardsPerRow; 
      _cardHeight = source.Height/_rowCount; 
     } 

     public Bitmap[,] CropCards() 
     { 
      var cards = new Bitmap[_rowCount, _cardsPerRow]; 

      for (int y = 0; y < _rowCount; y++) 
      { 
       for (int x = 0; x < _cardsPerRow; x++) 
       { 
        cards[y, x] = CropCard(x, y); 
       } 
      } 

      return cards; 
     } 

     private Bitmap CropCard(int x, int y) 
     { 
      var rect = new Rectangle(x * _cardWidth, y * _cardHeight, _cardWidth, _cardHeight); 

      return _source.Clone(rect, _source.PixelFormat); 
     } 
    } 

    public class CardDeck 
    { 
     private int _limit; 
     private int _cardsPerRow; 
     private int _index = 0; 
     private Bitmap[,] _cards; 

     public int Index 
     { 
      get 
      { 
       CheckLimits(); 
       return _index; 
      } 
      set 
      { 
       _index = value; 
       CheckLimits(); 
      } 
     } 

     public CardDeck(Bitmap[,] cards, int rowCount, int cardsPerRow) 
     { 
      _cards = cards; 
      _limit = rowCount * cardsPerRow; 
      _cardsPerRow = cardsPerRow;    
     } 


     public Image GetCardFromIndex() 
     { 
      var point = GetPointFromIndex(); 

      return _cards[point.Y, point.X]; 
     } 

     private void CheckLimits() 
     { 
      if (_index >= _limit) 
      { 
       _index = 0; 
      } 

      if (_index < 0) 
      { 
       _index = _limit - 1; 
      } 
     } 

     private Point GetPointFromIndex() 
     { 
      int x = this.Index % _cardsPerRow; 
      int y = this.Index/_cardsPerRow; 

      return new Point(x, y); 
     } 
    } 
} 

사용법 :

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace CardDeck 
{ 
    public partial class CardDeckForm : Form 
    { 
     private CardDeck _cardDeck = null; 

     public CardDeckForm() 
     { 
      InitializeComponent(); 

      LoadCards(); 

      ShowImage(); 
     } 

     private void LoadCards() 
     { 
      using (var source = new Bitmap(@"AllCards.png")) 
      { 
       var cards = new CardCropper(source, rowCount: 4, cardsPerRow: 13).CropCards(); 
       _cardDeck = new CardDeck(cards, rowCount: 4, cardsPerRow : 13); 
      } 
     } 

     private void NextButton_Click(object sender, EventArgs e) 
     { 
      _cardDeck.Index++; 
      ShowImage(); 
     } 

     private void PreviousButton_Click(object sender, EventArgs e) 
     { 
      _cardDeck.Index--; 
      ShowImage(); 
     } 

     private void ShowImage() 
     { 
      this.cardPictureBox.Image = _cardDeck.GetCardFromIndex(); 
     } 
    } 
} 
+1

Nullable 이유 s? – SimpleVar

+0

나는 그것들이 측정하기 위해 영업 중임을 의미했다. – CodeCaster

0
public static System.Drawing.Image CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight) 
     { 
      System.Drawing.Rectangle rect = new System.Drawing.Rectangle(cropX, cropY, cropWidth, cropHeight); 
      System.Drawing.Image cropped = bitmap.Clone(rect, bitmap.PixelFormat); 
      return cropped; 
     } 
관련 문제