1
votes

I'm attempting to use a custom built .PNG file of letters from the alphabet to give my game some nice looking textual graphics. However, since the core functionality of my WinForms game works using minimal graphics (I have a timer set to fire a little animation routine that fades out the word "Guessed" when a user enters a word that they've already tried - this is done using DrawString()). Right now I'm trying to get DrawImage(Image, dstRect, srcRect, Unit) to work, and I have it set to run on form load.

    private void DrawLetters()
    {

        // Create image.
        Graphics letters = this.CreateGraphics();
        Image newImage = Image.FromFile(strPath + "Letter_Map.png");

        // Create rectangle for displaying image.
        Rectangle destRect = new Rectangle(25, 25, 80, 80);

        // Create rectangle for source image.
        Rectangle srcRect = new Rectangle(0, 0, 833, 833);
        GraphicsUnit units = GraphicsUnit.Pixel;

        // Draw image to screen.
        letters.DrawImage(newImage, destRect, srcRect, units);
    }

This is practically verbatim from the MSDN site. My form populates with an array of Labels right now to show the user the grid of letters needed for the game. Is it them being drawn that overwrites the custom rectangle? I want to replace them all with images eventually. I understand srcRect to be (x, y, width, height) so I gave it the full size of the 'spritesheet'. For dstRect I only want it to place a block 80x80px on the form starting around 25x, 25y.

For the heck of it I created a blank form and called my DrawLetters() function during that form load event (I copied the function into that Form's code). I saw nothing, though, so I am starting to get a bit confused. I may need some education about just HOW the drawing works in conjunction with forms and controls being drawn on screen.

EDIT This https://stackguides.com/questions/837423/render-a-section-of-an-image-to-a-bitmap-c-sharp-winforms was what initially got me going, but this user has a working knowledge of XNA and seems to be trying to combine that with WinForms. XNA would be overkill, I believe, for the simple text game I am trying to 'pretty up'.

1
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". - John Saunders
Thanks, John. I'll remember that for the next time. - armadadrive

1 Answers

2
votes

You need to override the forms OnPaintMethod to get access to the forms Graphics object which you can then use to display the image on the form.

If you want to display a portion of the image, you will need to use a different overload of DrawImage, as follows:

public partial class DrawImageDemo : Form
{
    public DrawImageDemo()
    {
        InitializeComponent();
    }

    private Image _sprites;
    public Image Sprites
    {
        get
        {
            if (_sprites == null)
            {
                _sprites = Image.FromFile("test.jpg");
            }
            return _sprites;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // The forms graphics object
        Graphics g = e.Graphics;

        // Portion of original image to display
        Rectangle region = new Rectangle(0, 0, 80, 80);

        g.DrawImage(Sprites, 25, 25, region, GraphicsUnit.Pixel);

        base.OnPaint(e);
    }

}