Bild drucken

Jensi

Grünschnabel
Hallo.
Ich habe ein neues Problem. Und zwar möchte ich ein Bild drucken, dass ich in meine Picturebox geladen habe.
Ich habe das mal so probiert:
Code:
private void image_print(object sender, PrintPageEventArgs e)
	{
	e.Graphics.DrawImage();
	}
private void menuItem5_Click(object sender, System.EventArgs e)
	{
		PrintDocument pd = new PrintDocument();
		pd.PrintPage += new PrintPageEventHandler(image_print);
		pd.Print();
	}
Funktioniert aber leider nicht, was ich mir aber schon dachte. :(
Kann mir da vielleicht einer bei helfen, damit ich es richtig mache?

Danke

Jens
 
Viel Spaß damit :)

Code:
using System;
using System.Windows.Forms;
using Microsoft;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;

public class PrintingExample : System.Windows.Forms.Form 
{
	private System.Windows.Forms.PictureBox pictureBox;
	private System.Windows.Forms.Button printButton;
    private Font printFont;
    

   public PrintingExample() : base() 
   {
      // The Windows Forms Designer requires the following call.
      InitializeComponent();
   }

   // The PrintPage event is raised for each page to be printed.
   private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
   {
      float linesPerPage = 0;
      
      float leftMargin = ev.MarginBounds.Left;
      float topMargin = ev.MarginBounds.Top;
      string line = null;

      // Calculate the number of lines per page.
      linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

	  Image graph = pictureBox.Image;
   	  ev.Graphics.DrawImage(graph, graph.Width, graph.Height);
      // Print each line of the file.
      //while(count < linesPerPage && ((line=streamToPrint.ReadLine()) != null)) 
      //{
      //   yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
      //   ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
      //   count++;
      //}

      // If more lines exist, print another page.
      if(line != null)
         ev.HasMorePages = true;
      else
         ev.HasMorePages = false;
   }


   // The Windows Forms Designer requires the following procedure.
   private void InitializeComponent() {
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PrintingExample));
			this.printButton = new System.Windows.Forms.Button();
			this.pictureBox = new System.Windows.Forms.PictureBox();
			this.SuspendLayout();
			// 
			// printButton
			// 
			this.printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.printButton.Location = new System.Drawing.Point(360, 328);
			this.printButton.Name = "printButton";
			this.printButton.Size = new System.Drawing.Size(136, 40);
			this.printButton.TabIndex = 0;
			this.printButton.Text = "Print the file.";
			this.printButton.Click += new System.EventHandler(this.PrintButtonClick);
			// 
			// pictureBox
			// 
			this.pictureBox.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox.Image")));
			this.pictureBox.Location = new System.Drawing.Point(0, 8);
			this.pictureBox.Name = "pictureBox";
			this.pictureBox.Size = new System.Drawing.Size(496, 312);
			this.pictureBox.TabIndex = 1;
			this.pictureBox.TabStop = false;
			// 
			// PrintingExample
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(504, 381);
			this.Controls.Add(this.pictureBox);
			this.Controls.Add(this.printButton);
			this.Name = "PrintingExample";
			this.Text = "Print Example";
			this.Load += new System.EventHandler(this.PrintingExampleLoad);
			this.ResumeLayout(false);
		}

   // This is the main entry point for the application.
   public static void Main(string[] args) 
   {
      Application.Run(new PrintingExample());
   }

void PrintingExampleLoad(object sender, System.EventArgs e)
{
	
}

void PrintButtonClick(object sender, System.EventArgs e)
{
	try 
      {
          try 
          {
             printFont = new Font("Arial", 10);
             PrintDocument pd = new PrintDocument();
             pd.PrintPage += new PrintPageEventHandler
                (this.pd_PrintPage);
             pd.Print();
          }  
          catch (Exception ex1) {
          	MessageBox.Show(ex1.Message);
          }
      }  
      catch(Exception ex) 
      {
          MessageBox.Show(ex.Message);
      }
}

}

Neue Windows-Application erstellen, Source über alles drüberkopieren, der PictureBox ein Image zuweisen und drucken. Den Rest kannst dir da selber rausholen :)

Und mit den Koordinaten wie das Teil ausgedruckt werden soll musst Dich noch ein wenig rumspielen ... aber Drucken an sich geht ohne Troubles.
 
Zuletzt bearbeitet:
Hallo Norbert,

zunächst danke für deine tollen Beiträge in diesem Forum - da hab ich schon ne Menge raus geholt :)

Bei diesem Tipp habe ich in meiner Applikation ein Problem, evtl. kannst du mir einen Tipp geben:

Das Bild, dass ich in der pictureBox darstelle ist größer als die Box und wird kleiner skaliert dargestellt. Wenn ich nun mit deinem Code arbeite, kommt
nur eine leere Seite aus dem Drucker - Wenn ich ein Bild lade, dass kleiner/gleichgroß der Box ist, dann kommt das Bild raus.

Hast du eine Idee, woran das liegen kann?

Danke OliWan
...may the force be with you
 
Es gibt ne Methode PrintImageScaled oder so ähnlich .. Guck dir die mal an. So kannst dir Die Ränder des Papier-Formates besorgen und dann entsprechend skalieren.
 
Hallo, hab hier ein kleines Porblem:
Meine Situation:
Ich zeichne auf die PictureBox mit Hilfe von
Code:
Graphics g;
g = pictureBox1.CreateGraphics();

Wenn ich jetzt deinen Code hier benutze, sagt der Compiler mir
"Object reference not set to an instance of an object."
wenn ich mir pictureBox1.Image im debugger angucken sagt der mir das es nicht definiert ist.
Wie kann ich jetzt die Sachen in meiner Picturebox drucken?

Gruß
Jan
 
hab die Lösung
hab einfach in deinem Code gesagt:
Code:
g = ev.Graphics;
zeichnen();
g = pictureBox1.CreateGraphics();
wobei zeichnen() meine Funktion ist die normal über g auf die picturebox zeichnet.
Klappt alles wunderbar :)

Danke für die Hilfe :p
mfg
jan
 

Neue Beiträge

Zurück