Printing PDF using GhostScript.NET DPI printing issue - c#

I am using GhostScript.NET to print a PDF.
When I print it at 96DPI, the PDF prints fine, but is a little blurry.
If I try to print the document at 600DPI, the page that prints extremely magnified.
using GhostScript.NET.Rasterizer;
using System.Drawing.Printing;
PrintDocument doc = new PrintDocument();
doc.PrinterSettings.PrinterName="<printer name>";
doc.PrinterSettings.Copies=(short)1;
GhostScriptRasterizer rasterizer = new GhostScriptRasterizer();
rasterizer.Open("abc.pdf");
//Image page = rasterizer.GetPage(96,96); <-- this one prints ok
Image page = rasterizer.GetPage(600,600);
doc.Graphics.DrawImage(page, new Point());
One thing that I noticed when looking at the page object was that although I passed GetPage() 600, 600 - the image returned had a HorizontalResolution of 96 and a VerticalResolution of 96.
So I tried the following:
Bitmap b = new Bitmap(page.Width,page.Height);
b.SetResolution(600,600);
Graphics g = Graphics.FromImage(b);
g.DrawImage(page,0,0);
page = b;
This has a HorizontalResolution of 600 and VerticalResolution of 600, but this printed the image even larger!
Can anyone give advice here?

