C#C
C#3y ago
6 replies
Zura

✅ First time using System.Drawing, any idea on why it's not rendering?

using System;
using System.Drawing;
using System.Windows.Forms;

namespace CTRL
{
    public partial class Form1 : Form
    {
        private Bitmap playerTexture;

        public Form1()
        {
            InitializeComponent();

            playerTexture = LoadImage("assets/player.png");

            if (playerTexture != null)
            {
                using (Graphics g = this.CreateGraphics())
                {
                    RenderImage(g, playerTexture, 0, 0, 50, 50);
                }
            }
        }

        private Bitmap? LoadImage(string filePath)
        {
            try
            {
                return new Bitmap(filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error loading image: {ex.Message}");
                return null;
            }
        }

        private void RenderImage(Graphics g, Bitmap image, int x, int y, int width, int height)
        {
            if (image != null)
            {
                Rectangle destinationRect = new Rectangle(x, y, width, height);
                g.DrawImage(image, destinationRect);
            }
        }
    }
}

Image is in the correct file location, it just doesn't render. any idea why?
Was this page helpful?