Witam! Mam funkcję, która ma za zadanie zmieniać rozmiar obrazka. Najpierw wczytuję obrazek z pliku do pictureBox'a1, który należy do Frame1, potem wybieram z menu zmień rozmiar - tutaj pojawia się nowe okno(Frame2), w którym wpisuję w pola tekstowe nowe wymiary obrazka i zatwierdzam. W tym miejscu pojawia się problem, z którym nie umiem sobie dać rady - błąd taki jak poniżej a powodem jest tak linijka
Bitmap original = new Bitmap(bmp);
. Proszę o pomoc
Exception thrown: 'System.NullReferenceException' in System.Drawing.dll
An unhandled exception of type 'System.NullReferenceException' occurred in System.Drawing.dll
Additional information: Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
private void zmieńRozmiarToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap original = (Bitmap)pictureBox1.Image.Clone();
Rozmiar zmien = new Rozmiar();
zmien.ShowDialog();
}
public void ResizeImage(int targetWidth, int targetHeight, bool check)
{
//Image original;
Bitmap original = new Bitmap(bmp);
// original = pictureBox1.Image;
int oh = original.Height;
int ow = original.Width;
//Bitmap res;
int destWidth, destHeight;
int aspect = ow / oh;
Console.WriteLine("OW"+ ow + "\n" + "OH" + oh);
// Form1 f = new Form1();
destWidth = (int)(original.Width);
destHeight = (int)(original.Height);
Frame2
public partial class Rozmiar : Form
{
// CheckBox checkBox1;
//bool IsChecked { get { return checkBox1.Checked; } }
public Rozmiar()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
public bool OptionSelected
{
get { return checkBox1.Checked; }
set { checkBox1.Checked = value; }
}
private void ok_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
//Bitmap original = f.bmp;
int w, h, w_pr, h_pr, targetWidth, targetHeight;
bool check;
// w = int.Parse(textBox1.Text);
// h = int.Parse(textBox2.Text);
if (OptionSelected == true)
{
check = true;
}
else
{
check = false;
}
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text) && string.IsNullOrEmpty(textBox3.Text) &&
string.IsNullOrEmpty(textBox4.Text))
{
MessageBox.Show("Podaj wielkości w pixelach LUB w procentach!");
}
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text) && !string.IsNullOrEmpty(textBox3.Text) &&
!string.IsNullOrEmpty(textBox4.Text))
{
MessageBox.Show("Podaj wielkości w pixelach LUB w procentach!");
}
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text) && !string.IsNullOrEmpty(textBox3.Text) &&
!string.IsNullOrEmpty(textBox4.Text))
{
if (!int.TryParse(textBox3.Text, out w_pr) || !int.TryParse(textBox4.Text, out h_pr))
{
MessageBox.Show("Dozwolone WYŁĄCZNIE cyfry!");
}
else
{
w_pr = int.Parse(textBox3.Text);
h_pr = int.Parse(textBox4.Text);
targetWidth = w_pr;
targetHeight = h_pr;
f.ResizeImage(targetWidth, targetHeight, check);
}
}
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text) && string.IsNullOrEmpty(textBox3.Text) &&
string.IsNullOrEmpty(textBox4.Text))
{
if (!int.TryParse(textBox1.Text, out w) || !int.TryParse(textBox2.Text, out h))
{
MessageBox.Show("Dozwolone WYŁĄCZNIE cyfry!");
}
else
{
w =int.Parse(textBox1.Text);
h = int.Parse(textBox2.Text);
targetWidth = w;
targetHeight = h;
f.ResizeImage(targetWidth, targetHeight, check);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Close();
}
}