الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خطأ بكود الطباعة

بدأه كارم سامي في 10 ديسمبر 2013 · 1 رد · 295 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ،

 

 

ده كود الصفحة كلها

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Runtime.InteropServices.DllImportAttribute;

namespace Jep_MData
{
    public partial class JMD_CustomerCard_Print : Form
    {
       

        private System.IO.Stream streamToPrint;

        string streamType;

        private static extern bool BitBlt

        (

            IntPtr hdcDest, // handle to destination DC

            int nXDest, // x-coord of destination upper-left corner

            int nYDest, // y-coord of destination upper-left corner

            int nWidth, // width of destination rectangle

            int nHeight, // height of destination rectangle

            IntPtr hdcSrc, // handle to source DC

            int nXSrc, // x-coordinate of source upper-left corner

            int nYSrc, // y-coordinate of source upper-left corner

            System.Int32 dwRop // raster operation code

        );





        public JMD_CustomerCard_Print()
        {

           


            InitializeComponent();
        }

        private void JMD_CustomerCard_Print_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'jepmdataDataSet.company' table. You can move, or remove it, as needed.
            this.companyTableAdapter.Fill(this.jepmdataDataSet.company);
           

            label3.Text = Publics.Customer_Print_Name;
            label4.Text = Publics.Customer_Print_ID;

            





        }



        public void StartPrint(Stream streamToPrint, string streamType)
        {

            this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);

            this.streamToPrint = streamToPrint;

            this.streamType = streamType;

            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();

            PrintDialog1.AllowSomePages = true;

            PrintDialog1.ShowHelp = true;

            PrintDialog1.Document = printDocument1;

            DialogResult result = PrintDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {

                printDocument1.Print();

                //docToPrint.Print();

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            button1.Visible = false;
            button2.Visible = false;

            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");

            }

            button1.Visible = true;
            button2.Visible = true;
        }
        
        
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {




            System.Drawing.Image image = System.Drawing.Image.FromStream
            this.streamToPrint;

           int x = e.MarginBounds.X;

              int y = e.MarginBounds.Y;

             int width = image.Width;

             int height = image.Height;

            if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))

             {

             width = e.MarginBounds.Width;

                height = image.Height * e.MarginBounds.Width / image.Width;

            }

            else

              {

                   height = e.MarginBounds.Height;

                 width = image.Width * e.MarginBounds.Height / image.Height;

              }

           System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);

    e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
        }
       






    }

}

المشكلة في هذا السطر

            System.Drawing.Image image = System.Drawing.Image.FromStream
            this.streamToPrint;

1- مشكلة هنا System.Runtime.InteropServices.DllImportAttribute

 

وهي

 

 

Error    10    A using namespace directive can only be applied to namespaces; 'System.Runtime.InteropServices.DllImportAttribute' is a type not a namespace 

 

 

 

2- مشكلة هنا

خط احمر تحت System.Drawing.Image.FromStream
            this.streamToPrint;

 

والخطأ

 

Error    11    Only assignment, call, increment, decrement, and new object expressions can be used as a statement   

 

 

وخطأ اخر عند نفس الجملة

 

Error    10    Cannot convert method group 'FromStream' to non-delegate type 'System.Drawing.Image'. Did you intend to invoke the method? 

#2

قمت بعمل عدة محاولات حتي وصلت للصح .. ولكن الكود يطبع جزء صغير من الفورم فقط

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.IO;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Net;
using System.Drawing.Printing;

namespace Jep_MData
{
    public partial class JMD_CustomerCard_Print : Form
    {
       //1
        
        private System.IO.Stream streamToPrint;
        string streamType;

        public JMD_CustomerCard_Print()
        {

            InitializeComponent();
        }


        //2
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt
        (
            IntPtr hdcDest, // handle to destination DC
            int nXDest, // x-coord of destination upper-left corner
            int nYDest, // y-coord of destination upper-left corner
            int nWidth, // width of destination rectangle
            int nHeight, // height of destination rectangle
            IntPtr hdcSrc, // handle to source DC
            int nXSrc, // x-coordinate of source upper-left corner
            int nYSrc, // y-coordinate of source upper-left corner
            System.Int32 dwRop // raster operation code
        );





        private void JMD_CustomerCard_Print_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'jeptusmdataDataSet.company' table. You can move, or remove it, as needed.
            this.companyTableAdapter.Fill(this.jeptusmdataDataSet.company);
            //... سحب بيانات الدخول

            label3.Text = Publics.Customer_Print_Name;
            label4.Text = Publics.Customer_Print_ID;

       
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

       
        private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //3
            System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
            int x = e.MarginBounds.X;
            int y = e.MarginBounds.Y;
            int width = image.Width;
            int height = image.Height;
            if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
            {
                width = e.MarginBounds.Width;
                height = image.Height * e.MarginBounds.Width / image.Width;
            }
            else
            {
                height = e.MarginBounds.Height;
                width = image.Width * e.MarginBounds.Height / image.Height;
            }
            System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
            e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
        }

        public void StartPrint(Stream streamToPrint, string streamType)
        {
            this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
            this.streamToPrint = streamToPrint;
            this.streamType = streamType;
            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
            PrintDialog1.AllowSomePages = true;
            PrintDialog1.ShowHelp = true;
            PrintDialog1.Document = printDoc;

            DialogResult result = PrintDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                printDoc.Print();
                //docToPrint.Print();
            }

        }
        private void button2_Click(object sender, EventArgs e)
        {

            button1.Visible = false;
            button2.Visible = false;

            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");
            }

            button1.Visible = true;
            button2.Visible = true;
        }

        
       

    }

}

مواضيع مشابهة