Autor Beitrag
Fabian W.
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1766

Win 7
D7 PE
BeitragVerfasst: Fr 06.10.06 17:47 
Hi @ all! :)

In meinem Programm erstelle ich auf dem Canvas eines TImage ein BitMap das später über die "Printers"-Unit gedruckt werden soll. Das Drucken realisiere ich so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
  with Printer do
  begin
    //Mit BeginDoc wird ein Druckauftrag initiiert.
    BeginDoc;
    try
      //Informationen über die Auflösung
      ScaleX := GetDeviceCaps(Handle, logPixelsX) div PixelsPerInch;
      ScaleY := GetDeviceCaps(Handle, logPixelsY) div PixelsPerInch;
      //An die Auflösung anpassen
      RR := Rect(0, 0, img_Cache.picture.Width * scaleX, img_Cache.Picture.Height * ScaleY);
      Canvas.StretchDraw(RR, img_Cache.Picture.Graphic);

    finally
      EndDoc;   //Methode EndDoc beendet den aktuellen Druckauftrag und schließt die
      // Textdatei-Variable.
      img_Cache.Destroy;
    end;
  end;
So, mein Problem is jetzt, dass dieser Code das Bild komplett unangepasst druckt. Soll heißen: ist das Bild zu groß wird es einfach abgeschnitten.
Ich suche nun eine Möglichkeit das Bild auf das jeweilige Papierformat aufteilen zu lassen und dann seitenweise zu drucken, falls es zu groß ist...

mfg!
Fabian W. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1766

Win 7
D7 PE
BeitragVerfasst: Sa 07.10.06 11:12 
Ok - ich hab mich jetzt mal im Internet schlau gemacht und bin ein großes Stück weitergekommen. Ich habe jetzt folgenen Code. Er teilt das Bild auf und in der "Doppelschleife" soll das ganze dann gedruckt werden. Nur leider kommen da nur leere Seiten aus dem drucker - CopyRect funzt irgendwie nicht. Fehler werden keine gemeldet.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
procedure THaupt.PrintPlan();
  procedure GetPrinterMargins(var Margins: TPageMargins);
  var   //Druckerränder auslesen
    PixelsPerInch: TPoint;
    PhysPageSize: TPoint;
    OffsetStart: TPoint;
    PageRes: TPoint;
  begin
    PixelsPerInch.y := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
    PixelsPerInch.x := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
    Escape(Printer.Handle, GETPHYSPAGESIZE, 0, nil, @PhysPageSize);
    Escape(Printer.Handle, GETPRINTINGOFFSET, 0, nil, @OffsetStart);
    PageRes.y := GetDeviceCaps(Printer.Handle, VERTRES);
    PageRes.x := GetDeviceCaps(Printer.Handle, HORZRES);
    // Top Margin
    Margins.Top := OffsetStart.y {/ PixelsPerInch.y};
    // Left Margin
    Margins.Left := OffsetStart.x {/ PixelsPerInch.x};
    //  Margin
    Margins.Bottom := ((PhysPageSize.y - PageRes.y) {/ PixelsPerInch.y}) -
      (OffsetStart.y {/ PixelsPerInch.y});
    // Right Margin
    Margins.Right := ((PhysPageSize.x - PageRes.x) {/ PixelsPerInch.x}) -
      (OffsetStart.x {/ PixelsPerInch.x});
  end;
  procedure BitmapDrehen_90Grad(const Bitmap: TBitmap);
  var   //Bitmap um 90° drehen
    P: PRGBQuad; //^THelpRGB;
    x,y,b,h : Integer;
    RowOut: ^TMyHelp;
    help: TBitmap;
  begin
    Bitmap.PixelFormat := pf32bit;
    help := TBitmap.Create;
    try
      help.PixelFormat := pf32bit;
      b := bitmap.Height;
      h := bitmap.Width;
      help.Width := b;
      help.height := h;
      for y := 0 to (h-1) do
      begin
        rowOut := help.ScanLine[y];
        P  := Bitmap.scanline[bitmap.height-1];
        inc(p,y);
        for x := 0 to (b-1) do
        begin
          rowout[x] := p^;
          inc(p,h);
        end;
      end;
    finally
      bitmap.Assign(help);
    end;
  end;
