saving e.graphics into a pdf problems - c#

i did some e.graphics.draw string/image/line, and i managed to print it using print dialog print document and print previw dialog. but i wanted to save it into a pdf using microsoft print to pdf. into a specific folder. without using the file dialog, just one button to save it as pdf.
i dont have an idea what is the next step. so any help is appreciated, btw i searched for more than 10 days, but i couldn't find a way to do this. btw i used itextsharp,and pdf printer. but i couldn't do what i want because i want to save what i did using e.graphics,thanks in advance.

In order to use the Graphics class for drawing, you have to be able to create an instance for the canvas you want to draw on. Can you do that for a PDF using iTextSharp? I don't know as I've never used it but I don't think I've ever seen it done. I think iTextSharp has its own methods for inserting text, images, etc.
The first thing you need to do is determine whether iTextSharp does/can expose a Graphics instance. If it does not then you can either give up this idea and use the functionality that iTextSharp does provide, or you can use a Graphics object to draw on an Image and then insert that into your PDF using whatever functionality iTextSharp provides for that.
Assuming you're going ahead, the first thing you should do is write a method that takes a Graphics object as an argument and uses it to draw, e.g.
private void Draw(Graphics g)
{
// Call g.DrawString, etc, here.
}
You can then call that method anywhere you have a Graphics object to perform that same drawing. If you are currently using e.Graphics to draw in the PrintPage event handler of a PrintDocument, you would move that code to the method above and change e.Graphics to g, then call the method and pass e.Graphics instead. You can then call the same method elsewhere with different Graphics objects, e.g. create a Bitmap of the desired dimensions, call Graphics.FromImage and then pass the result to that method to do the same drawing on that Bitmap.

Related

Winforms: Capture part of PDF image to OCR

This is a C# winforms question.
The process I am trying to achieve is the following:
Using AxAcroPDFLib I'm loading a pdf file to the form
I want the user to be able to specify a square on that PDF and create a bmp from it
That bmp will then be loaded to a OCR to become text
What is my issue:
Step 1 and 3 are easy to do, the problem is how to allow the user to draw a square on top of the AxAcroPDFLib for a screenshot.
I already got different ways to draw squares on native winform components, but AxAcroPDFLib does not support mouse down, up, move, etc and paint events.
There is the option to convert the PDF to bitmap and display it on a picturebox and deal with events for drawing the square. Problem with that approach is that my PDF's are usually more than two pages, and I would like to avoid the conversion pdf to bmp due to changes to image quality that will impact on OCR.
I came to think that maybe something that works as the windows snippingtool would do the the job. My application would get the screenshot, temporarily save the image on disk (must be a file for OCR), I would then pass it to the OCR and done. Hard part, I could not think on how to take the screenshot of part of the PDF.
Do anyone here have any suggestion to different components or workarounds to deal with the requirement above? I am using Adobe just because it is simple, but maybe there are other components better suited to handle my requirements? I googled but haven't found any free ones, trying to avoid paid options.
Thanks
At some point in this process, the PDF is going to have to be rasterized in order to be passed to the OCR, so I don't totally understand your objection to converting it to a bitmap. If you're okay with Snipping Tool's behavior, then you must be OK with the quality of the PDF control's PDF->screen rasterization. If that resolution is acceptable, why not just capture the control's content to a Bitmap and let the user draw the selection marquee over that Bitmap?
Here is code I'm using to capture a control's contents to a Bitmap in Windows Forms. One caveat is that this is really a screen capture, so any windows or controls that overlap the control's visible area will be captured in the image.
using (Bitmap b = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(b))
{
Point p1 = myControl.PointToScreen(new Point(0, 0));
g.CopyFromScreen(p1.X, p1.Y, 0, 0, size);
}
// do stuff here with your Bitmap
}

Improving clarity of images in PictureBox C#

I have Panel on windows form containing labels for displaying information and 2 PictureBox controls. One for Company logo & other for captured photo. When i print them either on Printer or just save in PDF format, both images looking very blur. I want to improve quality of images. So they will appear clear even after zoom.
Any suggestion.
You can set this property of the Graphics object.
// g is type of Graphics.
g.InterpolationMode = InterpolationMode.High;
I don't know which technique you are using for printing but if you are using print form it is not good for decent picture or text. Therefore when you send form/PictureBox to the print object it would lose quality.The Solution is to use PrintDocument.
Share your code for more help.
Your printer has better resolution than your monitor. That bitmap won't work. You will have to recreate the print items directly on the e.Graphics of the PrintPageEventArgs.
Other work around is the technique that guy explain in his article.
http://www.dahuatu.com/3vy2K5z5mr.html

