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
|
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.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Drawing.Text;
// ajouter control :
//- bouton
//- printDocument
//- printPreviewDialog
namespace WinPrinting
{
public partial class Form1 : Form
{
//un bitmap pour dessiner le texte
//en 2 passes (normal et rotated)
private Bitmap bmp ;
private string monTexte = "Mathieu";
//selon toi largeur =128 mm,hauteur =82 mm
private Rectangle rect = new Rectangle(0, 0, 128, 82);
public Form1()
{
InitializeComponent();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(bmp,new Point(0,0));
e.HasMorePages = false;
}
private void btnPrint_Click(object sender, EventArgs e)
{
//unites en millimetres pour l'image
//page selon toi largeur =210 mm,hauteur =297 mm
bmp = new Bitmap(2100, 2970);
DrawTextSurBmp();
printPreviewDialog1.Document= printDocument1;
printPreviewDialog1.Show();
}
//dessin sur bitmap
private void DrawTextSurBmp()
{
Graphics g = Graphics.FromImage(bmp);
//unite de page en mm
g.PageUnit = GraphicsUnit.Millimeter;
g.Clear(Color.Yellow);
// 1ere passe (text normal)
int counter = 0;
while (counter < 3)
{
DrawString(g, monTexte, rect);
//Incremente Coord Y de Height;
rect.Y += rect.Height;
counter++;
}
// 2e passe
// reset counter
counter = 0;
// reset Origin du rectangle
rect.Location = new Point(0,0);
Matrix myMatrix = new Matrix();
//text deplace et rotated
//Move origin to (128 + 82, 0)
myMatrix.Translate(128 + 82, 0);
myMatrix.Rotate(90);
g.Transform = myMatrix;
while (counter < 3)
{
DrawString(g, monTexte, rect);
// Incremente Coord X de Width les axes
//ayant subi une rotation
rect.X += rect.Width ;
counter++;
}
}
//dessine le texte
private void DrawString(Graphics g, string str, Rectangle rect)
{
Font myFont = new Font("Arial", 28, FontStyle.Bold);
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint=TextRenderingHint.ClearTypeGridFit;
StringFormat fmt = new StringFormat();
fmt.Alignment = StringAlignment.Center;
fmt.LineAlignment = StringAlignment.Center;
g.DrawRectangle(Pens.Black, rect.X, rect.Y,
rect.Width, rect.Height);
g.DrawString(str, myFont, Brushes.Red, rect, fmt);
}
}
} |
Partager