C
C#11mo ago
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);
}
}
}
}
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?
3 Replies
Buddy
Buddy11mo ago
Create a new element, and override OnPaint method Do not paint in the constructor https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.onpaint?view=windowsdesktop-8.0#examples
zura
zuraOP11mo ago
Thank you. Appreciate it, worked great
Buddy
Buddy11mo ago
blobthumbsup
Want results from more Discord servers?
Add your server