using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private MyPanel IPanel;
private Button[] Btns = new Button[2];
public Form1()
{
InitializeComponent();
IPanel = new MyPanel(this);
this.StartPosition = FormStartPosition.Manual;
IPanel.Location = new Point(10, 10);
this.ClientSize = new Size(IPanel.Width + 20, IPanel.Height + 100);
string[] BtnsTxt = {"Print", "Preview"};
for (int I = 0; I < Btns.Length; I++)
{
Btns[I] = new Button();
Btns[I].Text = BtnsTxt[I];
Btns[I].Top = IPanel.Bottom + 10;
Btns[I].Left = this.ClientSize.Width - (I + 1) * (Btns[0].Width + 20);
Btns[I].Click += ff_Click;
Btns[I].Parent = this;
}
}
private void ff_Click(object sender, EventArgs e)
{
if (((Button)sender).Text == "Preview")
{
IPanel.Preview();
}
else
{
IPanel.Print();
}
}
}
internal class MyPanel : Panel
{
internal Bitmap Bmp;
internal string[] T = { "RHC-IN-000089", "MUHOZI DAVID", "08-Oct-1991", "MALE", "PC069663" };
private PrintPreviewDialog PPDlg = new PrintPreviewDialog();
private PrintDocument PD = new PrintDocument();
public MyPanel(Form Parent)
{
this.Size = new Size(480, 340);
this.BackColor = Color.White;
Bmp = new Bitmap("desert.jpg"); //the bugg is pointed here
Bmp = Bmp.Clone(new Rectangle(85, 140, 80, 120), Bmp.PixelFormat);
this.Parent = Parent;
PD.OriginAtMargins = true;
PD.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
PD.DefaultPageSettings.PaperSize = new PaperSize("ICard", 480, 340);
PPDlg.Document = PD;
PD.PrintPage += PD_PrintPage;
}
public void Preview()
{
PPDlg.ShowDialog();
}
public void Print()
{
PD.Print();
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
DrawCard(e.Graphics);
}
public void PD_PrintPage(object sender, PrintPageEventArgs e)
{
DrawCard(e.Graphics);
}
public void DrawCard(Graphics G)
{
int X = 260;
int Y = 130;
G.DrawImage(Bmp, 75, 130);
for (int I = 0; I < T.Length; I++)
{
G.DrawString(T[I], this.Font, Brushes.Black, X, Y);
X = 200;
Y += 30;
}