Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im trying to print an Image using PrintDocumentin C# but somehow the setting (like Number of Pages and Image Quality ) are ignored while printing and preview.

Is there anything wrong in following code, Am I missing something?
when occure a a generic error occurred in GDI+

my code below


string mname="";
           DialogResult result1 = MessageBox.Show("Are you  want to Print the Bill", "", MessageBoxButtons.YesNo);
           if (result1 == DialogResult.Yes)
           {
               Graphics g1 = this.CreateGraphics();
               Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
               Graphics g2 = Graphics.FromImage(MyImage);
               IntPtr dc1 = g1.GetHdc();
               IntPtr dc2 = g2.GetHdc();
               BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
               g1.ReleaseHdc(dc1);
               g2.ReleaseHdc(dc2);
               MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);
               FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read );
               StartPrint(fileStream, "Image");
               fileStream.Close();

               if (System.IO.File.Exists(@"c:\PrintPage.jpg"))
               {
                   System.IO.File.Delete(@"c:\PrintPage.jpg");
               }
               if (type == "IPD")
               {
                   foreach (DataGridViewRow row in dataGridView1.Rows)
                   {
                       if (row.Cells["medname"].Value != null)
                       {
                           mname = mname + row.Cells["medname"].Value.ToString() + ",";
                           SqlCommand cmd = new SqlCommand("insert into Tmedicine values('" + Tid().ToString() + "','" + label7.Text + "','" + label6.Text + "','" + row.Cells["medname"].Value + "','" + row.Cells["mdate"].Value + "','" + row.Cells["intake"].Value + "','" + row.Cells["unit"].Value + "','" + label4.Text + "','" + row.Cells["unitprice"].Value + "','" + row.Cells["qty"].Value + "','" + row.Cells["total"].Value + "')", con);
                           con.Open();
                           cmd.ExecuteNonQuery();
                           con.Close();
                       }
                   }
               }
               else
               {
                   foreach (DataGridViewRow row in dataGridView1.Rows)
                   {
                       if (row.Cells["medname"].Value != null)
                       {
                           mname = mname + row.Cells["medname"].Value.ToString() + ",";
                           SqlCommand cmd = new SqlCommand("insert into OutMedical values('" + oTid().ToString() + "','" + label7.Text + "','" + label6.Text + "','" + row.Cells["medname"].Value + "','" + row.Cells["mdate"].Value + "','" + row.Cells["intake"].Value + "','" + row.Cells["unit"].Value + "','" + label4.Text + "','" + row.Cells["unitprice"].Value + "','" + row.Cells["qty"].Value + "','" + row.Cells["total"].Value + "')", con);
                           con.Open();
                           cmd.ExecuteNonQuery();
                           con.Close();
                       }
                   }
               }

               SqlCommand com = new SqlCommand("insert into phpayment values('" + label3.Text + "','" + label4.Text + "','"+DateTime.Now.ToShortTimeString()+"','" + label7.Text + "','" + label6.Text + "','" + label10.Text + "','"+mname.ToString()+"','" + textBox1.Text + "')", con);
               con.Open();
               com.ExecuteNonQuery();
               con.Close();
               billid();
           }
Posted
Updated 25-Dec-11 20:45pm
v2
Comments
Rajesh Anuhya 26-Dec-11 2:50am    
Repost

1 solution

There are so many things wrong there, that I can't ifgnore them and even try to sort out you problem.
1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:
C#
SqlCommand cmd = new SqlCommand("insert into Tmedicine values(@TID)", con);
cmd.Parameters.AddWithValue(Tid());
con.Open();

2) Do not use the VS default names. You may remember that label7 contains information on the VAT being charged today, but how do you expect us to know that? Or you, in two weeks time? Use sensible names - it saves a lot of time in teh long run.
3) Never issue SQL commands which depend on a nominal order for columns in a database. Always name the comulms and the order in which you will provide content: that way the database can be changed outside you controil without making your whole application fail for no obvious reasons, or worse corrupting your entire DB:
SQL
INSERT INTO myTable (column1, column2) VALUES (@Value1, @Value2)

4) Comment your code. Do we have any idea what a type of "IPD" means? No. So tell us. It helps you when you have to come back to it.
5) When you use CreateGraphics, you are responsible for Disposing it. If you don't all sorts of strange errors can creep in!
6) When you tell us there is a problem, "Generic error in GDI+" for example, tell us which line the problem is occurring on!
7) What does that code have to do with PrintDocument? Where is it being called? Why it is being called?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900