Issue printing image as expected - c#

I have an image that I'm trying to print to legal size. However, there are a few challenges to this.
The image will vary in size. This is because I'm using a control that has limited print options but can export to an image.
I want to maximize the print area on the page. Smallest margin possible
The user needs to be able to select a printer and set options
Here's the code I'm using:
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile(Globals.TempDirectory + #"\temp.jpg");
Point p = new Point(100, 100);
Margins margins = new Margins(50, 50, 50, 50);
pd.DefaultPageSettings.Margins = margins;
args.Graphics.DrawImage(i, p);
};
pd.Print();
I've been having trouble with this because I can't set margins and can't seem to get the print out right. I want it to print in legal but when I print the image, it's not rotated properly and it just prints to a default printer. I'm up for anything to get this to work.

Printing in C# sucks
try a
printdialog()
to allow the user to select a printer and settings. once you get that to work the rest of it might click for you.
Edit: Showing you where and how to use it.
PrintDialog pDialog = new PrintDialog();
if (pDialog.ShowDialog() == DialogResult.OK)
{
pd.PrinterSettings = pDialog.PrinterSettings;
pd.Print();
}

Related

Printing to a smartcard printer not working, ideas?

I'm trying to print userdata to a smartcard using a smartcard printer but the result is that i'm getting blank cards out of the printer. I've been stuck on this problem for a day now and there is no(findable) answer on the internet.
With printing to the printer (it's an Datacard SP35 Plus) using no custom papersize and no printdialog. the result is an empty card.
using (PrintDocument pd = new PrintDocument())
{
pd.PrintPage += (object sender, PrintPageEventArgs e) =>
{
Image i = Image.FromFile("E:\\tmp.png");
e.Graphics.DrawImage(i, e.MarginBounds);
};
pd.Print();
}
The image is present and visible and plenty of size.
Furthermore, I came across a post that said that the size of the printdocument has to be set
PaperSize papersize = new PaperSize("Custom", Convert.ToInt32(widthInInch * 100), Convert.ToInt32(heightInInch * 100));
pd.DefaultPageSettings.PaperSize = papersize;
pd.PrinterSettings.DefaultPageSettings.PaperSize = papersize;
But didnt result in a visible print.
I've also tried
e.Graphics.DrawImage(i, 0, 0);
instead of
e.Graphics.DrawImage(i, e.MarginBounds);
but didn't result in a visible print.
The widthInInch is 3.38 and the heightInInch is 2.13(The default size of an CR80 card). This also resulted in an empty card.
When I print to PDF the result is visible and correct(I do not know the whitespace, of course).
Does somebody see the problem or has worked out something similar?
I "fixed" it by rewriting my application to UWP. This uses the new print function and made it work as intended.

Scaling WPF Window for Printing

Good Evening,
I am working on a small WPF app in VS2017 which uses c#. Its essentially just to make calculations for my workers in the field. I have everything built and tested, however, I am running into an issue when it comes to printing the results. I have scoured the corners of the internet and have gotten as far as getting the print dialog box to open and even print to PDF. The issue i am having is that when it prints, it is in full scale. I just need to be able to scale the app window to 80-90% of its size and then print. i will add the code and see if i am just overlooking something.
private void InvokePrint(object sender, RoutedEventArgs e)
{
PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
if (printDlg.ShowDialog() == true)
{
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min((ActualWidth*.9),ActualHeight);
//get the size of the printer page
Size sz = new Size((Width*.9), ActualHeight);
//update the layout of the visual to the printer page size.
this.Measure(sz);
this.Arrange(new Rect(new Point(0,0), sz));
//now print the visual to printer to fit on the one page.
printDlg.PrintVisual(this, "Offset Calculations");
What it is printing
What I want to print
add reference to ReachFramework.dll 4.0
PrintDialog dlg = new PrintDialog();
if ((bool)dlg.ShowDialog().GetValueOrDefault()) {
//switch landscape
dlg.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape;
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = dlg.PrintQueue.GetPrintCapabilities(dlg.PrintTicket);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight / this.ActualHeight);
//Transform the Visual to scale
this.LayoutTransform = new ScaleTransform(scale, scale);
//get the size of the printer page
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
//update the layout of the visual to the printer page size.
this.Measure(sz);
this.Arrange(new Rect(new System.Windows.Point((int)capabilities.PageImageableArea.OriginWidth, (int)capabilities.PageImageableArea.OriginHeight), sz));
//show the print dialog
dlg.PrintVisual(this, "MyDoc_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"));
}

In C#, what's the difference of PrinterSettings from a PrintDocument and PrintDialog?

Well, it's a very strange problem, and I already found a black magical solution. I'm curious about the reason.
I'm writing a program which use Brother QL-700 label printer to print labels. I need the labels be printed without showing the printer dialog.
The label printer support different size of label rolls and the default size of label roll is 29mm, while what I need is 62mm. I found that I can set the Page Size by
PrintDocument doc = new PrintDocument();
PaperSize size = new PaperSize() ;
size.Width = 244;//2.44 inch = 62mm
size.Height = 244;
size.RawKind = 256;//RawKind=0 does not work, I don't know why
doc.PrinterSettings.DefaultPageSettings.PaperSize = size;
However, this won't work, the driver of Printer would show a message saying the width does not fit.
But if I change the copy the PrinterSettings from a PrintDialog(), without showing it,
PrintDialog dlg = new PrintDialog();
doc.PrinterSettings = dlg.PrinterSettings;
Then it works.
In conclusion, what I don't understand is why
size.RawKind = 256;
and
PrintDialog dlg = new PrintDialog();
doc.PrinterSettings = dlg.PrinterSettings;
can make the printer work?
The documentation for RawKind does not state that 0 is a valid value. It also states that any value above 118 indicates a custom paper size. Since you are specifying a custom paper size, the value of 256 indicates that the size is custom and that is why it works.

C# printing page height according to the content

I'm doing some printing work in c# and got a small problem. I'm working with a thermal receipt printer. I want to set the height of the page according to the content of it. Which means, when I have fewer items, the page should be smaller and when I have long list of items, the page should grow accordingly.
I tried to set it with PrintPageEventArgs but this didn't result in satisfied result. How can this be done?
Thanks
Just take care of the width. Printer will cut the paper after the last printed element on the last page.
Just for the record. I had the same issue.
At the end what i had done is:
Int Line= starting position (in pixels)
For each line I want to print -> g.DrawString("text",font,xx,margin,line);
and then just before print
ps.Height = Line;
pd.Print();
pd is -> PrintDocument pd = new PrintDocument();
ps is -> PaperSize ps = new PaperSize("",my_width,1));
`enter code here` pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.PrintController = new StandardPrintController();
pd.DefaultPageSettings.Margins.Left = 0;
pd.DefaultPageSettings.Margins.Right = 0;
pd.DefaultPageSettings.Margins.Top = 0;
pd.DefaultPageSettings.Margins.Bottom = 0;
pd.DefaultPageSettings.PaperSize = ps;

ZEBRA label printing

I need to print labels on my ZEBRA S4M printer.
PrintDocument pd = new PrintDocument { PrinterSettings = new PrinterSettings { PrinterName = "ZDesigner S4M-203dpi ZPL" } };
pd.DefaultPageSettings.Landscape = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
And when using Portrait landscape - it's alright, but in Landscape mode I have a big space from the left side of label.. How can I print a horizontal text on my labels?
Thanks a lot.
ADDED
I think that this problem is possible because of small PrintingArea property that I can't change. It's size 300x200 and real paper size is about 200x500...
I believe you have two options, either try changing your default paper size, or performing a translate.

Categories

Resources