我有两种表格,表格一处理图像压缩,表格二处理细节。细节如; 浏览位图、图像质量和保存文件名。
我目前遇到的问题是位图正在返回到表单一为空。这会产生错误Object reference not set to an instance of an object
问题出在哪里?我已经盯着屏幕太久了,另一双眼睛真的很有帮助。
代码表格1:
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);
}
代码表格2:
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); }
}
}
}