当前位置: 动力学知识库 > 问答 > 编程问答 >

.net - C# print of Graphics.PageUnit millimeter

问题描述:

void Main()

{

PrintDocument docToPrint = new PrintDocument();

docToPrint.PrintPage += FontToPrint_PrintPage;

docToPrint.Print();

}

private void FontToPrint_PrintPage(object sender, PrintPageEventArgs e)

{

//设置单位为厘米后,第一次打印位置正确

//第二次打印所在位置缩小100倍,第三次继续缩小

e.Graphics.PageUnit = GraphicsUnit.Millimeter;

Font font = new Font("Arial", 9, FontStyle.Bold);

foreach (PrintDataBase model in printModel.PrintList)

{

e.Graphics.DrawString(model.Value, font, myBrush, model.XPosition, model.YPosition);

}

}

This is first print:

This is second print:

The content is becoming smaller and smaller After i set e.Graphics.PageUnit = GraphicsUnit.Millimeter;

I've been beating my brains out with it, but I just can't solve it.

网友答案:

I am sure it is not caused by the model,but caused by the unit Millimeter.still now I don't know how to solve this .

So the compelet code as blows :

public class PrintEntity {

    public bool HorizontalFlip { get; private set; }
    public bool VerticalFlip { get; private set; }
    public float PageSizeW { get; private set; }
    public float PageSizeH { get; private set; }
    public List<PrintDataBase> PrintList { get; private set; }
    public void SetValue(float _PageSizeW,float _PageSizeH,List<PrintDataBase> _PL
        ,bool _HF=false,bool _VF=false)
    {
        HorizontalFlip = _HF;
        VerticalFlip = _VF;
        PageSizeW = _PageSizeW;
        PageSizeH = _PageSizeH;
        PrintList = _PL;
    }
}
   public class PrintDataBase {
    public int PType { get; private set; }
    public float XPosition { get; private set; }
    public float YPosition { get; private set; }
    public bool IsEnable { get; private set; }
    public int FontSize { get; private set; }
    public bool IsBold { get; private set; }
    public int Width { get; private set; }
    public int Height { get; private set; }
    public string Value { get; private set; }
    public void SetValue(float _XPosition,float _YPosition,int _Width,int _Height,
        int _FontSize, string _Value, int _PType = 0, bool _IsBold = false, bool _IsEnable = false)
    {
        XPosition = _XPosition;
        YPosition = _YPosition;
        Width = _Width;
        Height = _Height;
        FontSize = _FontSize;
        PType = _PType;
        IsBold = _IsBold;
        IsEnable = _IsEnable;
        Value = _Value;
    }
}

private void FontToPrint_PrintPage(object sender, PrintPageEventArgs ev)
        {
            Graphics e = ev.Graphics;
            float pdix =  e.DpiX / 25.4F;
            float pdiy =  e.DpiY / 25.4F;
            e.PageUnit = GraphicsUnit.Millimeter;
            Font font = new Font("Arial", 20, FontStyle.Bold);
            SolidBrush brush = new SolidBrush(Color.Black);
            printModel.PrintList.RemoveAll(m => (m.IsEnable == false));

            foreach (PrintDataBase model in printModel.PrintList)
            {
                if (model.PType == 2)
                {
                    //Create QR Code Form Google.zxing
                    Bitmap codeImg = CreatCodeImg(model.Value, (int)(model.Width * pdix), (int)(model.Height * pdiy));
                    e.DrawImage(codeImg, model.XPosition, model.YPosition, model.Width, model.Height);
                    continue;
                }
                if (model.IsBold)
                {
                    font = new Font("Arial", model.FontSize, FontStyle.Bold);
                }
                else
                {
                    font = new Font("Arial", model.FontSize);
                }
                e.DrawString(model.Value, font, brush, model.XPosition , model.YPosition);
            }
        }
分享给朋友:
您可能感兴趣的文章:
随机阅读: