I have two Forms, Form1 which is the main Form and Form2 a secondary Form. Through a Button in Form1 I open Form2 and I pass an Integer and a Bitmap from Form1 to Form2. In Form2 I change the Bitmap based on what that Integer's value is. I display the changed Bitmap in Form2 and with a Button press I pass the changed Bitmap to Form1 and close Form2.
What I am having trouble with is getting that changed Bitmap back to Form1. Can't seem to find a way to do this.
In Form1
private void Button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ReceiveData(integer); //this to pass the integer
f2.Linear(bmp); //this to pass the Bitmap
f2.ShowDialog();
}
In Form 2
int integer_f2;
internal void ReceiveData(int integer)
{
integer_f2 = integer;
}
public void Linear(Bitmap bmp)
{
//-- ALGORITH THAT CHANGES THE IMAGES HERE (sample for testing purposes)--
Bitmap bmp_f2 = new Bitmap(bmp.Width, bmp.Height);
for (int x = 0; x < bmp.Width; x )
{
for (int y = 0; y < bmp.Height; y )
{
// basically fill bmp2 with whatever color integer_f2 represents
Color clr = Color.FromArgb(255, integer_f2, integer_f2, integer_f2);
bmp_f2.SetPixel(x, y, clr);
}
}
}
private void Button_Cancel_2_Click(object sender, EventArgs e)
{
Close();
}
private void Button_OK_2_Click(object sender, EventArgs e)
{
//-- PASS THE CHANGED BITMAP BACK TO FORM1 HERE --
Close();
}
CodePudding user response:
In Form2,
private class Form2 : Form
{
private Bitmap _bmp;
public Bitmap bmpResult { get; private set; } = null;
public void Linear(Bitmap bmp)
{
//-- ALGORITHM THAT CHANGES THE BITMAP HERE --
_bmp = bmp;
// Do what you need to here.
}
private void Button_Cancel_2_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
private void Button_OK_2_Click(object sender, EventArgs e)
{
bmpResult = _bmp;
DialogResult = DialogResult.OK;
}
}
and then in Form1,
private void Button1_Click(object sender, EventArgs e)
{
Bitmap bmp = null;
using (Form2 f2 = new Form2())
{
f2.ReceiveData(integer); //this to pass the integer
f2.Linear(bmp); //this to pass the Bitmap
if (f2.ShowDialog() == DialogResult.OK)
{
bmp = f2.bmpResult;
//or do whatever you want with the result
}
}
}
CodePudding user response:
One approach would be to overload the ShowDialog
method of Form2
to accept the required arguments. Form2 should also have a public property e.g. ChangedBitmap
that receives the result of the transform based on the integer.
public partial class Form2 : Form
{
public Form2() => InitializeComponent();
public DialogResult ShowDialog(IWin32Window owner, Bitmap bitmapIn, int integer)
{
// Set bitmap based on integer
switch (integer % 2)
{
case 0: ChangedBitmap = transform0(bitmapIn); break;
case 1: ChangedBitmap = transform1(bitmapIn); break;
}
// Call base class version
return base.ShowDialog(owner);
}
private Bitmap transform0(Bitmap bitmapIn) =>
ChangedBitmap = (Bitmap)pictureBoxLeft.Image; // Mock result
private Bitmap transform1(Bitmap bitmapIn) =>
ChangedBitmap = (Bitmap)pictureBoxRight.Image; // Mock result
public Bitmap ChangedBitmap { get; private set; }
private void buttonOK_Click(object sender, EventArgs e) =>
DialogResult = DialogResult.OK;
private void buttonCancel_Click(object sender, EventArgs e) =>
DialogResult = DialogResult.Cancel;
}
MainForm
looks at the DialogResult
to determine whether to use ChangedBitmap
.
public partial class MainForm : Form
{
public MainForm() => InitializeComponent();
private readonly Form2 _form2 = new Form2();
private int _testCount = 0;
// Check the result
private void buttonShowForm2_Click(object sender, EventArgs e)
{
var bitmapOut = (Bitmap)pictureBox1.Image;
switch (_form2.ShowDialog(this, bitmapOut, _testCount))
{
case DialogResult.OK:
// Answer to: "getting that changed Bitmap back to Form1"
pictureBox1.Image = _form2.ChangedBitmap;
_testCount ;
break;
case DialogResult.Cancel:
break;
}
}
// In MainForm.Designer.cs
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
// Modal dialog requires disposal when done.
_form2.Dispose();
}
// Modal dialog requires dispose when done.
base.Dispose(disposing);
}
}