var
  ScaleX, ScaleY, X, Y, XPages, YPages: Integer;
  RR: TRect;
  img_Cache: TImage;
  PaperSize: TPoint;
  PageMargins: TPageMargins;
begin
  //Bild komplett zeichnen...
  img_Cache := TImage.Create(self);
  img_Cache.Visible := false;
  img_Cache.AutoSize := false;
  img_Cache.Transparent := false;
  img_Cache.Picture.Bitmap := OrgPic;
  img_Cache.Picture.Graphic.Width := round(OrgPic.Width * zoom);
  img_Cache.Picture.Graphic.Height := round(OrgPic.Height * zoom);
  img_Cache.Canvas.StretchDraw(Rect(0, 0,
                                    round(OrgPic.Width * zoom),
                                    round(OrgPic.Height * zoom)),
                                    OrgPic);
  For x := 0 to CompoCount do
  begin
    If Sign[x] = nil then
      continue;
    if img_Cache.Picture.Graphic.Width < Sign[x].Left + Sign[x].Width then
      img_Cache.Picture.Graphic.Width := Sign[x].Left + Sign[x].Width+1;
    if img_Cache.Picture.Graphic.Height < Sign[x].Top + Sign[x].Height then
      img_Cache.Picture.Graphic.Height := Sign[x].Top + Sign[x].Height+1;
    Sign[x].PaintTo(img_Cache.Canvas,
                   Sign[x].Left + ScrollBox1.HorzScrollBar.Position,
                   Sign[x].Top + ScrollBox1.VertScrollBar.Position);
  end;
  img_Cache.Canvas.LineTo(0, 0);
  //Bild drehen
  if img_Cache.Picture.Graphic.Width > img_Cache.Picture.Graphic.Height then
    //wenn bild breiter als hoch, dann um 90° im uhrzeigersinn drehen
    BitmapDrehen_90Grad(img_Cache.Picture.Bitmap);
  with Printer do
  begin
    //Mit BeginDoc wird ein Druckauftrag initiiert.
    BeginDoc;
    try
      //Informationen über die Auflösung
      ScaleX := GetDeviceCaps(Handle, logPixelsX) div PixelsPerInch;
      ScaleY := GetDeviceCaps(Handle, logPixelsY) div PixelsPerInch;
      //Papiergröße ermitteln
      GetPrinterMargins(PageMargins);
      PaperSize := Point(round(Printer.PageWidth - (PageMargins.Left + PageMargins.Right)),
                         round(Printer.PageHeight - (PageMargins.Top + PageMargins.Bottom)));
      //Papiergröße an skalierte anassen
      PaperSize := Point(PaperSize.X div scaleX, PaperSize.Y div scaleY);
      //Bild aufteilen falls zu groß
      XPages := round((img_cache.Picture.Graphic.Width / PaperSize.X) + 0.5);
      YPages := round((img_cache.Picture.Graphic.Height / PaperSize.Y) + 0.5);
      Showmessage('Es werden '+inttostr(XPages * YPages)+' Seiten gedruckt!'+#13+inttostr(XPages)+#13+inttostr(YPages));
      //An die Auflösung angepasst seite für Seite drucken
      For x := 1 to XPages do
      begin
        For y := 1 to YPages do
        begin
          RR := Rect(0, 0, img_Cache.picture.Width * scaleX, img_Cache.Picture.Height * ScaleY);
          //Canvas.StretchDraw(RR, img_Cache.Picture.Graphic);
          canvas.CopyRect(RR, img_Cache.Canvas,
                          Rect((img_cache.Picture.Graphic.Width div XPages) * X,
                               (img_cache.Picture.Graphic.Height div YPages) * Y,
                               (img_cache.Picture.Graphic.Width div XPages) * (X + 1),
                               (img_cache.Picture.Graphic.Height div YPages) * (Y + 1)));
          if (x * y) < (XPages * YPages) then  //wenn letzte Seite noch nicht erreicht
            NewPage;                           //eine neue erstellen
        end;
      end;

    finally
      EndDoc;   //Methode EndDoc beendet den aktuellen Druckauftrag und schließt die
      // Textdatei-Variable.
      //Abort;    //!!!!!!!!
      img_Cache.Picture.SaveToFile('c:\test.bmp');//Ausgabe in datei (zum testen)
      img_Cache.Destroy;
    end;
  end;
end;
Vlt sieht ja jm nen Fehler im Code...
Fabian W. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1766

Win 7
D7 PE
BeitragVerfasst: Mo 09.10.06 18:52 
*nochmal push*
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mo 09.10.06 19:06 
Dann zeichne doch erstmal statt aufs Canvas des Printers auf ein Image. Da kannste herumprobieren, ohne ständig Papier zu 'verschwenden'. Zwar auch keine Hilfe, maybe aber ein nützliche Tipp. ;)

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 09.10.06 19:10 
Hmm, also mit StretchDraw oder CopyRect hab ich noch nicht gearbeitet. Aber wie wärs mit BitBlt? Das hat bei mir bisher immer funktioniert.

