Click here to Skip to main content
15,886,791 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to print an image using a labelprinter. I created the image but when I print it, a page border of like 1 cm is placed around the image which means half my label is empty. How can I disable these borders and sent the raw image to the printer?

As my label printer can operate as a normal windows printer I am using it like that. I want to create a windows print job to print the image.

C#
namespace PrintSample 
{ 
public partial class PrintForm : Form 
{ 
private System.IO.Stream streamToPrint; string streamType;

    public PrintForm()
    {
        InitializeComponent();
    }

    [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    private static extern bool BitBlt
    (
        IntPtr hdcDest, // handle to destination DC
        int nXDest, // x-coord of destination upper-left corner
        int nYDest, // y-coord of destination upper-left corner
        int nWidth, // width of destination rectangle
        int nHeight, // height of destination rectangle
        IntPtr hdcSrc, // handle to source DC
        int nXSrc, // x-coordinate of source upper-left corner
        int nYSrc, // y-coordinate of source upper-left corner
        System.Int32 dwRop // raster operation code
    );

    private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
        int x = e.MarginBounds.X;
        int y = e.MarginBounds.Y;
        int width = image.Width;
        int height = image.Height;
        if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
        {
            width = e.MarginBounds.Width;
            height = image.Height * e.MarginBounds.Width / image.Width;
        }
        else
        {
            height = e.MarginBounds.Height;
            width = image.Width * e.MarginBounds.Height / image.Height;
        }
        System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
        e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
    }

    public void StartPrint(Stream streamToPrint, string streamType)
    {
        this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
        this.streamToPrint = streamToPrint;
        this.streamType = streamType;            
        System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
        PrintDialog1.AllowSomePages = true;
        PrintDialog1.ShowHelp = true;
        PrintDialog1.Document = printDoc;

        DialogResult result = PrintDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            printDoc.Print();
        }

    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
        FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
        PaperSize.Equals(1, 1.5);
        StartPrint(fileStream, "Image");
        fileStream.Close();

    }
}
}
Posted

1 solution

This code looks sensible. It's my recollection that you can defeat the margins simply by printing outside them, have you tried that ? I also thought the margins were something that can be set in the print dialog box.
 
Share this answer
 
Comments
pieterjann 24-Aug-12 10:11am    
Yes, I tried that, and this messes up the scale, flips the image etc. When I print the image using Windows Photo Viewer there is no problem the image is perfect, I want that result in my application
Christian Graus 24-Aug-12 10:16am    
What if you change the margins in the print dialog, either before it's shown, or in the dialog ?
pieterjann 24-Aug-12 10:21am    
they are already at 0 mm. What happens in my program is as follows: When the print function is called the image is placed on a page, and that page is sent to the printer. The size of this page is not set by the printer page size settings. It is set somewhere else and I can not figure where.
Christian Graus 24-Aug-12 10:22am    
I am not sure then.
ZurdoDev 24-Aug-12 10:53am    
This isn't a solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900