How to WIC Encode a D2D bitmap to png

At the moment I'm programming a paint app in WPF C# and have a problem. I want to draw different shapes and save them as a png/bmp. So right now I'm decoding a bmp to a direct2d bitmap to draw on rendertarget. But my problem is that I don`t know how to save the D2DBitmap as a png because I can not find a encode function...
I am not allowed to use SharpDX so I hope someone can help me with this problem.
Or maybe somebody has another solution for drawing shapes and stuff but NOT in the xml file.
So I managed it by myself.
I created a surfaceRenderTarget that can be painted on and also is shown on Screen.
Then I created a WicBitmapRenderTarget with the same properties and this Rendertarget is easily saveable with a saveToFile method.
So I draw on the surfaceRenderTarget and push every operation to an operationstack and when I click the save button the programm draws the hole stack on the bitMapTarget and saves this.
This is my solution, maybe someone has a better.

How to manually get instance of Graphics object in WinForms?

I know how to work with object of type Graphics (at least I am able to render images) but I always do that by passing graphics object retrieved from OnPaint method.
I would like to display an image when the app is opened (ie in Form_Load method) but have no clue how to obtain the instance of Graphics object I could use?
Thanks
Using the e.Graphics object that OnPaint() supplies to you is the correct way of doing it. It will run right after the OnLoad() method. The form isn't visible yet in OnLoad.
Getting a Graphics object from Control.CreateGraphics() is supported. However, whatever you draw with this will be wiped out as soon as the form repaints itself. Which happens when the user moves another window across yours (pre-Aero) or when she minimizes and restores or otherwise resizes the window. Use CreateGraphics only ever when animating at a high rate.
If you're attempting to create a graphics object from the surface of your form, you can use this.CreateGraphics
If you are attempting to create a new Image, you can always initialize an Image and then call Graphics.CreateGraphics.FromImage(YourImage) e.g.
Bitmap b = new Bitmap(100,100);
var g = Graphics.CreateGraphics.FromImage(b);
At this point, any drawing performed to your Graphics object will be drawn onto your image.
None of the preceding answers worked for me. I found Rajnikant Rajwadi solution effective (see https://social.msdn.microsoft.com/Forums/vstudio/en-US/ce90eb80-3faf-4266-b6e3-0082191793f7/creation-of-graphics-object-in-wpf-user-control?forum=wpf)
Here is a horribly condensed call to Graphics.MeasureString(). (please code more responsibly)
SizeF sf = System.Drawing.Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle).MeasureString("w", new Font(TheControl.FontFamily.ToString(), (float)TheControl.FontSize));
And how do you plan to use the Graphics object you got in the Load event?
If you want to paint something on the screen, you have to be in the Paint event, or it will be cleared on the next paint.
What you can do: load another (simple) form, with just a picture, and hide it when your main form is loaded.
Since your Load event will probably run on the UI thread. Call DoEvents to make the other form appear.
form.CreateGraphics();
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.creategraphics.aspx
http://msdn.microsoft.com/en-us/library/5y289054.aspx

Printing in C#/.NET, scaling on a whole page

I am wondering how to do the following ( couldn't get google to help me here ):
I got a .jpg file of about 250px*250px in scale..
I want this image on a DIN-A4 page as often as possible
I need an "overlay" for every image on the page with a unique code ( lets say a unique barcode for example )
How would i start here? I have really no idea which classes or methods to use for this...
Also it'd be interesting to know which "formats" i can create the documents in...
Thanks for any help!
Well, you know how big the image is, and how big the paper is, so it's easy to work out how many you can fit on a page.
Then you'll want to create an instance if the Bitmap class from your jpg, then get a Graphics object from the bitmap, call the Graphics object's DrawString method with your text (plus a font, a brush, and a point). Remember to release the Graphics object or, ideally, wrap it in a using statement.Then, as Henk says, use a PrintDocument to print it - there's an example here of that - and write your images in as required to fill the page using the PrintPageEventArgs' Graphics object.

Categories

Resources