将面板打印到打印机 [英] Printing a panel to a printer

查看:155
本文介绍了将面板打印到打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个面板(及其内容)打印到打印机上。
我在网上看到不同的帖子,但我无法打印面板并获得正确的尺寸。例如,我想打印一个面板并获得80毫米x 40毫米的输出尺寸:

  private void Print_Click(object sender,EventArgs e)
{
int pixelsWidth = 300; // 300像素=〜8cm
int pixelsHeight = 150; // 150 pixels =〜4cm
panelLabel.Size = new Size(pixelsWidth,pixelsHeight);

PrintPanel();
}

private void PrintPanel()
{
System.Drawing.Printing.PrintDocument doc = new PrintDocument();
doc.PrintPage + =新的PrintPageEventHandler(doc_PrintPage);
doc.Print();

$ b private void doc_PrintPage(object sender,PrintPageEventArgs e)
{
位图bmp = new Bitmap(panelLabel.Width,panelLabel.Height);
panelLabel.DrawToBitmap(bmp,new Rectangle(0,0,panelLabel.Width,panelLabel.Height));
RectangleF bounds = e.PageSettings.PrintableArea;

e.Graphics.PageUnit = GraphicsUnit.Millimeter;
e.Graphics.DrawImage(bmp,bounds.Left,bounds.Top,bounds.Width,bounds.Height);
}


解决方案



唯一缺少的是为位图设置分辨率

如果你不这样做,它会从屏幕上复制出来,这通常不适用于打印机。通常导致输出太小,但是因为您已经将 Graphics 设置为使用毫米,所以我们需要调整位图以理解像素应该转换为什么。

假设二次像素尝试这样做:

  int pixelsWidth = 300; // 300像素=〜8cm 
int pixelsHeight = 150; // 150像素=〜4cm
位图bmp =新位图(pixelsWidth,pixelsHeight);
// ..

float targetWidthInInches = 80f / 25.4f;
float dpi = 1f * pixelsWidth / targetWidthInInches;

bmp.SetResolution(dpi,dpi);


I am trying to print a panel (with its contents) to a printer. I saw different posts on the net, but I am not able to print the panel and get the correct size. The panel is getting printed very large and not as expected.

For example, I want to print a panel and get as output size 80mm X 40mm:

    private void Print_Click(object sender, EventArgs e)
    {
        int pixelsWidth = 300;   // 300 pixels= ~8cm 
        int pixelsHeight = 150;  // 150 pixels= ~4cm            
        panelLabel.Size = new Size(pixelsWidth,pixelsHeight);  

        PrintPanel();
    }

    private void PrintPanel()
    {
        System.Drawing.Printing.PrintDocument doc = new PrintDocument();
        doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
        doc.Print();
    }

    private void doc_PrintPage(object sender, PrintPageEventArgs e)
    {               
        Bitmap bmp = new Bitmap(panelLabel.Width, panelLabel.Height);
        panelLabel.DrawToBitmap(bmp, new Rectangle(0, 0, panelLabel.Width, panelLabel.Height));
        RectangleF bounds = e.PageSettings.PrintableArea;

        e.Graphics.PageUnit = GraphicsUnit.Millimeter;
        e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, bounds.Width, bounds.Height);
    }

解决方案

You have set up things almost right.

The only thing missing is setting the resolution for the Bitmap.

If you don't do that is gets copied from the screen, which will usually not work well with the printer. Often resulting in way too small output, but as you have already set the Graphics to use millimeters we need to adapt the bitmap to understand what the pixels are supposed to translate to.

Assuming quadratic pixels try this :

int pixelsWidth = 300;   // 300 pixels= ~8cm 
int pixelsHeight = 150;  // 150 pixels= ~4cm  
Bitmap bmp = new Bitmap(pixelsWidth, pixelsHeight);
//..    

float targetWidthInInches = 80f / 25.4f;
float dpi = 1f * pixelsWidth / targetWidthInInches;

bmp.SetResolution(dpi, dpi);

这篇关于将面板打印到打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