Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 
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 24-Aug-12 3:43am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
pieterjann at 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 at 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 at 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 at 24-Aug-12 10:22am
   
I am not sure then.
ryanb31 at 24-Aug-12 10:53am
   
This isn't a solution.
Christian Graus at 24-Aug-12 11:07am
   
Who is stalking whom ? Do you have a problem ? I agree, I ended up not solving it, but I remember from my C++ days that I used to print outside the borders and that is how I printed to the edge when I did something similar.
pieterjann at 24-Aug-12 11:11am
   
Yea i thought of printing outside the borders, but I could not figure how I'd do that... :(
ryanb31 at 24-Aug-12 11:12am
   
"Who is stalking whom ?" Boo.

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

  Print Answers RSS
0 OriginalGriff 500
1 Prakriti Goyal 292
2 Yogesh Kumar Tyagi 224
3 Maciej Los 195
4 CPallini 170
0 OriginalGriff 6,652
1 Sergey Alexandrovich Kryukov 5,449
2 Maciej Los 3,474
3 Peter Leow 3,303
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 24 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100