Aber ich seh mir den Quelltext auch nochmal an. Ich hab ihn eben nur überflogen...
Fabian W. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1766

Win 7
D7 PE
BeitragVerfasst: Mo 09.10.06 19:55 
Ich zeichne doch auf das Canvas des images - nur klappt das kopieren der canvasteile von image auf printer ned (glaub ich^^)

BitBlt - muss ich mich mal schlau machen kannte ich ncoh nich...; vlt findste ja auch end Fehler im Code noch ;) ^^

mfg
Fabian W. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1766

Win 7
D7 PE
BeitragVerfasst: Fr 13.10.06 14:18 
user profile iconjaenicke hat folgendes geschrieben:
Aber ich seh mir den Quelltext auch nochmal an. Ich hab ihn eben nur überflogen...
Haste dir'n jetzt mal angeschaut? :)

Ich hab mich mal über BitBlt "schlau" gemacht und werde das ganze mal probieren!
//Edit ich merke grade BitBlt(Ziel.Handle, Ziel_x, Ziel_y, Ziel_Weite, Ziel_Höhe, Quelle.Handle, Quelle_x, Quelle_y, Kombinationscode);
Da ich nur ein bestimmtes Rechteck kopieren möchte bräuchte ich ja noch Quelle_Weite und -_Höhe bräuchte?! So habe ich für BitBlt keine Verwendeung...

Das kann doch ned so schwer sein das Ding zum Laufen zu bekommen - sieht niemand 'nen Fehler??


Zuletzt bearbeitet von Fabian W. am Fr 13.10.06 16:15, insgesamt 1-mal bearbeitet
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 13.10.06 15:19 
user profile iconFabian W. hat folgendes geschrieben:
user profile iconjaenicke hat folgendes geschrieben:
Aber ich seh mir den Quelltext auch nochmal an. Ich hab ihn eben nur überflogen...
Haste dir'n jetzt mal angeschaut? :)

Sorry, hab ich total vergessen! Mache ich jetzt sofort!
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 13.10.06 17:53 
Ähh, wäre schön, wenn du deine eigenen Typ-Definitionen auch angeen würdest (Ich meine TPageMargins = TRect und so)...
Was TMyHelp sein soll oder woher OrgPic oder Sign kommen weiß ich nicht...

Ich würde das nämlich gerne mal selber ausprobieren, was da passiert...
Vom reinen Ansehen finde ich da glaube ich nicht raus, was da nicht geht...

//EDIT:
Das Problem ist gelöst, es lag an den Source- und Dest-Rechtecken, die nicht korrekt berechnet wurden. Das nur, damit niemand sich mehr den Kopf zerbricht...