打印字符串和照片

时间:2014-01-18 10:23:53

标签: c# .net winforms printing

我正在尝试打印一些字符串和照片。我有一个包含这些控件的面板。我在下面添加我的代码。在代码中,DataTable包含字符串格式的控件的位置和大小。

第一个是面板和控件的图像。第二张图片显示了PrintPreviewDialog在运行代码后显示document的方式。

图片和代码张贴在下面。

private void printMenu_Click(object sender, EventArgs e)
{
    printPreviewDialog.Document = printDocument;
    printPreviewDialog.ShowDialog();
}

private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    var cvt = new FontConverter();

    int resX = PrnOpra.GetPrinterResolutionX(this.selectPrinterComboBox);
    int resY = PrnOpra.GetPrinterResolutionY(this.selectPrinterComboBox);
    Graphics g = e.Graphics;

    float scale = resX / ScrnRes;
    int bitmapHeight = 3517;
    int bitmapWidth = 1020;
    Bitmap bm = new Bitmap(bitmapWidth, bitmapHeight);

    g.DrawImage(System.Drawing.Image.FromFile(Application.StartupPath + @"\Resources\Item_Photos\" + dtItemsToBePrinted.Rows[u][c].ToString()),
                            pointItemPhoto.X, pointItemPhoto.Y, sizeItemPhoto.Width, sizeItemPhoto.Height);
    g.DrawString(dtItemsToBePrinted.Rows[u][c].ToString(),
                        cvt.ConvertFromString(font) as Font,
                        new SolidBrush(Color.Black),
                        pointFieldLabel.X, pointFieldLabel.Y,
                        new StringFormat());

    g.DrawImage((System.Drawing.Image)Properties.Resources.ResourceManager.GetObject(dtControls.Rows[u]["image"].ToString()),
                        float.Parse(dtControls.Rows[u]["locationX"].ToString(), CultureInfo.InvariantCulture.NumberFormat),
                        float.Parse(dtControls.Rows[u]["locationY"].ToString(), CultureInfo.InvariantCulture.NumberFormat),
                        float.Parse(dtControls.Rows[u]["width"].ToString(), CultureInfo.InvariantCulture.NumberFormat),
                        float.Parse(dtControls.Rows[u]["height"].ToString(), CultureInfo.InvariantCulture.NumberFormat));

     g.DrawString(dtControls.Rows[u]["text"].ToString(),
                        cvt.ConvertFromString(dtControls.Rows[u]["font"].ToString()) as Font,
                        new SolidBrush(Color.Black),
                        float.Parse(dtControls.Rows[u]["locationX"].ToString(), CultureInfo.InvariantCulture.NumberFormat),
                        float.Parse(dtControls.Rows[u]["locationY"].ToString(), CultureInfo.InvariantCulture.NumberFormat),
                        new StringFormat());

}

enter image description here

enter image description here

正如您在图像中看到的那样,Panel的格式非常合理,但在打印控件时会出错。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

我弄错了。我已将错误的位置值保存到datatable。我将Control.Top保存到Location.X,将Contorl.Left保存到Location.Y。现在印刷得很好。