错误:对象引用未设置为对象的实例。 [英] Error: Object reference not set to an instance of an object.

查看:88
本文介绍了错误:对象引用未设置为对象的实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次发帖问题所以如果我不遵循任何主题或任何错误请原谅我...



我收到此错误我的代码:

对象引用未设置为对象的实例。



这是我的代码:



  void  document_PrintPage( object  sender,PrintPageEventArgs e)
{

e.Graphics.DrawImage(_memoryImage, 0 0 );

}
private 位图_memoryImage;

private void CaptureScreen()
{
// 使用构造因为图形对象不
// 在离开方法范围时自动处理
使用 var myGraphics = CreateGraphics())
{
var s = Size;
_memoryImage = new 位图( 1500 1000 ,myGraphics);
使用 var memoryGraphics = Graphics.FromImage(_memoryImage))
{
printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize( PaperA4 826 1169 );
memoryGraphics.CopyFromScreen(Location.X,Location.Y, 70 30 ,s) ;
}
}
}

private void printDocument2_PrintPage( object sender,PrintPageEventArgs e)
{
// 计算考虑页边距的宽度和高度缩放
var wScale = e.MarginBounds.Width /( float )_ memoryImage.Width;
var hScale = e.MarginBounds.Height /( float )_ memoryImage.Height;

// 选择两个比例中较小的一个
< span class =code-keyword> var
scale = wScale < hScale? wScale:hScale;

// 对图像应用缩放
e.Graphics .ScaleTransform(规模,比例);

// 打印到默认打印机的页面
e.Graphics .DrawImage(_memoryImage, 70 30 );
printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize( PaperA4 826 1169 );
}
私有 void btnprintpre_Click( object sender,EventArgs e)
{
// 将PrintPreviewDialog与PrintDocument相关联。
printPreviewDialog1.Document = printDocument2;

// 显示PrintPreview对话框
// printPreviewDialog1.ShowDialog();

printPreviewDialog1.WindowState = FormWindowState.Maximized;
printPreviewDialog1.ShowDialog();
}





我接近var wScale = e.MarginBounds.Width /(float)_memoryImage.Width ;



因此,如果有人遇到此错误或可以解决我的问题,请帮助我。

解决方案

< blockquote> e.MarginBounds.Width或(float)_memoryImage.Width返回null给你错误原因的语句


调试你的代码并检查_memoryImage变量是否被分配了一些值。



在使用其属性之前检查对象值是一个很好的做法。



喜欢



if(_memoryImage!= null)

{

//在这里写下您的全部代码

}


< solve>

我的问题通过将正常按钮更改为菜单按钮并将代码放入其中来解决,但我现在面临另一个错误,



这是我的完整代码:

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Data.SqlClient;
使用 System.Diagnostics;
使用 System.Drawing.Printing;
使用 System.Data.SqlTypes;
使用 Microsoft.SqlServer.Server;
使用 System.Timers;

