C
C#3mo 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
SpReeD3mo 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
Frostyio3mo 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
SpReeD3mo ago
So, what is your desired result?
Frostyio
Frostyio3mo ago
that when i press the reset button i can change the images position without it keeping the original image
SpReeD
SpReeD3mo ago
Okay, what do you mean by "change the images position"?
Frostyio
Frostyio3mo 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
SpReeD3mo ago
If you dispose and clear the picturebox's Image like I described it's fine.
Frostyio
Frostyio3mo ago
i did do that but it didnt work https://hatebin.com/jmidjpdkji this is the latest code
SpReeD
SpReeD3mo 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
Frostyio3mo ago
weird i tried it and still keeps original image over
leowest
leowest3mo 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
Frostyio3mo 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
leowest3mo 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
More Posts
VS Project config questions1. Is it bad to enable "Place solution and project in the same folder"? It sounds neater to me but w(Blazor) I'm having trouble accessing a variable inside a parent component.I'm trying to access a simple bool in a parent component that shows when my side bar has been collap✅ There is already an open DataReader associated with this Command which must be closed firstI'm currently developing a Point Of Sale software in winforms c#. While working on cash management mC# Integration with MulesoftWe are looking to re-do an application in microservice style and biz req is pushing Mulesoft for thaVisual Studio opening search results in the search dock, not main windowKinda unrelated to C#, but since a few updates ago Visual Studio started behaving weirdly for me. Logic on properly deploying thingsSo I've been getting into a more professional workflow (id say) with better github practices, workinSystem.AggregateException: 'Some services are not able to be constructedI stuck with this problem and don't understand how to fix it: InvalidOperationException: Unable to rMAUI. How to remove the Title Bar?I need to remove the top panel so that the user can not use the buttons on it.Help me sort out perhaps badly done folder/solution structure so I can add a shared Class LibraryOk, so I have two solutions which must be separate so I can execute them both at the same time so thUsing Roslyn in WebAssembly, error creating an AdHocWorkspaceHello, this could be a really convoluted use case and I don't know what sort of support is expected