-1

我有两种表格,表格一处理图像压缩,表格二处理细节。细节如; 浏览位图、图像质量和保存文件名。

我目前遇到的问题是位图正在返回到表单一为空。这会产生错误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); }
        }

    }
}
4

1 回答 1

1

首先,您的错误是因为您将Bitmap对象存储在bitmp.. 但从bitmap1您的getBtmap财产返回。

getSomething此外,当它们还具有不接受值的设置访问器时,您已经调用了您的属性。这会让你感到困惑(你的变量名也会..考虑重命名它们)。

考虑一下:

frm.getFileName = "MY FILE NAME";

..不存储“我的文件名”..它存储文本框值。考虑改变这个逻辑。

于 2012-12-17T21:36:36.377 回答