hi i got the same Problem.
The Rasterizer only zoom to the dpi...
you have to use the GhostScriptProcessor.
private List<string> GetImageWithGhostGhostscriptProcessor(string psFilename, string outputPath, int dip = 300)
{
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);
GhostscriptVersionInfo gv = GhostscriptVersionInfo.GetLastInstalledVersion(
GhostscriptLicense.GPL | GhostscriptLicense.AFPL,
GhostscriptLicense.GPL);
using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
{
processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing);
List<string> switches = new List<string>();
switches.Add("-empty");
switches.Add("-dSAFER");
switches.Add("-dBATCH");
switches.Add("-dNOPAUSE");
switches.Add("-dNOPROMPT");
switches.Add(#"-sFONTPATH=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts));
switches.Add("-dFirstPage=" + 1);
switches.Add("-dLastPage=" + 1);
//switches.Add("-sDEVICE=png16m");
switches.Add("-sDEVICE=tiff24nc");
//switches.Add("-sDEVICE=pdfwrite");
switches.Add("-r" + dip);
switches.Add("-dTextAlphaBits=4");
switches.Add("-dGraphicsAlphaBits=4");
switches.Add(#"-sOutputFile=" + outputPath + "\\page-%03d.tif");
//switches.Add(#"-sOutputFile=" + outputPath + "page-%03d.png");
switches.Add(#"-f");
switches.Add(psFilename);
processor.StartProcessing(switches.ToArray(), null);
}
return Directory.EnumerateFiles(outputPath).ToList();
}

I think that the parameters that you are passing into the DrawImage method should be the size you want the image on the paper rather than leaving them by default, the DrawImage command will then take care of the scaling for you. Probably the easiest way is to use the following override of the DrawImage command.
Note: This will skew the image if the proportions of the image are not the same as the rectangle. Some simple math on the size of the image and paper size will allow you to create a new rectangle that fits in the bounds of the paper without skewing the image.
This is what works for me:
int desired_x_dpi = 600;
int desired_y_dpi = 600;
Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
pd.PrintPage += (sender, args) =>
{
args.Graphics.DrawImage(img, args.MarginBounds);
};
pd.Print();

Related

Printing a Windows Form fit to an A4 Paper [duplicate]

In C#, I am trying to print an image using PrintDocument class with the below code. The image is of size 1200 px width and 1800 px height. I am trying to print this image in a 4*6 paper using a small zeebra printer. But the program is printing only 4*6 are of the big image. that means it is not adjusting the image to the paper size !
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile("C://tesimage.PNG");
Point p = new Point(100, 100);
args.Graphics.DrawImage(i, 10, 10, i.Width, i.Height);
};
pd.Print();
When i print the same image using Window Print (right click and select print, it is scaling automatically to paper size and printing correctly. that means everything came in 4*6 paper.) How do i do the same in my C# program ?
The parameters that you are passing into the DrawImage method should be the size you want the image on the paper rather than the size of the image itself, the DrawImage command will then take care of the scaling for you. Probably the easiest way is to use the following override of the DrawImage command.
args.Graphics.DrawImage(i, args.MarginBounds);
Note: This will skew the image if the proportions of the image are not the same as the rectangle. Some simple math on the size of the image and paper size will allow you to create a new rectangle that fits in the bounds of the paper without skewing the image.
Not to trample on BBoy's already decent answer, but I've done the code that maintains aspect ratio. I took his suggestion, so he should get partial credit here!
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PrinterSettings.PrinterName = "Printer Name";
pd.DefaultPageSettings.Landscape = true; //or false!
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile(#"C:\...\...\image.jpg");
Rectangle m = args.MarginBounds;
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
args.Graphics.DrawImage(i, m);
};
pd.Print();
The solution provided by BBoy works fine. But in my case I had to use
e.Graphics.DrawImage(memoryImage, e.PageBounds);
This will print only the form. When I use MarginBounds it prints the entire screen even if the form is smaller than the monitor screen. PageBounds solved that issue. Thanks to BBoy!
You can use my code here
//Print Button Event Handeler
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += PrintPage;
//here to select the printer attached to user PC
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = pd;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
pd.Print();//this will trigger the Print Event handeler PrintPage
}
}
//The Print Event handeler
private void PrintPage(object o, PrintPageEventArgs e)
{
try
{
if (File.Exists(this.ImagePath))
{
//Load the image from the file
System.Drawing.Image img = System.Drawing.Image.FromFile(#"C:\myimage.jpg");
//Adjust the size of the image to the page to print the full image without loosing any part of it
Rectangle m = e.MarginBounds;
if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
}
e.Graphics.DrawImage(img, m);
}
}
catch (Exception)
{
}
}
Answer:
public void Print(string FileName)
{
StringBuilder logMessage = new StringBuilder();
logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "-------------------[ START - {0} - {1} -------------------]", MethodBase.GetCurrentMethod(), DateTime.Now.ToShortDateString()));
logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "Parameter: 1: [Name - {0}, Value - {1}", "None]", Convert.ToString("")));
try
{
if (string.IsNullOrWhiteSpace(FileName)) return; // Prevents execution of below statements if filename is not selected.
PrintDocument pd = new PrintDocument();
//Disable the printing document pop-up dialog shown during printing.
PrintController printController = new StandardPrintController();
pd.PrintController = printController;
//For testing only: Hardcoded set paper size to particular paper.
//pd.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Custom 6x4", 720, 478);
//pd.DefaultPageSettings.PaperSize = new PaperSize("Custom 6x4", 720, 478);
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrintPage += (sndr, args) =>
{
System.Drawing.Image i = System.Drawing.Image.FromFile(FileName);
//Adjust the size of the image to the page to print the full image without loosing any part of the image.
System.Drawing.Rectangle m = args.MarginBounds;
//Logic below maintains Aspect Ratio.
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
//Calculating optimal orientation.
pd.DefaultPageSettings.Landscape = m.Width > m.Height;
//Putting image in center of page.
m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2);
m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2);
args.Graphics.DrawImage(i, m);
};
pd.Print();
}
catch (Exception ex)
{
log.ErrorFormat("Error : {0}\n By : {1}-{2}", ex.ToString(), this.GetType(), MethodBase.GetCurrentMethod().Name);
}
finally
{
logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "-------------------[ END - {0} - {1} -------------------]", MethodBase.GetCurrentMethod().Name, DateTime.Now.ToShortDateString()));
log.Info(logMessage.ToString());
}
}
Agree with TonyM and BBoy - this is the correct answer for original 4*6 printing of label. (args.PageBounds). This worked for me for printing Endicia API service shipping Labels.
private void SubmitResponseToPrinter(ILabelRequestResponse response)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile(response.Labels[0].FullPathFileName.Trim());
args.Graphics.DrawImage(i, args.PageBounds);
};
pd.Print();
}
all these answers has the problem, that's always stretching the image to pagesize and cuts off some content at trying this.
Found a little bit easier way.
My own solution only stretch(is this the right word?) if the image is to large, can use multiply copies and pageorientations.
PrintDialog dlg = new PrintDialog();
if (dlg.ShowDialog() == true)
{
BitmapImage bmi = new BitmapImage(new Uri(strPath));
Image img = new Image();
img.Source = bmi;
if (bmi.PixelWidth < dlg.PrintableAreaWidth ||
bmi.PixelHeight < dlg.PrintableAreaHeight)
{
img.Stretch = Stretch.None;
img.Width = bmi.PixelWidth;
img.Height = bmi.PixelHeight;
}
if (dlg.PrintTicket.PageBorderless == PageBorderless.Borderless)
{
img.Margin = new Thickness(0);
}
else
{
img.Margin = new Thickness(48);
}
img.VerticalAlignment = VerticalAlignment.Top;
img.HorizontalAlignment = HorizontalAlignment.Left;
for (int i = 0; i < dlg.PrintTicket.CopyCount; i++)
{
dlg.PrintVisual(img, "Print a Image");
}
}