命名空间 ComplaintReport
{
public partial class MRU240:表格
{

private PrintPreviewDialog Pv;
private PageSetupDialog Ps;
private PrintDocument Pd;
private PrintDialog Pr;

private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem mnuFile;
private System.Windows.Forms.MenuItem mnuSetup;
private System.Windows.Forms.MenuItem mnuPreview;
private System.Windows.Forms.MenuItem mnuPrint;
private System.Windows.Forms.Button cmdQuit;

public MRU240()
{
InitializeComponent();

Pv = new PrintPreviewDialog();
Ps = new PageSetupDialog();
Pr = new PrintDialog();
Pd = new PrintDocument();

Pd.DocumentName = 我的新文件;
Pv.Document = Pd;
Ps.Document = Pd;
Pr.Document = Pd;
}



private void clearAll()
{
txt1.Text = ;
txtsno.Text = ;
txt3.Text = ;
txt4.Text = ;
txt5.Text = ;
txt6.Text = ;
txt7.Text = ;
txt8.Text = ;
txt9.Text = ;
txt11.Text = ;
txt12.Text = ;
txt13.Text = ;
txt14.Text = ;
txt15.Text = ;
txt16.Text = ;
txt17.Text = ;
txt18.Text = ;
txt19.Text = ;
txt20.Text = ;

}
private void newToolStripMenuItem_Click( object sender,EventArgs e)
{
clearAll();

btnedit.Visible = false ;
btnupdate.Visible = false ;
txt1.Visible = true ;
txt2.Visible = true ;
txtsno.Visible = true ;
txt3.Visible = true ;
txt4.Visible = true ;
txt5.Visible = true ;
txt6.Visible = true ;
txt7.Visible = true ;
txt8.Visible = true ;
txt9.Visible = true ;
txt11.Visible = true ;
txt12.Visible = true ;
txt13.Visible = true ;
txt14.Visible = true ;
txt15.Visible = true ;
txt16.Visible = true ;
txt17.Visible = true ;
txt18.Visible = true ;
txt19.Visible = true ;
txt20.Visible = true ;
lbluno.Visible = false ;
label1.Visible = true ;
label2.Visible = true ;
label3.Visible = true ;
label4.Visible = true ;
label5.Visible = true ;
label6.Visible = true ;
label7.Visible = true ;
label8.Visible = true ;
label9.Visible = true ;
label10.Visible = true ;
label11.Visible = true ;
label12.Visible = true ;
label13.Visible = true ;
label14.Visible = true ;
label15.Visible = true ;
label16.Visible = true ;
label17.Visible = true ;
label18.Visible = true ;
label19.Visible = true ;
label21.Visible = true ;
label22.Visible = true ;
label23.Visible = true ;
label25.Visible = true ;
btnexit.Visible = true ;
comboBox1.Visible = false ;

txt1.ReadOnly = false ;
txt3.ReadOnly = false ;
txt4.ReadOnly = false ;
txt5.ReadOnly = false ;
txt6.ReadOnly = false ;
txt7.ReadOnly = false ;
txt8.ReadOnly = false ;
txt9.ReadOnly = false ;
txt11.ReadOnly = false ;
txt12.ReadOnly = false ;
txt13.ReadOnly = false ;
txt14.ReadOnly = false ;
txt15.ReadOnly = false ;
txt16.ReadOnly = false ;
txt17.ReadOnly = false ;
txt18.ReadOnly = false ;
txt19.ReadOnly = false ;
txt20.ReadOnly = false ;
txtsno.ReadOnly = false ;
txt2.ReadOnly = true ;
}

private void openToolStripMenuItem_Click( object sender,EventArgs e)
{
string cs = Data Source = 192.168.1.133; database = ComplaintReport; user id = sa; password = ssdrnd;
SqlConnection conn = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand();
conn.Open();

SqlDataAdapter da = new SqlDataAdapter( 从MRU240中选择Mno,conn);
DataSet ds = new DataSet();
cmd = new SqlCommand( 选择Mno来自MRU240,conn);
da.Fill(ds, mru240);

comboBox1.DataSource = ds.Tables [ mru240];
comboBox1.DisplayMember = Mno;
conn.Close();

// btnprint.Visible = true;

comboBox1.Visible = true ;
lbluno.Visible = true ;
btnupdate.Visible = false ;
txt1.Visible = false ;
txt2.Visible = false ;
txtsno.Visible = false ;
txt3.Visible = false ;
txt4.Visible = false ;
txt5.Visible = false ;
txt6.Visible = false ;
txt7.Visible = false ;
txt8.Visible = false ;
txt9.Visible = false ;
txt11.Visible = false ;
txt12.Visible = false ;
txt13.Visible = false ;
txt14.Visible = false ;
txt15.Visible = false ;
txt16.Visible = false ;
txt17.Visible = false ;
txt18.Visible = false ;
txt19.Visible = false ;
txt20.Visible = false ;
label3.Visible = false ;
label2.Visible = false ;
label4.Visible = false ;
label5.Visible = false ;
label6.Visible = false ;
label7.Visible = false ;
label8.Visible = false ;
label9.Visible = false ;
label10.Visible = false ;
label11.Visible = false ;
label12.Visible = false ;
label13.Visible = false ;
label14.Visible = false ;
label15.Visible = false ;
label16.Visible = false ;
label17.Visible = false ;
label18.Visible = false ;
label19.Visible = false ;
label21.Visible = false ;
label22.Visible = false ;
label23.Visible = false ;
label25.Visible = false ;
btnedit.Visible = false ;

btnexit.Visible = false ;

}


private void toolStripMenuback_Click( object sender,EventArgs e)
{
SSCVR obj6 = new SSCVR();
obj6.Show();
this .Hide();
}


私人 void MRU240_Load(< span class =code-keyword> object sender,EventArgs e)
{
comboBox1.DropDownHeight = comboBox1.ItemHeight * 10 ;
comboBox1.MouseWheel + = new MouseEventHandler(comboBox1_MouseWheel);

txt1.Visible = false ;
txt2.Visible = false ;
txtsno.Visible = false ;
txt3.Visible = false ;
txt4.Visible = false ;
txt5.Visible = false ;
txt6.Visible = false ;
txt7.Visible = false ;
txt8.Visible = false ;
txt9.Visible = false ;
txt11.Visible = false ;
txt12.Visible = false ;
txt13.Visible = false ;
txt14.Visible = false ;
txt15.Visible = false ;
txt16.Visible = false ;
txt17.Visible = false ;
txt18.Visible = false ;
txt19.Visible = false ;
txt20.Visible = false ;
txtsno.Visible = false ;
comboBox1.Visible = false ;
label2.Visible = false ;
label4.Visible = false ;
label5.Visible = false ;
label6.Visible = false ;
label7.Visible = false ;
label8.Visible = false ;
label9.Visible = false ;
label10.Visible = false ;
label11.Visible = false ;
label12.Visible = false ;
label13.Visible = false ;
label14.Visible = false ;
label15.Visible = false ;
label16.Visible = false ;
label17.Visible = false ;
label18.Visible = false ;
label19.Visible = false ;
label21.Visible = false ;
label22.Visible = false ;
label23.Visible = false ;
label25.Visible = false ;
label13.Visible = false ;
label3.Visible = false ;
btnupdate.Visible = false ;
btnexit.Visible = false ;

}
void comboBox1_MouseWheel( object sender,MouseEventArgs e)
{
((HandledMouseEventArgs)e)。已处理= true ;
}



private void btnexit_Click( object sender,EventArgs e)
{
SSCVRGSE obj4 = new SSCVRGSE();
obj4.Show();
this .Hide();
}



private void saveToolStripMenuItem_Click( object sender,EventArgs e)
{
string cs = < span class =code-string> Data Source = 192.168.1.133; database = ComplaintReport; user id = sa; password = ssdrnd;
SqlConnection conn = new SqlConnection(cs);
if (txtsno.Text ==
{
MessageBox.Show( 请输入序列号< /跨度>);
txtsno.Focus();
}
if (txtsno.Text.Length > = 7
{
MessageBox.Show( 序列号低于7个字符);
txtsno.Focus();
}


尝试
{
string cfd = txt1.Text;
string Uno = txt2.Text;
string Mno = txtsno.Text;
string cprc = txt3.Text;
string sdc = txt4.Text;
string contact = txt5.Text;
string orderno = txt6.Text;
string ino = txt7.Text;
string consig = txt8.Text;
string pft = txt9.Text;
string remarks = txt11.Text;
string obsv = txt12.Text;
string disptach = txt13.Text;
string repair = txt14.Text;
string cause = txt15.Text;
string userinfo = txt16.Text;
string remedial = txt17.Text;
string feedbck = txt18.Text;
string conclu = txt19.Text;
string notes = txt20.Text;

SqlCommand cmd = new SqlCommand( 插入MRU240值(@ cfd,@ Uno,@ Mno,@ cprc,@ sdc,@ contact,@ orderno,@ ino,@ consig,@ pft,@ remarks,@ obsv,@ dispatch,@ repair, @ cause,@ userinfo,@ remedial,@ feedbck,@ conclu,@ notes),conn);
cmd.Parameters.AddWithValue( @ cfd,cfd);
cmd.Parameters.AddWithValue( @ Uno,Uno);
cmd.Parameters.AddWithValue( @ Mno,Mno);
cmd.Parameters.AddWithValue( @ cprc,cprc);
cmd.Parameters.AddWithValue( @ sdc,sdc);
cmd.Parameters.AddWithValue( @ contact,contact);
cmd.Parameters.AddWithValue( @ orderno,orderno);
cmd.Parameters.AddWithValue( @ ino,ino);
cmd.Parameters.AddWithValue( @ consig,consig);
cmd.Parameters.AddWithValue( @ pft,pft);
cmd.Parameters.AddWithValue( @ remarks,备注);
cmd.Parameters.AddWithValue( @ obsv,obsv);
cmd.Parameters.AddWithValue( @ dispatch,disptach);
cmd.Parameters.AddWithValue( @ repair,修复);
cmd.Parameters.AddWithValue( @ cause,cause);
cmd.Parameters.AddWithValue( @ userinfo,userinfo);
cmd.Parameters.AddWithValue( @ remedial,补救);
cmd.Parameters.AddWithValue( @ feedbck,feedbck);
cmd.Parameters.AddWithValue( @ conclu,conclu);
cmd.Parameters.AddWithValue( @ notes,notes);

conn.Open();
int i = cmd.ExecuteNonQuery();
if (i == 1
{
MessageBox。显示( 保存记录);
clearAll();
}

}
catch (Exception ex)
{
MessageBox.Show( ex.Message);
}
conn.Close();

}

private void btnedit_Click_1(< span class =code-keyword> object
sender,EventArgs e)
{
txt1.ReadOnly = false ;
lbluno.Visible = true ;
txt3.ReadOnly = false ;
txt4.ReadOnly = false ;
txt5.ReadOnly = false ;
txt6.ReadOnly = false ;
txt7.ReadOnly = false ;
txt8.ReadOnly = false ;
txt9.ReadOnly = false ;
txt11.ReadOnly = false ;
txt12.ReadOnly = false ;
txt13.ReadOnly = false ;
txt14.ReadOnly = false ;
txt15.ReadOnly = false ;
txt16.ReadOnly = false ;
txt17.ReadOnly = false ;
txt18.ReadOnly = false ;
txt19.ReadOnly = false ;
txt20.ReadOnly = false ;
txtsno.ReadOnly = true ;
label13.Visible = true ;
btnupdate.Visible = true ;
}

private void comboBox1_SelectedIndexChanged_1( object sender,EventArgs e)
{
btnupdate.Visible = false ;
btnedit.Visible = true ;
txt1.Visible = true ;
txt2.Visible = true ;
txtsno.Visible = true ;
txt3.Visible = true ;
txt4.Visible = true ;
txt5.Visible = true ;
txt6.Visible = true ;
txt7.Visible = true ;
txt8.Visible = true ;
txt9.Visible = true ;
txt11.Visible = true ;
txt12.Visible = true ;
txt13.Visible = true ;
txt14.Visible = true ;
txt15.Visible = true ;
txt16.Visible = true ;
txt17.Visible = true ;
txt18.Visible = true ;
txt19.Visible = true ;
txt20.Visible = true ;
label2.Visible = true ;
label4.Visible = true ;
label5.Visible = true ;
label6.Visible = true ;
label7.Visible = true ;
label8.Visible = true ;
label9.Visible = true ;
label10.Visible = true ;
label11.Visible = true ;
label12.Visible = true ;
label13.Visible = true ;
label14.Visible = true ;
label15.Visible = true ;
label16.Visible = true ;
label17.Visible = true ;
label18.Visible = true ;
label19.Visible = true ;
label21.Visible = true ;
label22.Visible = true ;
label23.Visible = true ;
label25.Visible = true ;
label3.Visible = true ;
txtsno.Visible = true ;
btnexit.Visible = true ;
comboBox1.Visible = true ;
txt1.ReadOnly = true ;
txtsno.Visible = true ;
txt3.ReadOnly = true ;
txt4.ReadOnly = true ;
txt5.ReadOnly = true ;
txt6.ReadOnly = true ;
txt7.ReadOnly = true ;
txt8.ReadOnly = true ;
txt9.ReadOnly = true ;
txt11.ReadOnly = true ;
txt12.ReadOnly = true ;
txt13.ReadOnly = true ;
txt14.ReadOnly = true ;
txt15.ReadOnly = true ;
txt16.ReadOnly = true ;
txt17.ReadOnly = true ;
txt18.ReadOnly = true ;
txt19.ReadOnly = true ;
txt20.ReadOnly = true ;
txtsno.ReadOnly = true ;

string cs = server = 192.168.1.133; database = ComplaintReport; user id = sa; password = ssdrnd;
SqlConnection conn = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand();
尝试
{
conn.Open();
int dbsno = int .Parse(comboBox1.Text);

cmd = new SqlCommand( 从MRU240中选择*,其中Mno = @ dbsno,conn);
cmd.Parameters.AddWithValue( @ dbsno,dbsno);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txt1.Text = dr [ cfd]。ToString();
txt2.Text = dr [ Uno]。ToString();
txtsno.Text = dr [ Mno]。ToString();
txt3.Text = dr [ cprc]。ToString();
txt4.Text = dr [ sdc]。ToString();
txt5.Text = dr [ contact]。ToString();
txt6.Text = dr [ orderno]。ToString();
txt7.Text = dr [ ino]。ToString();
txt8.Text = dr [ 收货人]。ToString();
txt9.Text = dr [ pft]。ToString();
txt12.Text = dr [ observation]。ToString();
txt13.Text = dr [ dispatch]。ToString();
txt14.Text = dr [ repair]。ToString();
txt15.Text = dr [ cause]。ToString();
txt16.Text = dr [ userinfo]。ToString();
txt17.Text = dr [ 补救]。ToString();
txt18.Text = dr [ feedback]。ToString();
txt19.Text = dr [ 结论]。ToString();
txt20.Text = dr [ notes]。ToString();
txt11.Text = dr [ 备注]。ToString();
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message) ;
}

conn.Close();
}

私有 void btnupdate_Click_1( object sender,EventArgs e)
{
string cs = Data Source = 192.168.1.133; database = ComplaintReport; user id = sa; password = ssdrnd;
SqlConnection conn = new SqlConnection(cs);

string cfd = txt1.Text;
string Uno = txt2.Text;
string Mno = txtsno.Text;
string cprc = txt3.Text;
string sdc = txt4.Text;
string contact = txt5.Text;
string orderno = txt6.Text;
string ino = txt7.Text;
string consig = txt8.Text;
string pft = txt9.Text;
string remarks = txt11.Text;
string obsv = txt12.Text;
string disptach = txt13.Text;
string repair = txt14.Text;
string cause = txt15.Text;
string userinfo = txt16.Text;
string remedial = txt17.Text;
string feedbck = txt18.Text;
string conclu = txt19.Text;
string notes = txt20.Text;
int dbsno = int .Parse(comboBox1.Text);
SqlCommand cmd = new SqlCommand( update MRU240设置cfd = @ cfd,Uno = @ Uno,cprc = @ cprc,sdc = @ sdc,contact = @ contact,orderno = @ orderno,ino = @ ino,consignee = @ consig,pft = @ pft,remarks = @备注,观察= @ obsv,dispatch = @ dispatch,repair = @ repair,cause = @ cause,userinfo = @ userinfo,remedial = @ remedial,feedback = @ feedbck,结论= @ conclu,notes = @ notes其中Mno = @ dbsno,conn);
cmd.Parameters.AddWithValue( @ cfd,cfd);
cmd.Parameters.AddWithValue( @ Uno,Uno);
cmd.Parameters.AddWithValue( @ Mno,Mno);
cmd.Parameters.AddWithValue( @ cprc,cprc);
cmd.Parameters.AddWithValue( @ sdc,sdc);
cmd.Parameters.AddWithValue( @ contact,contact);
cmd.Parameters.AddWithValue( @ orderno,orderno);
cmd.Parameters.AddWithValue( @ ino,ino);
cmd.Parameters.AddWithValue( @ consig,consig);
cmd.Parameters.AddWithValue( @ pft,pft);
cmd.Parameters.AddWithValue( @ remarks,备注);
cmd.Parameters.AddWithValue( @ obsv,obsv);
cmd.Parameters.AddWithValue( @ dispatch,disptach);
cmd.Parameters.AddWithValue( @ repair,修复);
cmd.Parameters.AddWithValue( @ cause,cause);
cmd.Parameters.AddWithValue( @ userinfo,userinfo);
cmd.Parameters.AddWithValue( @ remedial,补救);
cmd.Parameters.AddWithValue( @ feedbck,feedbck);
cmd.Parameters.AddWithValue( @ conclu,conclu);
cmd.Parameters.AddWithValue( @ notes,notes);
cmd.Parameters.AddWithValue( @ dbsno,dbsno);

conn.Open();
int i =( int )cmd.ExecuteNonQuery();
if (i > = 1
{
MessageBox.Show( 记录已更新 );
clearAll();
txt2.Text = ;
}
else
{
MessageBox.Show( 记录未更新);
}
conn.Close();
}


private void txtsno_Leave(< span class =code-keyword> object sender,EventArgs e)
{
if (txtsno.Text == < span class =code-string>
{
MessageBox.Show( 请输入序列号);
txtsno.Focus();
}
if (txtsno.Text.Length > = 7
{
MessageBox.Show( 输入以下7个字符);
txtsno.Focus();
}

}

受保护 覆盖 void OnPaint(PaintEventArgs e)
{
DrawIt(e.Graphics);
}

private void DrawIt(Graphics G)
{
// PrintDocument document = new PrintDocument();
< span class =code-comment> // document.PrintPage + = new PrintPageEventHandler(document_PrintPage);
CaptureScreen();

}
private 位图_memoryImage;



private void CaptureScreen()
{
// 投入使用构造因为图形对象不
// 在离开方法范围时自动处理
使用 var myGraphics = CreateGraphics())
{
var s =大小;
_memoryImage = new 位图( 1500 1000 ,myGraphics);
使用 var memoryGraphics = Graphics.FromImage(_memoryImage))
{
printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize( PaperA4 826 1169 );
memoryGraphics.CopyFromScreen(Location.X,Location.Y, 70 30 ,s) ;
}
}
}
私有 void printDocument2_PrintPage ( object sender,PrintPageEventArgs e)
{
// 计算考虑页边距的宽度和高度缩放
var wScale = e.MarginBounds.Width /()_ memoryImage.Width;
var hScale = e.MarginBounds.Height /( float )_ memoryImage.Height;

// 选择两个比例中较小的一个
< span class =code-keyword> var scale = wScale < hScale? wScale:hScale;

// 对图像应用缩放
e.Graphics .ScaleTransform(规模,比例);

// 打印到默认打印机的页面
e.Graphics .DrawImage(_memoryImage, 70 30 );
printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize( PaperA4 826 1169 );
}


private void pd_Print(< span class =code-keyword> object sender,PrintPageEventArgs e)
{
DrawIt(e.Graphics);
}


私人 void pageSetupToolStripMenuItem_Click(< span class =code-keyword> object sender,EventArgs e)
{
Ps.ShowDialog();
Pd.DefaultPageSettings = Ps.PageSettings;
Pd.PrinterSettings = Ps.PrinterSettings;
}

private void printPreviewToolStripMenuItem_Click( object sender,EventArgs e)
{
Pv.WindowState = FormWindowState.Maximized;
Pv.ShowDialog();
}

private void printToolStripMenuItem_Click( object sender,EventArgs e)
{
if (Pr.ShowDialog()== DialogResult.OK )
Pd.Print();
}





}
}





点击页面设置我可以设置一个页面,但是它在页面中显示了一些其他代码,而点击打印预览时只显示空白页面,所以当我尝试打印时只有空白页面即将来临...所以任何人都可以帮助我,或者任何遇到此类错误的人都会帮助我。


Hi,i'm posting question for first time so if i dnt follow any threads or any mistake please excuse me...

Im getting this error in my code:
"Object reference not set to an instance of an object."

Here is my code:

 void document_PrintPage(object sender, PrintPageEventArgs e)
        {
            
            e.Graphics.DrawImage(_memoryImage, 0, 0);

        }
        private Bitmap _memoryImage;

        private void CaptureScreen()
        {
            // put into using construct because Graphics objects do not 
            //  get automatically disposed when leaving method scope
            using (var myGraphics = CreateGraphics())
            {
                var s = Size;
                _memoryImage = new Bitmap(1500, 1000, myGraphics);
                using (var memoryGraphics = Graphics.FromImage(_memoryImage))
                {
                    printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 826, 1169);
                    memoryGraphics.CopyFromScreen(Location.X, Location.Y, 70, 30, s);
                }
            }
        }

        private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
        {
             //calculate width and height scalings taking page margins into account
            var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
            var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;

            // choose the smaller of the two scales
            var scale = wScale < hScale ? wScale : hScale;

            // apply scaling to the image
            e.Graphics.ScaleTransform(scale, scale);

            // print to default printer's page
            e.Graphics.DrawImage(_memoryImage, 70, 30);
            printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 826, 1169);
        }
private void btnprintpre_Click(object sender, EventArgs e)
        {
            //Associate PrintPreviewDialog with PrintDocument.
            printPreviewDialog1.Document = printDocument2;

            // Show PrintPreview Dialog
            //printPreviewDialog1.ShowDialog();

            printPreviewDialog1.WindowState = FormWindowState.Maximized;
            printPreviewDialog1.ShowDialog();
        }



And i'm getting near "var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;"

So if anyone has come across this error or can solve my problem please help me.

解决方案

e.MarginBounds.Width or (float)_memoryImage.Width is returning null to the statement thats why you have error


debug your code and check whether _memoryImage variable is assigned some value or not.

Its a good practice to check for object value before using its properties.

like

if(_memoryImage != null)
{
//write your whole code here
}


<solved>
My problem got solved by changing the normal buttons to menu buttons and putting the code in it but i'm facing another error now,

Here is my complete code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing.Printing;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Timers;

namespace ComplaintReport
{
    public partial class MRU240 : Form
    {

        private PrintPreviewDialog Pv;
        private PageSetupDialog Ps;
        private PrintDocument Pd;
        private PrintDialog Pr;

        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem mnuFile;
        private System.Windows.Forms.MenuItem mnuSetup;
        private System.Windows.Forms.MenuItem mnuPreview;
        private System.Windows.Forms.MenuItem mnuPrint;
        private System.Windows.Forms.Button cmdQuit;

         public MRU240()
        {
            InitializeComponent();

            Pv = new PrintPreviewDialog();
            Ps = new PageSetupDialog();
            Pr = new PrintDialog();
            Pd = new PrintDocument();

            Pd.DocumentName = "My New Document";
            Pv.Document = Pd;
            Ps.Document = Pd;
            Pr.Document = Pd;
        }


       
        private void clearAll()
        {
            txt1.Text = "";
            txtsno.Text = "";
            txt3.Text = "";
            txt4.Text = "";
            txt5.Text = "";
            txt6.Text = "";
            txt7.Text = "";
            txt8.Text = "";
            txt9.Text = "";
            txt11.Text = "";
            txt12.Text = "";
            txt13.Text = "";
            txt14.Text = "";
            txt15.Text = "";
            txt16.Text = "";
            txt17.Text = "";
            txt18.Text = "";
            txt19.Text = "";
            txt20.Text = "";
       
        }
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            clearAll();
        
            btnedit.Visible = false;
            btnupdate.Visible = false;
            txt1.Visible = true;
            txt2.Visible = true;
            txtsno.Visible = true;
            txt3.Visible = true;
            txt4.Visible = true;
            txt5.Visible = true;
            txt6.Visible = true;
            txt7.Visible = true;
            txt8.Visible = true;
            txt9.Visible = true;
            txt11.Visible = true;
            txt12.Visible = true;
            txt13.Visible = true;
            txt14.Visible = true;
            txt15.Visible = true;
            txt16.Visible = true;
            txt17.Visible = true;
            txt18.Visible = true;
            txt19.Visible = true;
            txt20.Visible = true;
            lbluno.Visible = false;
            label1.Visible = true;
            label2.Visible = true;
            label3.Visible = true;
            label4.Visible = true;
            label5.Visible = true;
            label6.Visible = true;
            label7.Visible = true;
            label8.Visible = true;
            label9.Visible = true;
            label10.Visible = true;
            label11.Visible = true;
            label12.Visible = true;
            label13.Visible = true;
            label14.Visible = true;
            label15.Visible = true;
            label16.Visible = true;
            label17.Visible = true;
            label18.Visible = true;
            label19.Visible = true;
            label21.Visible = true;
            label22.Visible = true;
            label23.Visible = true;
            label25.Visible = true;
            btnexit.Visible = true;
            comboBox1.Visible = false;

            txt1.ReadOnly = false;
            txt3.ReadOnly = false;
            txt4.ReadOnly = false;
            txt5.ReadOnly = false;
            txt6.ReadOnly = false;
            txt7.ReadOnly = false;
            txt8.ReadOnly = false;
            txt9.ReadOnly = false;
            txt11.ReadOnly = false;
            txt12.ReadOnly = false;
            txt13.ReadOnly = false;
            txt14.ReadOnly = false;
            txt15.ReadOnly = false;
            txt16.ReadOnly = false;
            txt17.ReadOnly = false;
            txt18.ReadOnly = false;
            txt19.ReadOnly = false;
            txt20.ReadOnly = false;
            txtsno.ReadOnly = false;
            txt2.ReadOnly = true;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string cs = "Data Source=192.168.1.133;database=ComplaintReport;user id=sa;password=ssdrnd";
            SqlConnection conn = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand();
            conn.Open();
           
            SqlDataAdapter da = new SqlDataAdapter("select Mno from MRU240", conn);
            DataSet ds = new DataSet();
            cmd = new SqlCommand("select Mno from MRU240", conn);
            da.Fill(ds, "mru240");

            comboBox1.DataSource = ds.Tables["mru240"];
            comboBox1.DisplayMember = "Mno";
            conn.Close();
           
            //btnprint.Visible = true;
            
            comboBox1.Visible = true;
            lbluno.Visible = true;
            btnupdate.Visible = false;
            txt1.Visible = false;
            txt2.Visible = false;
            txtsno.Visible = false;
            txt3.Visible = false;
            txt4.Visible = false;
            txt5.Visible = false;
            txt6.Visible = false;
            txt7.Visible = false;
            txt8.Visible = false;
            txt9.Visible = false;
            txt11.Visible = false;
            txt12.Visible = false;
            txt13.Visible = false;
            txt14.Visible = false;
            txt15.Visible = false;
            txt16.Visible = false;
            txt17.Visible = false;
            txt18.Visible = false;
            txt19.Visible = false;
            txt20.Visible = false;
            label3.Visible = false;
            label2.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            label6.Visible = false;
            label7.Visible = false;
            label8.Visible = false;
            label9.Visible = false;
            label10.Visible = false;
            label11.Visible = false;
            label12.Visible = false;
            label13.Visible = false;
            label14.Visible = false;
            label15.Visible = false;
            label16.Visible = false;
            label17.Visible = false;
            label18.Visible = false;
            label19.Visible = false;
            label21.Visible = false;
            label22.Visible = false;
            label23.Visible = false;
            label25.Visible = false;
            btnedit.Visible = false;
            
            btnexit.Visible = false;

        }

      
        private void toolStripMenuback_Click(object sender, EventArgs e)
        {
            SSCVR obj6 = new SSCVR();
            obj6.Show();
            this.Hide();
        }
       

        private void MRU240_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownHeight = comboBox1.ItemHeight * 10;
            comboBox1.MouseWheel += new MouseEventHandler(comboBox1_MouseWheel);
   
            txt1.Visible = false;
            txt2.Visible = false;
            txtsno.Visible = false;
            txt3.Visible = false;
            txt4.Visible = false;
            txt5.Visible = false;
            txt6.Visible = false;
            txt7.Visible = false;
            txt8.Visible = false;
            txt9.Visible = false;
            txt11.Visible = false;
            txt12.Visible = false;
            txt13.Visible = false;
            txt14.Visible = false;
            txt15.Visible = false;
            txt16.Visible = false;
            txt17.Visible = false;
            txt18.Visible = false;
            txt19.Visible = false;
            txt20.Visible = false;
            txtsno.Visible = false;
            comboBox1.Visible = false;
            label2.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            label6.Visible = false;
            label7.Visible = false;
            label8.Visible = false;
            label9.Visible = false;
            label10.Visible = false;
            label11.Visible = false;
            label12.Visible = false;
            label13.Visible = false;
            label14.Visible = false;
            label15.Visible = false;
            label16.Visible = false;
            label17.Visible = false;
            label18.Visible = false;
            label19.Visible = false;
            label21.Visible = false;
            label22.Visible = false;
            label23.Visible = false;
            label25.Visible = false;
            label13.Visible = false;
            label3.Visible = false;
            btnupdate.Visible = false;
            btnexit.Visible = false;

        }
        void comboBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            ((HandledMouseEventArgs)e).Handled = true;
        }
        


        private void btnexit_Click(object sender, EventArgs e)
        {
            SSCVRGSE obj4 = new SSCVRGSE();
            obj4.Show();
            this.Hide();
        }



        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string cs = "Data Source=192.168.1.133;database=ComplaintReport;user id=sa;password=ssdrnd";
            SqlConnection conn = new SqlConnection(cs);
            if (txtsno.Text == "")
            {
                MessageBox.Show("Please enter serial number");
                txtsno.Focus();
            }
            if (txtsno.Text.Length >= 7)
            {
                MessageBox.Show("Serial number below 7 charachters ");
                txtsno.Focus();
            }


            try
            {
                string cfd = txt1.Text;
                string Uno = txt2.Text;
                string Mno = txtsno.Text;
                string cprc = txt3.Text;
                string sdc = txt4.Text;
                string contact = txt5.Text;
                string orderno = txt6.Text;
                string ino = txt7.Text;
                string consig = txt8.Text;
                string pft = txt9.Text;
                string remarks = txt11.Text;
                string obsv = txt12.Text;
                string disptach = txt13.Text;
                string repair = txt14.Text;
                string cause = txt15.Text;
                string userinfo = txt16.Text;
                string remedial = txt17.Text;
                string feedbck = txt18.Text;
                string conclu = txt19.Text;
                string notes = txt20.Text;
           
                SqlCommand cmd = new SqlCommand("insert into MRU240 values(@cfd,@Uno,@Mno,@cprc,@sdc,@contact,@orderno,@ino,@consig,@pft,@remarks,@obsv,@dispatch,@repair,@cause,@userinfo,@remedial,@feedbck,@conclu,@notes)", conn);
                cmd.Parameters.AddWithValue("@cfd", cfd);
                cmd.Parameters.AddWithValue("@Uno", Uno);
                cmd.Parameters.AddWithValue("@Mno", Mno);
                cmd.Parameters.AddWithValue("@cprc", cprc);
                cmd.Parameters.AddWithValue("@sdc", sdc);
                cmd.Parameters.AddWithValue("@contact", contact);
                cmd.Parameters.AddWithValue("@orderno", orderno);
                cmd.Parameters.AddWithValue("@ino", ino);
                cmd.Parameters.AddWithValue("@consig", consig);
                cmd.Parameters.AddWithValue("@pft", pft);
                cmd.Parameters.AddWithValue("@remarks", remarks);
                cmd.Parameters.AddWithValue("@obsv", obsv);
                cmd.Parameters.AddWithValue("@dispatch", disptach);
                cmd.Parameters.AddWithValue("@repair", repair);
                cmd.Parameters.AddWithValue("@cause", cause);
                cmd.Parameters.AddWithValue("@userinfo", userinfo);
                cmd.Parameters.AddWithValue("@remedial", remedial);
                cmd.Parameters.AddWithValue("@feedbck", feedbck);
                cmd.Parameters.AddWithValue("@conclu", conclu);
                cmd.Parameters.AddWithValue("@notes", notes);

                conn.Open();
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    MessageBox.Show("Record is saved ");
                    clearAll();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            conn.Close();

        }

        private void btnedit_Click_1(object sender, EventArgs e)
        {
            txt1.ReadOnly = false;
            lbluno.Visible = true;
            txt3.ReadOnly = false;
            txt4.ReadOnly = false;
            txt5.ReadOnly = false;
            txt6.ReadOnly = false;
            txt7.ReadOnly = false;
            txt8.ReadOnly = false;
            txt9.ReadOnly = false;
            txt11.ReadOnly = false;
            txt12.ReadOnly = false;
            txt13.ReadOnly = false;
            txt14.ReadOnly = false;
            txt15.ReadOnly = false;
            txt16.ReadOnly = false;
            txt17.ReadOnly = false;
            txt18.ReadOnly = false;
            txt19.ReadOnly = false;
            txt20.ReadOnly = false;
            txtsno.ReadOnly = true;
            label13.Visible = true;
            btnupdate.Visible = true;
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            btnupdate.Visible = false;
            btnedit.Visible = true;
            txt1.Visible = true;
            txt2.Visible = true;
            txtsno.Visible = true;
            txt3.Visible = true;
            txt4.Visible = true;
            txt5.Visible = true;
            txt6.Visible = true;
            txt7.Visible = true;
            txt8.Visible = true;
            txt9.Visible = true;
            txt11.Visible = true;
            txt12.Visible = true;
            txt13.Visible = true;
            txt14.Visible = true;
            txt15.Visible = true;
            txt16.Visible = true;
            txt17.Visible = true;
            txt18.Visible = true;
            txt19.Visible = true;
            txt20.Visible = true;
            label2.Visible = true;
            label4.Visible = true;
            label5.Visible = true;
            label6.Visible = true;
            label7.Visible = true;
            label8.Visible = true;
            label9.Visible = true;
            label10.Visible = true;
            label11.Visible = true;
            label12.Visible = true;
            label13.Visible = true;
            label14.Visible = true;
            label15.Visible = true;
            label16.Visible = true;
            label17.Visible = true;
            label18.Visible = true;
            label19.Visible = true;
            label21.Visible = true;
            label22.Visible = true;
            label23.Visible = true;
            label25.Visible = true;
            label3.Visible = true;
            txtsno.Visible = true;
            btnexit.Visible = true;
            comboBox1.Visible = true;
            txt1.ReadOnly = true;
            txtsno.Visible = true;
            txt3.ReadOnly = true;
            txt4.ReadOnly = true;
            txt5.ReadOnly = true;
            txt6.ReadOnly = true;
            txt7.ReadOnly = true;
            txt8.ReadOnly = true;
            txt9.ReadOnly = true;
            txt11.ReadOnly = true;
            txt12.ReadOnly = true;
            txt13.ReadOnly = true;
            txt14.ReadOnly = true;
            txt15.ReadOnly = true;
            txt16.ReadOnly = true;
            txt17.ReadOnly = true;
            txt18.ReadOnly = true;
            txt19.ReadOnly = true;
            txt20.ReadOnly = true;
            txtsno.ReadOnly = true;

            string cs = "server=192.168.1.133;database=ComplaintReport;user id=sa;password=ssdrnd";
            SqlConnection conn = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand();
            try
            {
                conn.Open();
                int dbsno = int.Parse(comboBox1.Text);

                cmd = new SqlCommand("select * from MRU240 where Mno=@dbsno", conn);
                cmd.Parameters.AddWithValue("@dbsno", dbsno);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    txt1.Text = dr["cfd"].ToString();
                    txt2.Text = dr["Uno"].ToString();
                    txtsno.Text = dr["Mno"].ToString();
                    txt3.Text = dr["cprc"].ToString();
                    txt4.Text = dr["sdc"].ToString();
                    txt5.Text = dr["contact"].ToString();
                    txt6.Text = dr["orderno"].ToString();
                    txt7.Text = dr["ino"].ToString();
                    txt8.Text = dr["consignee"].ToString();
                    txt9.Text = dr["pft"].ToString();
                    txt12.Text = dr["observation"].ToString();
                    txt13.Text = dr["dispatch"].ToString();
                    txt14.Text = dr["repair"].ToString();
                    txt15.Text = dr["cause"].ToString();
                    txt16.Text = dr["userinfo"].ToString();
                    txt17.Text = dr["remedial"].ToString();
                    txt18.Text = dr["feedback"].ToString();
                    txt19.Text = dr["conclusion"].ToString();
                    txt20.Text = dr["notes"].ToString();
                    txt11.Text = dr["remarks"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            conn.Close();
        }

        private void btnupdate_Click_1(object sender, EventArgs e)
        {
            string cs = "Data Source=192.168.1.133;database=ComplaintReport;user id=sa;password=ssdrnd";
            SqlConnection conn = new SqlConnection(cs);

            string cfd = txt1.Text;
            string Uno = txt2.Text;
            string Mno = txtsno.Text;
            string cprc = txt3.Text;
            string sdc = txt4.Text;
            string contact = txt5.Text;
            string orderno = txt6.Text;
            string ino = txt7.Text;
            string consig = txt8.Text;
            string pft = txt9.Text;
            string remarks = txt11.Text;
            string obsv = txt12.Text;
            string disptach = txt13.Text;
            string repair = txt14.Text;
            string cause = txt15.Text;
            string userinfo = txt16.Text;
            string remedial = txt17.Text;
            string feedbck = txt18.Text;
            string conclu = txt19.Text;
            string notes = txt20.Text;
            int dbsno = int.Parse(comboBox1.Text);
            SqlCommand cmd = new SqlCommand("update MRU240 set cfd=@cfd,Uno=@Uno,cprc=@cprc,sdc=@sdc,contact=@contact,orderno=@orderno,ino=@ino,consignee=@consig,pft=@pft,remarks=@remarks,observation=@obsv,dispatch=@dispatch,repair=@repair,cause=@cause,userinfo=@userinfo,remedial=@remedial,feedback=@feedbck,conclusion=@conclu,notes=@notes where Mno=@dbsno", conn);
            cmd.Parameters.AddWithValue("@cfd", cfd);
            cmd.Parameters.AddWithValue("@Uno", Uno);
            cmd.Parameters.AddWithValue("@Mno", Mno);
            cmd.Parameters.AddWithValue("@cprc", cprc);
            cmd.Parameters.AddWithValue("@sdc", sdc);
            cmd.Parameters.AddWithValue("@contact", contact);
            cmd.Parameters.AddWithValue("@orderno", orderno);
            cmd.Parameters.AddWithValue("@ino", ino);
            cmd.Parameters.AddWithValue("@consig", consig);
            cmd.Parameters.AddWithValue("@pft", pft);
            cmd.Parameters.AddWithValue("@remarks", remarks);
            cmd.Parameters.AddWithValue("@obsv", obsv);
            cmd.Parameters.AddWithValue("@dispatch", disptach);
            cmd.Parameters.AddWithValue("@repair", repair);
            cmd.Parameters.AddWithValue("@cause", cause);
            cmd.Parameters.AddWithValue("@userinfo", userinfo);
            cmd.Parameters.AddWithValue("@remedial", remedial);
            cmd.Parameters.AddWithValue("@feedbck", feedbck);
            cmd.Parameters.AddWithValue("@conclu", conclu);
            cmd.Parameters.AddWithValue("@notes", notes);
            cmd.Parameters.AddWithValue("@dbsno", dbsno);
          
            conn.Open();
            int i = (int)cmd.ExecuteNonQuery();
            if (i >= 1)
            {
                MessageBox.Show("Record is updated ");
                clearAll();
                txt2.Text = "";
            }
            else
            {
                MessageBox.Show("Record is not updated");
            }
            conn.Close();
        }
       

        private void txtsno_Leave(object sender, EventArgs e)
        {
            if (txtsno.Text == "")
            {
                MessageBox.Show("Please enter serial number");
                txtsno.Focus();
            }
            if (txtsno.Text.Length >= 7)
            {
                MessageBox.Show("Enter below 7 charachers ");
                txtsno.Focus();
            }

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            DrawIt(e.Graphics);
        }

        private void DrawIt(Graphics G)
        {
            //PrintDocument document = new PrintDocument();
            //document.PrintPage += new PrintPageEventHandler(document_PrintPage);
            CaptureScreen();

        }
        private Bitmap _memoryImage;



        private void CaptureScreen()
        {
            // put into using construct because Graphics objects do not 
            //  get automatically disposed when leaving method scope
            using (var myGraphics = CreateGraphics())
            {
                var s = Size;
                _memoryImage = new Bitmap(1500, 1000, myGraphics);
                using (var memoryGraphics = Graphics.FromImage(_memoryImage))
                {
                    printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 826, 1169);
                    memoryGraphics.CopyFromScreen(Location.X, Location.Y, 70, 30, s);
                }
            }
        }
        private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
        {
            // calculate width and height scalings taking page margins into account
            var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
            var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;

            // choose the smaller of the two scales
            var scale = wScale < hScale ? wScale : hScale;

            // apply scaling to the image
            e.Graphics.ScaleTransform(scale, scale);

            // print to default printer's page
            e.Graphics.DrawImage(_memoryImage, 70, 30);
            printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 826, 1169);
        }


        private void pd_Print(object sender, PrintPageEventArgs e)
        {
            DrawIt(e.Graphics);
        }


        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ps.ShowDialog();
            Pd.DefaultPageSettings = Ps.PageSettings;
            Pd.PrinterSettings = Ps.PrinterSettings;
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Pv.WindowState = FormWindowState.Maximized;
            Pv.ShowDialog();
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Pr.ShowDialog() == DialogResult.OK)
                Pd.Print();
        }

      
       

       
    }
}



While clicking on page setup i'm able to setup a page but it is showing me some other code in the page and while clicking on print preview is displaying only blank page so when i try to take print only blank page is coming...So can anyone help me from this or anyone who came across this type of error help me out.


这篇关于错误:对象引用未设置为对象的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