I have two forms, form one handles the image compression and form two handles the details.
Details like; browse for a bitmap, image quality and a save file name.
The problem i am having at the moment is the bitmap is returning to form one as null.
This then produces the error Object reference not set to an instance of an object
Where abouts is the problem?
Ive been staring at the screen too long and another pair of eyes could really help.
Code Form1:
public void compressImg(Bitmap bitmp, string fileName, int quality)
{
//string fileName = Microsoft.VisualBasic.Interaction.InputBox("Enter a file name:", "Save File", "JPEGCompress", 250, 250);
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
ici = codec;
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
bitmp.Save("C:\\" + fileName + ".jpg", ici, ep);
}
private void compressAnImageToolStripMenuItem_Click(object sender, EventArgs e)
{
string fileName;
int quality;
Bitmap bitmp;
Form2 f2 = new Form2();
f2.ShowDialog();
fileName = f2.getFileName;
quality = f2.getQuality;
bitmp = f2.getBtmap;
//Bitmap newbitmp = new Bitmap(bitmp);
compressImg(bitmp, fileName, quality);
}
Code Form2:
public partial class Form2 : Form
{
public static string fileName;
public static int quality;
public Bitmap bitmp, bitmap1;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
public string getFileName
{
get { return fileName; }
set { fileName = txtFileName.Text; }
}
public int getQuality
{
get { return quality; }
set { quality = imgQualTrkBar.Value; }
}
private void BtnSubmitInfo_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog2.ShowDialog() == DialogResult.OK)
{
bitmp = new Bitmap(openFileDialog2.FileName);
}
}
public Bitmap getBtmap
{
get { return bitmap1; }
set { bitmap1 = new Bitmap(bitmp); }
}
}
}
Firstly, your error is because you’re storing your
Bitmapobject inbitmp.. but returningbitmap1from yourgetBtmapproperty.Also, you’ve called your properties
getSomethingwhen they also have set accessors that don’t accept a value. This will confuse you (as will your variable names.. consider renaming them too).Consider this:
..that doesn’t store “MY FILE NAME”.. it stores the textbox value. Consider changing this logic.