Cards.dll - display in picturebox
<p>I've been working on personal project to utilize cards.dll to display selected cards on a Form.  Starting with the 2003 article by Mike Kitchen, I have implemented code that does the display directly on the Form.</p>

<p>I would like to alter the code to draw the card display to a bitmap and then have the bitmap displayed in a picturebox on the form in response to a button click.  I do not want the card display drawn directly on the Form. Once I have the bitmap, transferring to the picturebox appears relatively trivial - "pictureBox1.Image=bmp".</p>

<p>My problem is how to draw the cards to a bitmap instead of directly to Form.</p>

<p>My current code is below:</p>

<p>Later Addition:  Solution was trivial--</p>

<p>Create pictureBox_Paint method</p>

<pre><code>Graphics g = pictureBox.CreateGraphics();
g=Graphics.FromImage(bmp);
pbhdc=g.GetHdc();

cardHandle.drawCardBack(pbhdc,x,y,pattern);

g.Release(pbhdc);
g.Dispose();
</code></pre>

<p>Under button_click method</p>

<pre><code>pictureBox.Image=bmp
</code></pre>

<p>voila! it works and is persistent.</p>

<pre><code> using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Pcardstest
{

    public partial class Form1 : Form
    {
        private Pcardstest.cardsdll cardHandle;
        private IntPtr hdc = new IntPtr();
        int icardct;
        int icardnum;
        int[] arcardnumtot;
        Card card = new Card();
        List&lt;Card&gt; cardlist = new List&lt;Card&gt;();
        Random rand;

        public Form1()
        {
            InitializeComponent();
            cardHandle = new Pcardstest.cardsdll();
            arcardnumtot = new int[52];
            this.Text = "Card Display";
            this.Size = new Size(600, 600);
        }
        // Override Form's OnPaint method
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics x = e.Graphics;

            hdc = x.GetHdc();
            x.ReleaseHdc(hdc);
            Deal();
            Draw();
        }

        //Deal and sort 11 cards
        private void Deal()
        {
            for (int ndex = 0; ndex &lt; 52; ndex++)
            {
                arcardnumtot[ndex] = 0;
            }
            icardct = 0;
            icardnum = 4;
            rand = new Random((int)DateTime.Now.Ticks);
            while (icardct &lt; 11)
            {
                icardnum = rand.Next(52);
                if ((icardnum &lt; 4) || (icardnum &gt; 31))  //ensure cards only 9 thru A
                {
                    if (arcardnumtot[icardnum] &lt; 2) //ensure no more than two of any card
                    {
                        arcardnumtot[icardnum] = arcardnumtot[icardnum] + 1;
                        Card card = new Card();
                        cardlist.Add(new Card(icardnum, icardnum % 4, icardnum / 4));
                        icardct = icardct + 1;
                    }
                    for (int index = 0; index &lt; cardlist.Count; index++)    //temporarily change rank of A to rank above king
                    {                                                       //for display sorting purposes
                        if (cardlist[index].CardRank == 0)
                        {
                            cardlist[index].CardRank = 14;
                        }
                    }
                    for (int index = 0; index &lt; cardlist.Count; index++)    //temporarily change rank of 10 to rank above king
                    {                                                       //for display sorting purposes
                        if (cardlist[index].CardRank == 9)
                        {
                            cardlist[index].CardRank = 13;
                        }
                    }
                    cardlist.Sort
                    (
                        delegate(Card p1, Card p2)
                        {
                            int comparesuit = p1.CardSuit.CompareTo(p2.CardSuit);  //sort ascending on suit
                            if (comparesuit == 0)
                            {
                                return p2.CardRank.CompareTo(p1.CardRank);  //sort descending on rank
                            }
                            return comparesuit;
                        }
                    );
                    for (int index = 0; index &lt; cardlist.Count; index++)      //restore actual rank to A
                    {
                        if (cardlist[index].CardRank == 14)
                        {
                            cardlist[index].CardRank = 0;
                        }
                    }
                    for (int index = 0; index &lt; cardlist.Count; index++)    //restore actual rank to 10
                    {
                        if (cardlist[index].CardRank == 13)
                        {
                            cardlist[index].CardRank = 9;
                        }
                    }
                }
            }
        }

        // Draw card
        private void Draw()
        {
            cardHandle.drawCard(hdc, 25, 10, cardlist[0].CardNum,0,0);
            cardHandle.drawCard(hdc, 50, 10, cardlist[1].CardNum,0,0);
            cardHandle.drawCard(hdc, 75, 10, cardlist[2].CardNum,0,0);
            cardHandle.drawCard(hdc, 100, 10, cardlist[3].CardNum,0,0);
            cardHandle.drawCard(hdc, 125, 10, cardlist[4].CardNum,0,0);
            cardHandle.drawCard(hdc, 150, 10, cardlist[5].CardNum,0,0);
            cardHandle.drawCard(hdc, 175, 10, cardlist[6].CardNum,0,0);
            cardHandle.drawCard(hdc, 200, 10, cardlist[7].CardNum,0,0);
            cardHandle.drawCard(hdc, 225, 10, cardlist[8].CardNum,0,0);
            cardHandle.drawCard(hdc, 250, 10, cardlist[9].CardNum,0,0);
            cardHandle.drawCard(hdc, 275, 10, cardlist[10].CardNum,0,0);
        }
    }
    public enum eSUIT : int
    {
        CLUBS = 0,
        DIAMOND = 1,
        HEARTS = 2,
        SPADES = 3
    }

    public enum eRank : int
    {
        ACE = 0,
        TWO = 1,
        THREE = 2,
        FOUR = 3,
        FIVE = 4,
        SIX = 5,
        SEVEN = 6,
        EIGHT = 7,
        NINE = 8,
        TEN = 9,
        JACK = 10,
        QUEEN = 11,
        KING = 12
    }

    public enum eBACK : int
    {
        CROSSHATCH = 53, /* XP = CROSSHATCH */
        WEAVE1 = 54, /* XP = SKY */
        WEAVE2 = 55, /* XP = MINERAL */
        ROBOT = 56, /* XP = FISH */
        FLOWERS = 57, /* XP = FROG */
        VINE1 = 58, /* XP = MOONFLOWER */
        VINE2 = 59, /* XP = ISLAND */
        FISH1 = 60, /* XP = SQUARES */
        FISH2 = 61, /* XP = MAGENTA */
        SHELLS = 62, /* XP = SANDDUNES */
        CASTLE = 63, /* XP = SPACE */
        ISLAND = 64, /* XP = LINES */
        CARDHAND = 65, /* XP = TOYCARS */
        UNUSED = 66, /* XP = UNUSED */
        THE_X = 67, /* XP = THE_X */
        THE_O = 68  /* XP = THE_0 */
    }

    public class cardsdll
    {
        /*************************************************************************
        * Interface to cards.dll                                                 *
        *************************************************************************/

        [DllImport("cards.dll")]
        private static extern bool cdtInit(ref int width, ref int height);

        [DllImport("cards.dll")]
        private static extern void cdtTerm();

        [DllImport("cards.dll")]
        private static extern bool cdtDraw(IntPtr hdc, int x, int y, int card, int mode, long color);

        [DllImport("cards.dll")]
        private static extern bool cdtDrawExt(IntPtr hdc, int x, int y, int dx, int dy, int card, int mode, long color);

        [DllImport("cards.dll")]
        private static extern bool cdtAnimate(IntPtr hdc, int cardback, int x, int y, int frame);

        /*************************************************************************
        * Constant declarations                                                  *
        *************************************************************************/

        /* mode parameters */
        const int mdFaceUp = 0; /* Draw card face up */
        const int mdFaceDown = 1; /* Draw card face down */
        const int mdHilite = 2; /* Same as FaceUp except drawn inverted */
        const int mdGhost = 3; /* Draw a ghost card -- for ace piles */
        const int mdRemove = 4; /* draw background specified by color */
        const int mdInvisibleGhost = 5; /* ? */
        const int mdDeckX = 6; /* Draw X */
        const int mdDeckO = 7; /* Draw O */

        /*************************************************************************
        * Public Interface                                                       *
        *************************************************************************/

        public cardsdll()
        {
            int width = 71;
            int height = 95;
            if (!cdtInit(ref width, ref height))
                throw new Exception("cards.dll did not load");
        }
        public void Dispose()
        {
            cdtTerm();
        }
        public bool drawCard(IntPtr hdc, int x, int y, int card, int mode, long color)
        {
            return cdtDraw(hdc, x, y, card, mode, color);
        }
        public bool drawCardBack(IntPtr hdc, int x, int y, eBACK back)
        {
            return cdtDraw(hdc, x, y, (int)back, mdFaceDown, 0);
        }
        public bool drawAnimatedBack(IntPtr hdc, int x, int y, int card, int frame)
        {
            return cdtAnimate(hdc, card, x, y, frame);
        }
        public bool drawInvertedCard(IntPtr hdc, int x, int y, int card)
        {
            return cdtDraw(hdc, x, y, card, mdHilite, 0);
        }
        public bool drawEmptyCard(IntPtr hdc, int x, int y, long color)
        {
            return cdtDraw(hdc, x, y, 1, mdGhost, color);
        }
        public bool drawExtrudedCard(IntPtr hdc, int x, int y, int dx, int dy, int card, int mode, long color)
        {
            return cdtDrawExt(hdc, x, y, dx, dy, card, mode, color);
        }
        public static eSUIT getSuit(int card)
        {
            int suit = card % 4;
            return (eSUIT)suit;
        }
        public static eRank getRank(int card)
        {
            int rank = card / 4;
            return (eRank)rank;
        }
        internal void drawCard(IntPtr hdc, int p, int p_3, int p_4, int p_5)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        internal void drawEmptyCard(IntPtr hdc, int p, int p_3, int p_4, int p_5)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

    public class Card
    {
        public Card()
        {
        }
        public Card(int cardnum, int cardsuit, int cardrank)
        {
            this.p_cardnum = cardnum;
            this.p_cardsuit = cardsuit;
            this.p_cardrank = cardrank;
        }
        private int p_cardnum = 0;
        private int p_cardsuit = 0;
        private int p_cardrank = 0;

        public int CardNum
        {
            get
            {
                return p_cardnum;
            }
            set
            {
                p_cardnum = value;
            }
        }
        public int CardSuit
        {
            get
            {
                return p_cardsuit;
            }
            set
            {
                p_cardsuit = value;
            }
        }
        public int CardRank
        {
            get
            {
                return p_cardrank;
            }
            set
            {
                p_cardrank = value;
            }
        }
    }
}
</code></pre>
