при печати исключение ссылка на объект не установлена ​​на экземпляр объекта - PullRequest
0 голосов
/ 20 декабря 2018

Я работал над настольным приложением с c # 6.0, мое приложение должно распечатать изображение и информационную страницу.

Проблема заключается в том, что при запуске приложения из Visual Studio 2015 изображение ипечать информации в порядке.Когда я экспортирую файл app.exe и запускаю его, изображение все еще в порядке и напечатано.Но когда я пытаюсь распечатать информацию, он выдает исключение «ссылка на объект не установлена ​​на экземпляр объекта»

Мой код приведен ниже:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            StringFormat drawFormat = new StringFormat();
            drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;

            // start Drawingn logo
            //storing the path of the project in variable startDirectory which ends to open project folder wherevere it is
            var startDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
            string logopath = startDirectory + "/AddStudent/bin/Debug/pictures/logo.png";

            Bitmap bm = new Bitmap(logopath);
            Rectangle destRect = new Rectangle(320, 0, 200, 200);
            Rectangle srcRect = new Rectangle(0, 0, 1000, 1000);
            GraphicsUnit srcUnit = GraphicsUnit.Pixel;
            e.Graphics.DrawImage(bm, destRect, srcRect, srcUnit);
            // ChangeOpacity(bm, 0.5F);
            bm.Dispose();

            // Create pen.
            Pen black_Pen = new Pen(Color.Black, 2);
            // Create points that define line.
            PointF point_1 = new PointF(30.0F, 130.0F);//(x,y)
            PointF point_2 = new PointF(800.0F, 130.0F);//(length,startpoint)
            e.Graphics.DrawLine(black_Pen, point_1, point_2);


            // height increas by 35 
            e.Graphics.DrawString("معلومات الطالب", new Font("Tahoma", 18, FontStyle.Bold), Brushes.Black, new Point(520, 130), drawFormat);
            // start basic information
            int a = 185;
            e.Graphics.DrawString(" الاسم ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, a), drawFormat);
            e.Graphics.DrawString(txtName.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, a),drawFormat);
            e.Graphics.DrawString(" رقم القيد ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(400, a), drawFormat);
            e.Graphics.DrawString(txtEnIdNumber.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(200, a), drawFormat);
            int b = 220;
            e.Graphics.DrawString(" تولد ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, b), drawFormat);
            e.Graphics.DrawString(txtDateTime.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, b), drawFormat);
            e.Graphics.DrawString(" الجنس ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(400, b), drawFormat);
            e.Graphics.DrawString(cmbGender.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(200, b), drawFormat);
            int c = 255;
            e.Graphics.DrawString(" المرحلة ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, c), drawFormat);
            e.Graphics.DrawString(cmbStage.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, c), drawFormat);
            e.Graphics.DrawString(" الشعبة ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(400, c), drawFormat);
            e.Graphics.DrawString(cmbSection.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(200, c), drawFormat);
            // end basic information

            // start other info
            int d = 355;
            e.Graphics.DrawString(" وظيفة الام ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, d), drawFormat);
            e.Graphics.DrawString(txtMJ.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, d), drawFormat);
            e.Graphics.DrawString(" الام على قيد الحياة ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(400, d), drawFormat);
            e.Graphics.DrawString(cmbMother.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(200, d), drawFormat);
            int f = 390;
            e.Graphics.DrawString(" وظيفة الاب ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, f), drawFormat);
            e.Graphics.DrawString(txtFJ.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, f), drawFormat);
            e.Graphics.DrawString(" الاب على قيد الحياة ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(400, f), drawFormat);
            e.Graphics.DrawString(cmbFather.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(200, f), drawFormat);
            int g = 425;
            e.Graphics.DrawString(" يسكن مع ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, g), drawFormat);
            e.Graphics.DrawString(cmbLive.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, g), drawFormat);
            e.Graphics.DrawString(" عدد افراد الاسرة ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(400, g), drawFormat);
            e.Graphics.DrawString(txtFamilyNum.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(200, g), drawFormat);
            int h = 460;
            e.Graphics.DrawString(" الهوايات ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, h), drawFormat);
            e.Graphics.DrawString(cmbHobbies.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, h), drawFormat);
            e.Graphics.DrawString(" الامراض المزمنة ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(400, h), drawFormat);
            e.Graphics.DrawString(cmbDiseases.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(200, h), drawFormat);
            // end other info

            // start extra info
            int i = 560;
            e.Graphics.DrawString(" مدينة ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, i), drawFormat);
            e.Graphics.DrawString(txtCity.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, i), drawFormat);
            e.Graphics.DrawString(" منطقة ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, i+35), drawFormat);
            e.Graphics.DrawString(txtArea.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, i+35), drawFormat);
            int k = 630;
            e.Graphics.DrawString(" نقطة دالة ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, k), drawFormat);
            e.Graphics.DrawString(txtNear.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, k), drawFormat);

            int l = 790;
            e.Graphics.DrawString(" رقم الاب ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, l), drawFormat);
            e.Graphics.DrawString(txtPhone2.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, l), drawFormat);
            e.Graphics.DrawString(" رقم الام ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, l+35), drawFormat);
            e.Graphics.DrawString(txtPhone1.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(670, l+35), drawFormat);

            int s = 890;
            e.Graphics.DrawString(" مشاكل صحية اخرى ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, s), drawFormat);
            e.Graphics.DrawString(rtxHealth.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(580, s), drawFormat);
            int z = 945;
            e.Graphics.DrawString(" ملاحظات ", new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(800, z), drawFormat);
            e.Graphics.DrawString(rtxNote.Text, new Font("Tahoma", 16, FontStyle.Regular), Brushes.Black, new Point(580, z), drawFormat);
            // end 

            // report finishing
            var date = DateTime.Now.ToString("dd/MM/yyyy");
            // Create pen.
            Pen blackPen = new Pen(Color.Black, 3);
            // Create points that define line.
            PointF point1 = new PointF(30.0F, 1100.0F);//(x,y)
            PointF point2 = new PointF(800.0F, 1100.0F);//(length,startpoint)
            e.Graphics.DrawLine(blackPen, point1, point2);
            e.Graphics.DrawString("تقرير معلومات الطالب", new Font("Tahoma", 12, FontStyle.Regular), Brushes.DarkBlue, new Point(800, 1100), drawFormat);
            e.Graphics.DrawString(date, new Font("Tahoma", 12, FontStyle.Regular), Brushes.DarkBlue, new Point(800, 1125), drawFormat);

        }

 private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDialog pd = new PrintDialog();
            PrintDocument doc = new PrintDocument();
            doc.PrinterSettings.CreateMeasurementGraphics();
            doc.PrintPage += printDocument1_PrintPage;
            pd.Document = doc;
            if (pd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    doc.Print();
                }
                catch (Exception err) { MessageBox.Show(err.Message); }
            }//"الطابعة التي اخترتها غير معرفة"
            printPreviewDialog1.ShowDialog();

        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...