简体   繁体   中英

C# Graphics.DrawImage truncated/cropped image when printing

Im trying to print an image with C#. The problem is that the image is cropped/truncated when printing. The resolution and width/height are correct since 2 borders match the printed page, but the image is only printed by half.

Printed Image: Printed Image Expected Image: Expected Image

The image is a.png of 6''x5'' (152.4mm x 127mm) but i can convert the image to.tiff (same issue). The printer has the correct paper selected. If i convert the png to pdf and print it with Adobe Reader i have no issues (without C#)

This is part of the code (the implementation is a Class but is only variable initialization)

private void PD_PrintPage(object sender, PrintPageEventArgs e)
{
    try
    {
        if (e != null)
        {
            PD_PrintPage_DrawImage(e);
            PD_PrintPage_Close(e);
        }
    }
    catch (Exception)
    {
        // Error handling.
        e.HasMorePages = false;
        e.Cancel = true;
        _exceptionFlag = true;
    }
}
public Print(String devName, String portName, int totalPrintPage)
{
    try
    {
        // Create the PrintDocumentObject.
        _pdPrint = new PrintDocument();

        // Add the PrintPageEventHandler.
        _pdPrint.PrintPage += new PrintPageEventHandler(PD_PrintPage);

        // Set the using Printer.
        _pdPrint.PrinterSettings.PrinterName = devName;
        _pdPrint.DocumentName = "Label Print";

        _totalPrintPage = 1;        
        
        return;
    }
    catch
    {
        throw;
    }
}

public void DoPrinting()
{
    try
    {
        _pdPrint.Print();
        if(_exceptionFlag == true)
        {
            throw new Exception();
        }
    }
    catch (Exception)
    {
        throw;
    }
}

private void PD_PrintPage_DrawImage(PrintPageEventArgs e)
{
    try
    {
        float width = 152.4F;
        float height = 127.0F;

        // Set page unit.
        e.Graphics.PageUnit = GraphicsUnit.Millimeter;
        
        // String currentDirectryPath = Directory.GetCurrentDirectory();
        String imageFilePath = "C:\\Users\\uservm\\Documents\\interfaces\\label6x5.png";

        //String imageFilePath = Path.Combine(currentDirectryPath, imageFileName);

        using (FileStream fs = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
        using (Image img = Image.FromStream(fs))
        {
            // Draw image.

            e.Graphics.DrawImage(img, 0, 0, width, height);
        }
    }
    catch (Exception)
    {
        // Error handling.
        MessageString.GetSystemError((int)MessageString.STATE_DRAWIMAGE_ERROR);
    }
}

The new Rectangel() method only accepts int as variable but my page size is float . Still, how do i implement it on my funcion? I think the issue is that the draw area is not full.

Any ideas?

That type of result or worse with other formats is normally a result of problems in the form definition, commonly seen when printing a landscape screen on a windows default of portrait and and undersized page. here is exactly same test print input but two different paper output types. 在此处输入图像描述

Acrobat is written by a print house specialist (unlike Microsoft) so their paper output is usually more whats expected. The key to resolution is ensuring all units are known and the image you posted is unitless thus not 6 x 5 which it would almost be (a pixel over in x and y) if set to 100 dpi so perhaps it may help if set to 101 dpi. OR better yet use a ratio of 576 wide and 480 high which will default to 6X5 in many apps. Also double check what windows print management thinks that page layout should be eg ensure it is set to borderless and the correct orientation for the printer driver since some will use roll feed at 90 degrees to normal.

If your FORMS are set correctly it should be no problem printing full media at any orientation so here I defined 6x5 and 5x6 and that is the measured output 在此处输入图像描述

It may help to try a different small format specialist driver, thus for labels "Bartender Seagull" drivers (if available for that printer model) are often recommended.

Disclaimer I support SumatraPDF and that could be modified to suit your printing, HOWEVER it is not a simple task for small formats and usually requires much overriding of windows default settings. Most important DO NOT use -silent switch, especially when print serving.

在此处输入图像描述

Note I had to add a 6x5 Form in Windows print management, and obscurely that is actually defined as 5 inches wide by 6 inches high even though it says 6.00x5.00. since you want it to be applied rotated as Landscape, I then have to ensure the driver is set to Landscape by default. it is likely to fail if needing headless rotation. So now I have my Media structure as paperkind=138 (the number will be different on every machine and I could use setting paper=6x5 , which did work too) numbering is more reliable for some drivers media lists. Also since we know your image is oversized I need to add shrink. By far it is easier to use Acrobats defaults rather than Microsofts, and Acrobat can be left running in standby (/T) so it does not need to slowly reload for each print.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM