Take screenshot of selected area in pdf in windows application using c# - c#

I am developing a windows application in .Net, in which I want to select a pdf from my computer and display it in the form. User can select some part of this pdf in the application and an image will be generated of the selected area.
I do not have any idea how to do this.
How do I read and display pdf and take screenshot of its content?
I have tried using the com component Acrobat Reader to read the pdf but it does not allow me to capture selected area using mouse.

The easiest way wouldbe to convert pdf into a bitmap (oseries of bitmaps if the pdf is multi-paged) then display it. And when user selects some area - just cut the bitmap and save into the file.
You can find many examples how to convert pdf to bitmap:
Convert PDF to JPG or PNG using C# or Command Line
Convert Pdf file pages to Images with itextsharp
and many more
EDIT:
This article seems to be very close to your problem:
http://www.codeproject.com/Articles/37637/View-PDF-files-in-C-using-the-Xpdf-and-muPDF-libra
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Portable Document Format (*.pdf)|*.pdf";
if (dlg.ShowDialog() == DialogResult.OK)
{
_pdfDoc = new PDFLibNet.PDFWrapper();
_pdfDoc.LoadPDF(dlg.FileName);
_pdfDoc.CurrentPage = 1;
PictureBox pic =new PictureBox();
pic.Width=800;
pic.Height=1024;
_pdfDoc.FitToWidth(pic.Handle);
pic.Height = _pdfDoc.PageHeight;
_pdfDoc.RenderPage(pic.Handle);
Bitmap _backbuffer = new Bitmap(_pdfDoc.PageWidth, _pdfDoc.PageHeight);
using (Graphics g = Graphics.FromImage(_backbuffer))
{
_pdfDoc.RenderHDC(g.GetHdc);
g.ReleaseHdc();
}
pic.Image = _backbuffer;
}
Having the bitmap painted you can then draw on it (ie. for select range), cut and save to file as you wish.

Related

How to read text image from pdf using pdfbox

I have one PDF document with image. That image contains text.
Now I want to read the text from the image using pdfbox.
I have tried the PDTTextStripper but it not working for image text.
Can you please give some idea about it.
PDDocument pDDocument = PDDocument.load(new java.io.File(fileName));
PDFTextStripper textStripper = new PDFTextStripper();
string text = textStripper.getText(pDDocument);
Console.WriteLine(text);
I want to read the text inside the image from the pdf using pdfbox c# .net.

Windows form distance measuring program acts up after multiple images opened up

I have a program which allows the user to open an image and measure it by dragging the mouse over it. The image opens up in a picture box when you press a button. The program works after two images are opened, but not after three images are opened. After the third image, the measurements are grossly overestimated.
Here is the part of my code which opens the image:
private void openPlan_Click(object sender, EventArgs e)// open plan folder
{
pictureBox1.Image = null;
// open file dialog
OpenFileDialog open = new OpenFileDialog();
open.InitialDirectory = #"C:\Users\Admin\Documents\complete Lumber Estimation Tool\Plans\";
// image filters
if (open.ShowDialog() == DialogResult.OK)
{
trackBar1.Value = 4;
zoom = 1.0F;
imgOriginal = null;
// display image in picture box
imgOriginal = new Bitmap(open.FileName);
pictureBox1.Image = imgOriginal;
}
}
Is this mainly a problem with how a windows form program stores data?
If so, how do I make sure the program isn't using any data or parameters from the previous image?
As in my comment:
Always dispose images, specially with open file handles.
Use:
imgOriginal?.Dispose();
pictureBox1.Image?.Dispose();
instead of: .Image = null
...in a greater detail:
The Image class is using GDI, which causes to open handles. These handles are not closed automatically by just loosing the reference in your application. They are not collected by the garbrage collector (learn.microsoft.com Garbrage-Collection).
This can result in unpredictable behavior of your application or beyond.
e.g: If you load an image from a file, the open handle results in a filelock(!) also if you just wand to display the image.
For that cases, you can load the whole image into a intermediate System.IO.MemoryStream object.
Encapsulate the image from the original source like this:
var stream = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(filepath));
var Image = Image.FromStream(stream);
This will load the file into the memory and the image out of it.
Take care to Dispose the stream and the Image after your use or when loading a different image.

How to draw vector graphics and save it in PDF file?

I'm working on a project which generally is collecting data and drawing results in charts. (Using C#) I need to save my charts in a PDF file. My question is, how to save charts in a PDF file without loosing resolution? My point is how to draw vector graphics instead of raster graphics?
I tried iTextSharp to create PDF file but the result is not satisfying at all!
I'm new here, so I'm not able to upload pictures.
Here is the result after saving my file:
https://www.dropbox.com/s/ruwtc82hfosxk6y/Test.pdf?dl=0
Here is the PDF that I need to create:
https://www.dropbox.com/s/jvu5uu069imo9xc/nir%20well%20abfar.pdf?dl=0
There are two ways I know of to get high-quality images from your Chart into a PDF.
One is by using vector formats:
Use chart.SaveImage with one of the three emf formats.
Convert the resulting emf file to wmf
Insert the wmf file into your iTextSharp document.
1 and 3 are one-liners. But step 2 is not. In fact I haven't found a working c# solution at all. The best was a weird reference to an edit that has disappeared here ..
If you can use some other program to do the conversion you will get nice results like this demo pdf file.. I used Illustrator for the conversion.
Two: If you can't get step 2 to work, you can still get nice results, if you use raster images with a nice and high resolution. Here is how to do it:
First we hide the Chart, so we don't scare the user. Then we make it as large as we want the output to be. Then we DrawToBitmap and finally we reset the chart again..:
Size s = chart1.Size;
chart1.Hide();
// pick your size in pixels
// I simply multiply my screen size..:
chart1.Size = new System.Drawing.Size(s.Width * 5, s.Height * 5);
using (Bitmap bmp = new Bitmap(chart1.ClientSize.Width, chart1.ClientSize.Height))
{
// you should set the resolution,
// although I didn't find a way for iTextSharp to use it visually
bmp.SetResolution(600, 600);
using (Graphics G = Graphics.FromImage(bmp))
{
G.SmoothingMode = SmoothingMode.HighQuality;
G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
chart1.DrawToBitmap(bmp, chart1.ClientRectangle);
bmp.Save(yourImageFile, ImageFormat.Png);
}
}
chart1.Size = s;
chart1.Show();
You could also use SaveImage and save a few lines, but you can't set the resolution of the png file there and it will be saved at the currnt screen resolution, which is 96dpi here..
Now you have a large image and will probably have to scale it down in iTextSharp:
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(yourImageFile);
img .ScalePercent(15); // scale to fit your needs..
doc.Add(img );
Note that the legend and the labels get very small this way, so you may have to enlarge them before saving. I also found that, when scaling down, the image is rather bright until you zoom in..
Here are two screenshots, one from the chart, the other from the pdf documnet after zooming in a lot (300%)..:

