2016年4月25日 星期一

C# 列印圖片、列印文字、列印圖片及文字


要列印資料,需要設定 PrintPageEvent 以及在內使用,其PrintPageEvent 在 printDocument 的元件中,需要將其拖曳至 Windows Form。

文字列印:

printDocument1.PrintPage += printPageEvent;
private void printPageEvent(object sender, System.Drawing.Printing.PrintPageEventArgs e){
    Graphics g = e.Graphics;
    SolidBrush pb = new SolidBrush(Color.Black);
    Single left = printDocument1.DefaultPageSettings.Margins.Left - 10;
    Single top = printDocument1.DefaultPageSettings.Margins.Top - 20;
    g.DrawString("Enchanté", new Font("微軟正黑體",pb,left,top));
}
列印圖片:
void printPage(object sender,PrintPageEventArgs e){
    Point location = new Point(0,0);
    Image img = Image.FromFile(@"C:\Test.jpg");
    e.Graphics.DrawImage(img,location);
}
圖片下方加入文字,再列印:
void printPage(object sender,PrintPageEventArgs e){
    Bitmap img = new Bitmap(xx,xxHeight + 80); // set height to 80, may text can show under the picture
    Graphics g = ...
    // .... image
    Font f = new Font("微軟正黑體", 18, FontStyle.Italic | FontStyle.Bolid);
    String text = "Ravi de vous rencontrer!";
    g.MeasureString(text,f);
    g.DrawString(text, f , Brushes.Black,this.Width - 500,this.Height + 8);
    
    Point location = new Point(0,0);
    e.Graphics.DrawImage(img,location);
}

沒有留言:

張貼留言

© ERIC RILEY , 自由無須告知轉貼
Background Japanese Sayagata by Olga Libby