C# 在.NET中提取图像的一部分

C# 在.NET中提取图像的一部分,c#,.net,vb.net,image,playing-cards,C#,.net,Vb.net,Image,Playing Cards,我有一个PNG的一副卡作为一个图像(它有所有52张卡合并成一个图像文件)。如何根据需要提取单个卡(或在启动时将它们全部提取到单独的图像文件中) 我理解“知道要获取哪一行和哪一列”的逻辑,这是我遇到麻烦的实际图像处理代码 我正在使用VisualStudio2010和VB(尽管任何.NET语言的示例代码都可以) 我不允许发布图片本身,但这里有一个示例图片 谢谢。这将加载原始版本并创建一个裁剪版本,从(0,0)开始,尺寸为100x100。您必须编写逻辑来迭代每个卡片,知道何时移动到下一行,等等。但是

我有一个PNG的一副卡作为一个图像(它有所有52张卡合并成一个图像文件)。如何根据需要提取单个卡(或在启动时将它们全部提取到单独的图像文件中)

我理解“知道要获取哪一行和哪一列”的逻辑,这是我遇到麻烦的实际图像处理代码

我正在使用VisualStudio2010和VB(尽管任何.NET语言的示例代码都可以)

我不允许发布图片本身,但这里有一个示例图片


谢谢。

这将加载原始版本并创建一个裁剪版本,从(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);

顺便说一句,我没有包括对card.Save()的调用,因为在保存过程中有很多选项需要设置,这有点超出了您的问题范围。然而,关于如何将其保存到磁盘的信息,.

我非常无聊,看看这是否有帮助

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);
        }
    }
}

请显示一些源代码。。。你试过什么?到底是什么不起作用?我的意思是它们由OP来测量。如果有人感兴趣,这里是等效的VB.NET代码Dim cards作为Bitmap=New Bitmap(“C:\tpm\card\u 4color.png”)Dim srrect作为Rectangle=New Rectangle(0,0,100,100)Dim card作为Bitmap=cards.Clone(srrect,cards.PixelFormat)谢谢Scott,代码运行得很好。有人能提供WinRT版本吗?
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();
        }
    }
}
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;
        }