.NET C# - Both Bitmap and Image Loads Images Smaller than Expected

I'm trying to process the typical 16 megapixel images from a modern camera. Picking a random image, both the file system and my image editing software says the image is 4608 x 3456. When I load the image in C# (VS2013 .Net 4.5) using either new Bitmap(filename) or Image.FromFile(filename), I get an image successfully loaded. However, the resulting image has a size of 1613 x 1210. Now, in some cases I want to create custom size thumbnails, and this will work ok. However, I have another need where I detect "non normal" orientations and I simply want to flip/rotate for display, then save.
Saving these images (without any adjustments, just load and save) creates a valid image on disk. However, both the file system AND my image tool says the size is 1613 x 1210.
How do I load the full size image and preserve all info back to disk? Any ideas as to what I'm doing wrong? I just want to rotate the image where needed, I don't want to shrink it!
Here's a snippet of what I tried, as promised in a comment below:
Bitmap bm = new Bitmap(fileName);
Image jpg = Image.FromFile(fileName);
jpg.Save("e:\\test.jpg");
bm.Save("e:\\test2.jpg");
Both files are smaller than their original size, and match the width and height the debugger shows for both in-memory images.
Per a suggested answer, I tried this code but saw no difference in the results:
long width = 0;
long height = 0;
byte[] imageBytes = File.ReadAllBytes(fileName);
using (MemoryStream ms = new MemoryStream(imageBytes))
{
Image jpg3 = Image.FromStream(ms);
width = jpg3.Width;
height = jpg3.Height;
jpg3.Save("e:\\test3.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
Thanks in advance.
[EDIT]
User Error from a followup post:
While it's true I do have a 4608x3456 size image of the very picture I
am trying to load, I selected the wrong directory in my OpenFileDialog
and was actually selected a, you guessed it, 1613 x 1210 version of
this image. The code WAS loading the full thing, the silly operator
(me) was gumming it up.
Before I post, I'll try the full-size image ... yeah, it works fine.
Can you show your program code?
I downloaded 5169x3423 size sample image.
Load this image using program and when i restored it, original and new image are same.
I load image file to byte array and save it to Bitmap.
private Image LoadImage()
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.ShowDialog();
byte[] byteImage = File.ReadAllBytes(openFile.FileName);
Image myImage;
using (var ms = new MemoryStream(byteImage))
{
myImage = Image.FromStream(ms);
}
return myImage;
}
private void SaveImage(Image myImage)
{
Bitmap bmp = new Bitmap(myImage);
// Temp save location
bmp.Save("c:\\test\\myImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

Windows API Code Pack - ShellFile not generating PDF bitmap

Using the code from previous stack overflow questions:
System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);
image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);
I am trying to use window API to get the thumbnail of a PDF
I am led to believe that this generates an image file that resembles the first page of the PDF document.
The reality however is that it does NOT look like that, and merely looks like the PDF icon.
Is there anything that I'm missing that is required before this actually works as intended?
PDF files are correctly associated with adobe reader.
When browsing directories in windows explorer I DO see thumbnails associated with the documents.
I should note that the code DOES in fact correctly extract thumbnails when dealing with Excel and Word documents.
EDIT (references):
C# get thumbnail from file via windows api
Get thumbnail of any file, not only image files on Windows XP/Vista
Windows API Code Pack Thumbnail gives preview thumb of pdf but not Word or Excel
You need to specify that you want the thumbnail, not the icon (the default).
Change your code into this:
System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);
//force the actual thumbnail, not the icon
f.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly;
image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);
The problem is because you have not selected the active frame that you will create the thumbnail from.
I cannot verify it on my current machine, because I don't have the Windows API on it, but it's giving you the standard PDF thumbnail because in your code you have't specified which page to use for the thumbnail.
Try doing something like this:
Image image = new Image();
//Load image here
int frameindex = 1; // Page number you want to use for thumbnail
Guid guide = image.FrameDimensionsList[0];
FrameDimension fDimension = new FrameDimension(guide);
image.SelectActiveFrame(fDimension, frameindex);
//Then go on to extract your thumbnail
I was not able to get ExtraLargeBitmap to work for PDF files but all other sizes (Large, Medium, and Small) worked fine.
Dim MyShellFile As ShellFile = ShellFile.FromFilePath(fi.FullName)
Dim MyThumbNail As Image
MyShellFile.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly
MyThumbNail = MyShellFile.Thumbnail.LargeBitmap
Me.PictureBox2.Image = MyThumbNail

Categories