Winforms: Capture part of PDF image to OCR - c#

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
}

Related

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.

overlay images on top of an existing image (web)

My goal is to display a large rectangular image in a section of a webpage that will act as a background for other, smaller images to be laid on top of. The smaller rectangular images will be dynamically selected based upon database entries. I was able to create a java applet that drew the larger base rectangular image and then drew the smaller images over the base image. This worked very well.
I am attempting to recreate the functionality using C# in Microsoft Visual Web Developer 2010. I have found system.drawing functionally that may work, but haven’t found a web based solution yet. Any help would be appreciated.
If I understand correctly, you want to overlay smaller images on top of another image. At the end you'll end up with one image to display. This is easy to do in C#:
string image1 = #"c:\image.jpg";
string image2 = #"c:\image2.jpg";
System.Drawing.Image canvas = Bitmap.FromFile( image1 );
Graphics gra = Graphics.FromImage( canvas );
Bitmap smallImg = new Bitmap(image2);
gra.DrawImage( smallImg, new Point( 70, 70 ) );
canvas.Save( #"c:\newimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg );
My two cents here... Another thing which helped me on .NET 2.0 and 3.0 was explicitly deleting the Image, Graphics and Bitmap objects after you are done, specially when you would be accessing any of the image sources (image1, image2 and smallImg above) within the same routine.
Deleting these objects will instantly release the file locks. I experienced that the garbage collector wouldn't necessarily clean these up for me at the desired time, even if I made a separate sub-routine for my image manipulation.

How to grab an image and save it in a folder [c# windows application]

I am Creating an windows application using c#.I have a button which should capture the image(the entire Desktop screen) and Save it in a folder .Also i need to show the preview of the image .
Graphics.CopyFromScreen Method
sample code:
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
g.Save();
bmp.Save("D:\\file.jpg", ImageFormat.Bmp);
as for show the preview. IMO not that hard to write it on ur own.
There are different ways to perform what you bring here. Using the Screen class, there are a few simple samples I found on the Internet. Others are using Direct3D.
TeboScreen: Basic C# Screen Capture Application;
Capture a Screen Shot;
C# – Screen capture with Direct3D;
Capture DeskTop Screen;
Enhanced Desktop Recorder in .NET using C# and Windows Forms; (perhaps not suited for your question, but might get interesting if you plan further features.)
Capturing the Screen Image Using C#.
In short, the idea consists of getting the image of the desktop using the Screen class or your favorite way, store it into a Bitmap object and save this bitmap into a file.
As for displaying a preview, once your Bitmap instance is created, you simply need a PictureBox and set its Image property and show your form to the user so he may see the image.
Hope this helps! =)
You will need to do some importing of Interop dlls.
Take a look at the following example shows very well how to capture the screen shot and save to disk.
public void CaptureScreen(string fileName,ImageFormat imageFormat)
{
int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()),
hdcDest = GDI32.CreateCompatibleDC(hdcSrc),
hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,
GDI32.GetDeviceCaps(hdcSrc,8),GDI32.GetDeviceCaps(hdcSrc,10)); GDI32.SelectObject(hdcDest,hBitmap);
GDI32.BitBlt(hdcDest,0,0,GDI32.GetDeviceCaps(hdcSrc,8),
GDI32.GetDeviceCaps(hdcSrc,10),hdcSrc,0,0,0x00CC0020);
SaveImageAs(hBitmap,fileName,imageFormat);
Cleanup(hBitmap,hdcSrc,hdcDest);
}
The above example taken from the website. All code by Perry Lee

Draw a bitmap from a control taller than the screen

I am having quite a few problems saving a C# control to a bitmap file. The control in question is a kind of drawing surface on which the user can write text, draw pictures and paint boxes. The control is resizable and draggable. What happen is, when the control is really big and is not totally visible on the screen, the parts of the control not visible are saved half-drawn on the bitmap. The code used to generate the bitmap is quite simple:
Bitmap bitmap = new Bitmap(myControl.Width, myControl.Height);
myControl.DrawToBitmap(bitmap);
I have tried the following methods to try to have a fully painted bitmap, without any success:
myControl.Invalidate(myControl.ClientRectangle, true);
myControl.Refresh();
myControl.Update();
Application.DoEvents();
I cannot scale the control down to make it fully visible since resolution and image quality are very important for that project. In fact, I am actually trying to scale the image up to increase it's quality. Are there ways I am not aware of generating an image from a control ?
Tank you.
DrawToBitmap has limitations and dont always work as expected. Try instead work with native GDI+
Here is example
Maybe my answer here Capturing a Window that is hidden or minimized can help you?

Categories

Resources