GDI+

T

Tommy Lang

Hi,
I need some help. I am trying to draw cards.
I am using DrawRectagle() to draw upp the outlining of the card. Then
I fill the card with white color FillRectangle(). Then with DrawString
I can write the cards name at the top and bottom of the card.
So far everything works great. I can draw up one, two or more cards
and it looks great. Just like I want it to.
But now to my problem...
The cards lacks the symbol(clubs, heart, spade, diamond) in the center
of the card. I've managed to draw up all the different symbols using
gdi+ with DrawPolygon. For ex diamonds:
****** ex code ******
//Create pens and brushes
Pen penBlack = new Pen(Color.Black);
Pen penRed = new Pen(Color.Red);
SolidBrush bsWhite = new SolidBrush(Color.White);
SolidBrush bsRed = new SolidBrush(Color.Red);

//Draw rectangle and fill it
Rectangle r = new Rectangle(this.Property_X, this.Property_Y,
this.Property_Width, this.Property_Height);
e.DrawRectangle(penBlack, new Rectangle(this.Property_X,
this.Property_Y, this.Property_Width, this.Property_Height));
e.FillRectangle(bsWhite, this.Property_X+1, this.Property_Y+1,
this.Property_Width-2, this.Property_Height-2);

//Write name on card in top and bottom, centered
Font TheFont = new Font("Arial", 8, FontStyle.Bold);
float CenterOfCard = this.Property_Width / 2;
SizeF StringSize = e.MeasureString(base.m_Namn, TheFont);
float StartPosition = CenterOfCard -(StringSize.Width / 2);
e.DrawString(base.m_Namn, TheFont, bsRed, this.Property_X +
StartPosition, this.Property_Y+5);
e.DrawString(base.m_Namn, TheFont, bsRed, this.Property_X +
StartPosition, this.Property_Y + (this.Property_Height - 20));

//Draw symbol diamond
Point[] point = new Point[]{new Point(79, 109),new Point(90, 95), new
Point(101, 109), new Point(101, 110),new Point(90, 123), new Point(79,
110), new Point(79, 109)};

e.DrawPolygon(penRed, point);
e.FillPolygon(bsRed, point);

//Dispose pens and brushes
penBlack.Dispose();
penRed.Dispose();
bsWhite.Dispose();
bsRed.Dispose();
****** code ******
This will draw the diamond symbol at the specified(point) place on a
form.
It works good as long as the card is in the upper left corner.
The way I have implemented it I can move the cards around on the
screen, with text and it still looks good.
But I dont get how to do the math with the symbols, since they have
many points.
When the sympol consists of many points (like clubs), how do I
calculate so that it always stays centered within the rectangle drawn
with DrawRectagle?

Hope that I explained it clearly.
Thanks Tommy
 
N

n!

When the sympol consists of many points (like clubs), how do I
calculate so that it always stays centered within the rectangle drawn
with DrawRectagle?

The graphics object supports a transformation matrix which you can use to
translate (amongst other things) your paths, so that they appear at a
different location. For simplistic transformations, the graphics object
provides support methods to make it easier for you:

using System.Drawing;
using System.Drawing.Drawing2D;

private void DrawMySymbol( Graphics graphics, float x, float y )
{
GraphicsState oldState = graphics.Save();

graphics.TranslateTransform( x, y );

// Draw symbol as usual here...

graphics.Restore( oldState );
}

Note there is a newsgroup specifically for questions regarding drawing with
the .NET framework (microsoft.public.dotnet.framework.drawing).

n!
 
G

gary hitch

If i were you i would forget about drawing the cards with GDI.

Unless you have some special reason for doing it this way.

I would use bitmaps of card images instead and "paint" them on the
rectangular card areas.



Tommy Lang said:
Hi,
I need some help. I am trying to draw cards.
I am using DrawRectagle() to draw upp the outlining of the card. Then
I fill the card with white color FillRectangle(). Then with DrawString
I can write the cards name at the top and bottom of the card.
So far everything works great. I can draw up one, two or more cards
and it looks great. Just like I want it to.
But now to my problem...
The cards lacks the symbol(clubs, heart, spade, diamond) in the center
of the card. I've managed to draw up all the different symbols using
gdi+ with DrawPolygon. For ex diamonds:
****** ex code ******
//Create pens and brushes
Pen penBlack = new Pen(Color.Black);
Pen penRed = new Pen(Color.Red);
SolidBrush bsWhite = new SolidBrush(Color.White);
SolidBrush bsRed = new SolidBrush(Color.Red);