c# - Print the winform in high quality?

This question may sound familiar but i have searched internet and couldn't find a solution to it.
I know for printing we can use Crystal Report but i am discarding that idea because of it's certain disadvantages. Here are some of the disadvantages:-
Needs installation on PC and if the Visual Studio version is 2017 then you have to download 200MB+ setup.
If you have made certain objects like textobject and lines in one section and you have to add something in the middle of it then you have to manually set location of every object otherwise if you collectively select all the object and move it then their original location and the spacing between each object gets lost.
Currently i am using VS2008 and it has crystal report integrated in it which has a well know problem of sometime changing the text of every textobject and adding an "i" in it for some reason.
I also tried to download an alternate to crystal report. But its interface is not that friendly.
Alternate that i am choosing
Now i have designed my report on Windows Form. When i am trying to print, its quality is worse as compared to Crystal Reports print. Here is the code for it
private System.IO.Stream streamToPrint;
string streamType;
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);
}
private void btnPrint_Click(object sender, EventArgs e)
{
string fileName = Application.StartupPath + "\\PrintPage.jpg";
using (Graphics gfx = this.CreateGraphics())
{
using (Bitmap bmp = new Bitmap(this.Width, this.Height, gfx))
{
this.DrawToBitmap(bmp, new Rectangle(0, 0, this.Width, this.Height));
bmp.Save(fileName);
}
}
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StartPrint(fileStream, "Image");
fileStream.Close();
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
}
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();
//docToPrint.Print();
}
}
private void Frm_Test_Shown(object sender, EventArgs e)
{
try
{
btnPrint_Click(sender, e);
}
catch (Exception ex) { clsUtility.ShowErrMsg(ex.Message); }
}
I understand the reason why it is doing so because of the image and screen resolution thing (Ref : https://stackoverflow.com/a/12547806/2994869). The workaround people mentioned was to increase the size of windows form and it's object by 6 times but that had the same result still the quality is worse.
Is there any workaround or any trick that i can do to print a windows form so that quality is near to that of Crystal Report's.
For high quality you need to create a source with higher resolution than the screen, which is all you can get from DrawToBitmap.
Using DrawToBitmap will only help if you can enlarge a single control a lot. For the whole form it will not help.
You will need to fully re-create the parts you want to print and use as many DrawString and other DrawXXX methods as needed. Yes, a lot of work. (But fun ;-)
But the code you show also blunders by using a jpg file format, which was created for soft images (i.e. photographs). Change that to png and compare!

Printing more than one page for control

This code will only print one page but the height of the control exceeds the page height and therefore will need to be printed on a second page. I have been doing some investigation as to what will enable the control to proceed onto another page. I got as far as DocumentPaginator and fiddled with the size making it bigger and smaller than the sz variable but no difference. Any Ideas as to what controls a creation of a new page? does the size relate to pagination?
private void Print()
{
var pd = new PrintDialog();
var document = new FixedDocument();
var fixedPage = new FixedPage();
var pageContent = new PageContent();
System.Printing.PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);
System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
MarSheetReport mar = new MarSheetReport();
document.DocumentPaginator.PageSize = sz;
Transform originalScale = fixedPage.LayoutTransform;
//get selected printer capabilities
fixedPage.LayoutTransform = new ScaleTransform(0.2823293807641634 + 0.2498215560314061, 0.2823293807641634 + 0.2498215560314061);
fixedPage.Width = sz.Width;
fixedPage.Height = sz.Height;
// Add visual, measure/arrange page.
fixedPage.Children.Add(mar.o);
fixedPage.Measure(sz);
fixedPage.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
fixedPage.UpdateLayout();
//fixedPage.LayoutTransform = originalScale;
((IAddChild)pageContent).AddChild(fixedPage);
document.Pages.Add(pageContent);
pd.PrintDocument(document.DocumentPaginator, "My Document");
}
Here is one way of doing it i.e creating the bitmap from the visual and breaking it on multiple pages.
http://www.codeproject.com/Articles/339416/Printing-large-WPF-UserControls

