C
C#8mo ago
Frostyio

Winform my button to reset my picturebox and fill it in with a new generated image is not working

https://hatebin.com/cslhmvmnya - my code Lines 121-124 is the methos to reset the picturebox but Im unsure how to do this it uses a Graphic From Image, then uses imported from file images
13 Replies
SpReeD
SpReeD8mo ago
What does not work there? I can only assume when and what error there can be. For instance, you're not disposing the Image of the PictureBox, but the PictureBox box itself on Line 116, which leads to an exception trying to access a disposed object. Usually in WinForms you Clear the image, by disposing it first, since it's a common memory leak mistake. Do it with something like this:
PBX_MyImage.Image?.Dispose();
PBX_MyImage.Image = null;
PBX_MyImage.Image?.Dispose();
PBX_MyImage.Image = null;
Try not to dispose the Control, but the Bitmap object inside it.
Frostyio
FrostyioOP8mo ago
im not sure whats not working, i disposed and set the picturebox to null so it must be what is set on the picturebox that is not resetting itself somewhere between lines 73-88 I'd assume
SpReeD
SpReeD8mo ago
So, what is your desired result?
Frostyio
FrostyioOP8mo ago
that when i press the reset button i can change the images position without it keeping the original image
SpReeD
SpReeD8mo ago
Okay, what do you mean by "change the images position"?
Frostyio
FrostyioOP8mo ago
i made a way that the user can chose the x, y and size and im trying to make it possible to reset the image cause of original icon is still there afterword
SpReeD
SpReeD8mo ago
If you dispose and clear the picturebox's Image like I described it's fine.
Frostyio
FrostyioOP8mo ago
i did do that but it didnt work https://hatebin.com/jmidjpdkji this is the latest code
SpReeD
SpReeD8mo ago
There's no problem in GenerateCard and the image should save and clear the Picturebox by adding pboxGEN.Image = null; on line 116. After this, you can recreate an image and altering the x,y,width & height
Frostyio
FrostyioOP8mo ago
weird i tried it and still keeps original image over
leowest
leowest8mo ago
I would guess your dialogs are holding the images hostage at this point u dont dispose of a single dialog after u use them
Frostyio
FrostyioOP8mo ago
i'll look into it i disposed them, same result ok i got some new results, when in my program if I open file dialogs again and chose the same images and generate it again (without using my reset button) it removes the old Image and adds the new one but how come they keep my original image if I dont reopen file dialogs?
leowest
leowest8mo ago
I did some testing, the problem is that it keeps the stream alive so its always modifying the stream your loading in memory that's why it never wipes it. What u want to do is, use a Bitmap, and manipulate that bitmap. For example:
private Bitmap _bmp;
public Form1()
{
InitializeComponent();
_bmp = new Bitmap(pboxGEN.Width, pboxGEN.Height);
}

private void Form1_Load(object sender, EventArgs e)
{
pboxGEN.Image = _bmp;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
pboxGEN.Image = null;
_bmp.Dispose();
}
private Bitmap _bmp;
public Form1()
{
InitializeComponent();
_bmp = new Bitmap(pboxGEN.Width, pboxGEN.Height);
}

private void Form1_Load(object sender, EventArgs e)
{
pboxGEN.Image = _bmp;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
pboxGEN.Image = null;
_bmp.Dispose();
}
And
private void GenerateCard(object sender, EventArgs e)
{
if (cfCard == null)
{
MessageBox.Show("Background is invalid...");
return;
}

if (cardIcon == null)
{
MessageBox.Show("Icon is invalid...");
return;
}

if (!int.TryParse(textBoxIconX.Text, out var iconX))
{
MessageBox.Show("Icon position X is invalid...");
return;
}

if (!int.TryParse(textBoxIconY.Text, out var iconY))
{
MessageBox.Show("Icon position Y is invalid...");
return;
}

if (!int.TryParse(textBoxIconW.Text, out var iconW))
{
MessageBox.Show("Icon width is invalid...");
return;
}

if (!int.TryParse(textBoxIconH.Text, out var iconH))
{
MessageBox.Show("Icon height is invalid...");
return;
}

_bmp = new Bitmap(cfCard);
using var g = Graphics.FromImage(_bmp);
g.DrawImage(cardIcon, iconX, iconY, iconW, iconH);
pboxGEN.Image = _bmp;
pboxGEN.Invalidate();
}
private void GenerateCard(object sender, EventArgs e)
{
if (cfCard == null)
{
MessageBox.Show("Background is invalid...");
return;
}

if (cardIcon == null)
{
MessageBox.Show("Icon is invalid...");
return;
}

if (!int.TryParse(textBoxIconX.Text, out var iconX))
{
MessageBox.Show("Icon position X is invalid...");
return;
}

if (!int.TryParse(textBoxIconY.Text, out var iconY))
{
MessageBox.Show("Icon position Y is invalid...");
return;
}

if (!int.TryParse(textBoxIconW.Text, out var iconW))
{
MessageBox.Show("Icon width is invalid...");
return;
}

if (!int.TryParse(textBoxIconH.Text, out var iconH))
{
MessageBox.Show("Icon height is invalid...");
return;
}

_bmp = new Bitmap(cfCard);
using var g = Graphics.FromImage(_bmp);
g.DrawImage(cardIcon, iconX, iconY, iconW, iconH);
pboxGEN.Image = _bmp;
pboxGEN.Invalidate();
}
Want results from more Discord servers?
Add your server