//Draw rectangle and fill it
Rectangle r = new Rectangle(this.Property_X, this.Property_Y,
this.Property_Width, this.Property_Height);
e.DrawRectangle(penBlack, new Rectangle(this.Property_X,
this.Property_Y, this.Property_Width, this.Property_Height));
e.FillRectangle(bsWhite, this.Property_X+1, this.Property_Y+1,
this.Property_Width-2, this.Property_Height-2);

//Write name on card in top and bottom, centered
Font TheFont = new Font("Arial", 8, FontStyle.Bold);
float CenterOfCard = this.Property_Width / 2;
SizeF StringSize = e.MeasureString(base.m_Namn, TheFont);
float StartPosition = CenterOfCard -(StringSize.Width / 2);
e.DrawString(base.m_Namn, TheFont, bsRed, this.Property_X +
StartPosition, this.Property_Y+5);
e.DrawString(base.m_Namn, TheFont, bsRed, this.Property_X +
StartPosition, this.Property_Y + (this.Property_Height - 20));

//Draw symbol diamond
Point[] point = new Point[]{new Point(79, 109),new Point(90, 95), new
Point(101, 109), new Point(101, 110),new Point(90, 123), new Point(79,
110), new Point(79, 109)};

e.DrawPolygon(penRed, point);
e.FillPolygon(bsRed, point);

//Dispose pens and brushes
penBlack.Dispose();
penRed.Dispose();
bsWhite.Dispose();
bsRed.Dispose();
****** code ******
This will draw the diamond symbol at the specified(point) place on a
form.
It works good as long as the card is in the upper left corner.
The way I have implemented it I can move the cards around on the
screen, with text and it still looks good.
But I dont get how to do the math with the symbols, since they have
many points.
When the sympol consists of many points (like clubs), how do I
calculate so that it always stays centered within the rectangle drawn
with DrawRectagle?

Hope that I explained it clearly.
Thanks Tommy
 
M

Mike Kitchen

Hi Tommy

See either my article at codeproject

http://www.codeproject.com/csharp/drawcardscp1.asp

or my site for some help, (this is a work in progress).

http://www.publicjoe.f9.co.uk/csharp/card00.html

Cheers

Mike

Tommy Lang said:
Hi,
I need some help. I am trying to draw cards.
I am using DrawRectagle() to draw upp the outlining of the card. Then
I fill the card with white color FillRectangle(). Then with DrawString
I can write the cards name at the top and bottom of the card.
So far everything works great. I can draw up one, two or more cards
and it looks great. Just like I want it to.
But now to my problem...
The cards lacks the symbol(clubs, heart, spade, diamond) in the center
of the card. I've managed to draw up all the different symbols using
gdi+ with DrawPolygon. For ex diamonds:
****** ex code ******
//Create pens and brushes
Pen penBlack = new Pen(Color.Black);
Pen penRed = new Pen(Color.Red);
SolidBrush bsWhite = new SolidBrush(Color.White);
SolidBrush bsRed = new SolidBrush(Color.Red);

//Draw rectangle and fill it
Rectangle r = new Rectangle(this.Property_X, this.Property_Y,
this.Property_Width, this.Property_Height);
e.DrawRectangle(penBlack, new Rectangle(this.Property_X,
this.Property_Y, this.Property_Width, this.Property_Height));
e.FillRectangle(bsWhite, this.Property_X+1, this.Property_Y+1,
this.Property_Width-2, this.Property_Height-2);

//Write name on card in top and bottom, centered
Font TheFont = new Font("Arial", 8, FontStyle.Bold);
float CenterOfCard = this.Property_Width / 2;
SizeF StringSize = e.MeasureString(base.m_Namn, TheFont);
float StartPosition = CenterOfCard -(StringSize.Width / 2);
e.DrawString(base.m_Namn, TheFont, bsRed, this.Property_X +
StartPosition, this.Property_Y+5);
e.DrawString(base.m_Namn, TheFont, bsRed, this.Property_X +
StartPosition, this.Property_Y + (this.Property_Height - 20));

//Draw symbol diamond
Point[] point = new Point[]{new Point(79, 109),new Point(90, 95), new
Point(101, 109), new Point(101, 110),new Point(90, 123), new Point(79,
110), new Point(79, 109)};

e.DrawPolygon(penRed, point);
e.FillPolygon(bsRed, point);

//Dispose pens and brushes
penBlack.Dispose();
penRed.Dispose();
bsWhite.Dispose();
bsRed.Dispose();
****** code ******
This will draw the diamond symbol at the specified(point) place on a
form.
It works good as long as the card is in the upper left corner.
The way I have implemented it I can move the cards around on the
screen, with text and it still looks good.
But I dont get how to do the math with the symbols, since they have
many points.
When the sympol consists of many points (like clubs), how do I
calculate so that it always stays centered within the rectangle drawn
with DrawRectagle?

Hope that I explained it clearly.
Thanks Tommy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top