Select an image from rectangle using writeablebitmap

I have written an application in silverlight, I am placing a rectangle on the image and want to select the part of image covered by rectangle and show it on a image control on click of a button.
I am not good at handling ratios and image manipulation things, so I am unable to get it right way.
The code for the same goes as below, and would appreciate, if anyone could suggest me a way or solution to get around with this.
public void CaptureImage(object sender, RoutedEventArgs e)
{
BitmapImage bitmapImage = new BitmapImage();
//// bitmapImage.CreateOptions = BitmapCreateOptions.None;
bitmapImage = NewImage;
////calculate bounding box
int originalWidth = bitmapImage.PixelWidth;
int originalHeight = bitmapImage.PixelHeight;
int newSmallWidth = (int)SquareBlue.Width;
int newSmallHeight = (int)SquareBlue.Height;
////generate temporary control to render image
Image temporaryImage = new Image { Source = bitmapImage, Width = newSmallWidth, Height = newSmallHeight };
////create writeablebitmap
WriteableBitmap wb = new WriteableBitmap(newSmallWidth, newSmallHeight);
TranslateTransform t = new TranslateTransform();
t.X = -5;
t.Y = -5;
wb.Render(temporaryImage, t);
wb.Invalidate();
myImage.Source = wb;
}
Whenever this code gets executed, whole image gets snapped, instead of the part selected by rectangle. Could anyone, guide me as what I am doing wrong here.
I'd recommend that you use the Crop method the WriteableBitmapEx library provides.

AForge.NET -> AVIWriter Adding Images as Frames

I want to make a video from images, and each image should stay for one second.
The AVIWriter has 25 frames rate, so i have to add one image 25 times to make it stay for one second.
I tried changing the frame-rate, but it is not working.
Can anyone suggest a workaround?
The following is the code for writing frames into video:
private void writeVideo()
{
// instantiate AVI writer, use WMV3 codec
AVIWriter writer = new AVIWriter("wmv3");
// create new AVI file and open it
writer.Open(fileName, 320, 240);
// create frame image
Bitmap image = new Bitmap(320, 240);
var cubit = new AForge.Imaging.Filters.ResizeBilinear(320, 240);
string[] files = Directory.GetFiles(imagesFolder);
writer.FrameRate = 25;
int index = 0;
int failed = 0;
foreach (var item in files)
{
index++;
try
{
image = Image.FromFile(item) as Bitmap;
//image = cubit.Apply(image);
for (int i = 0; i < 25; i++)
{
writer.AddFrame(image);
}
}
catch
{
failed++;
}
this.Text = index + " of " + files.Length + ". Failed: " + failed;
}
writer.Close();
}
Hello I had the same problem and saw this topic read it all and no comment for
http://www.aforgenet.com/framework/docs/html/bc8345f8-8e09-c1a4-4834-8330e5e85605.htm
There is a note like that "The property should be set befor opening new file to take effect."
The solution of Hakan is working, if you use this code:
AVIWriter videoWriter;
videoWriter = new AVIWriter("wmv3");
videoWriter.FrameRate = 1;
videoWriter.Open("test.avi", 320, 240);
videoWriter.AddFrame(bitmap1);
videoWriter.AddFrame(bitmap2);
videoWriter.AddFrame(bitmap3);
videoWriter.Close();
There is well one bitmap displayed per second.
(just for giving a directly working piece of code).
The default frame rate for AVIWriter is 25. As you have no option to specify dropped or otherwise skipped frames, why wouldn't you set AVIWriter::FrameRate property to 1 before you start populating your writer with frames?

Categories