C
C#11mo ago
SparkyCracked

✅ Winforms: open two forms at the same time

Good day everyone. So I have an issue (duhh, thats why I am here xD). I am trying to get a screenshot of the users image...I am using two forms to achieve this, one to show the image and another to screenshot. When I try open the first form AspectRatioForm and then the second SelectArea. It throws the issue below:
System.ArgumentException: 'Parameter is not valid.'
System.ArgumentException: 'Parameter is not valid.'
This is the code I wrote:
c#
public void openImageScreenshot(string imageLocationPath, PictureBox pictureBox)
{
SelectArea area = new SelectArea(this);

area.BringToFront();
area.Activate();

using (Image img = Image.FromFile(imageLocationPath))
{
AspectRatioForm aspectRatioForm = new AspectRatioForm(img);
aspectRatioForm.Show();
}
this.Hide();
area.ShowDialog();
// After showing and hiding the SelectArea form, you can access the file path:
string imagePathScreenshot = area.GetScreenshotFilePath();
pictureBox.ImageLocation = imagePathScreenshot;
this.Show();
}
c#
public void openImageScreenshot(string imageLocationPath, PictureBox pictureBox)
{
SelectArea area = new SelectArea(this);

area.BringToFront();
area.Activate();

using (Image img = Image.FromFile(imageLocationPath))
{
AspectRatioForm aspectRatioForm = new AspectRatioForm(img);
aspectRatioForm.Show();
}
this.Hide();
area.ShowDialog();
// After showing and hiding the SelectArea form, you can access the file path:
string imagePathScreenshot = area.GetScreenshotFilePath();
pictureBox.ImageLocation = imagePathScreenshot;
this.Show();
}
Now I don't know why this is happening because if I call them separately in terms of their own functions using ShowDialog they both show one by one (once one form closes the other will show).
3 Replies
SparkyCracked
SparkyCrackedOP11mo ago
So a thought just came to my mind on the following issue. What if I open the screenshot form inside the image form...
CoRoys
CoRoys11mo ago
Where exactly is the issue thrown?
SparkyCracked
SparkyCrackedOP11mo ago
It was thrown here:
c#
area.ShowDialog();
c#
area.ShowDialog();
I have adjusted the code a lot. I know do this: Call this function
c#
public void openImageDialog(string imageLocationPath, PictureBox picBox)
{
using (Image img = Image.FromFile(imageLocationPath))
{
AspectRatioForm aspectRatioForm = new AspectRatioForm(img, this, picBox);
aspectRatioForm.Show();
aspectRatioForm.take_screenshot(this, picBox);
aspectRatioForm.Dispose();
this.BringToFront();
}
}
c#
public void openImageDialog(string imageLocationPath, PictureBox picBox)
{
using (Image img = Image.FromFile(imageLocationPath))
{
AspectRatioForm aspectRatioForm = new AspectRatioForm(img, this, picBox);
aspectRatioForm.Show();
aspectRatioForm.take_screenshot(this, picBox);
aspectRatioForm.Dispose();
this.BringToFront();
}
}
Inside the aspectRatioForm.take_screenshot:
c#
public void take_screenshot(Form1 form, PictureBox pictureBox)
{
SelectArea area = new SelectArea(form);
area.BringToFront();
area.Activate();
area.ShowDialog();
// After showing and hiding the SelectArea form, you can access the file path:
string imagePathScreenshot = area.GetScreenshotFilePath();
pictureBox.ImageLocation = imagePathScreenshot;
}
c#
public void take_screenshot(Form1 form, PictureBox pictureBox)
{
SelectArea area = new SelectArea(form);
area.BringToFront();
area.Activate();
area.ShowDialog();
// After showing and hiding the SelectArea form, you can access the file path:
string imagePathScreenshot = area.GetScreenshotFilePath();
pictureBox.ImageLocation = imagePathScreenshot;
}
This no more throws the error of parameters. My guess was that when the aspectRatio form was opening, it would not keep the main form(this variable) and so it was raising that error So when you try pass the variable this in the original code here:
c#
SelectArea area = new SelectArea(this);
c#
SelectArea area = new SelectArea(this);
there were issues Ok cool my program works now as a whole. Goal: I was trying to open two forms, one to show the picture, another to take a screenshot of it. This was just to correct the aspect ratio of the image Issue: The forms were throwing a Parameter issue. Thought process: if it is a parameter issue, that means the variables were either becoming invalid/lost. Solution: follow the flow of code, open the window (1), and open the next window (2) from the first window (1). This will allow you to pass in the parameters needed for both windows.
Want results from more Discord servers?
Add your server