Windows API Code Pack - ShellFile not generating PDF bitmap - c#

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

Related

Convert Image file to PDF document letter size using PDFTron

I am trying to convert an image file to a PDF document with a defined page size (letter size).
Currenlty I am able to convert an image to a PDF document without defining any page dimensions (default dimensions of the PDF is the image size). I would like to define the page dimensions on the creation of the document, and place the image on that page (possibly with margins).
The following code snippet shows how I am currently converting an image file to a PDF document without setting any dimensions for the page:
async static Task<bool> ConvertImageToPDF(TestFile file)
{
pdftron.PDF.PDFDoc pdfdoc = new PDFDoc(); //Initialize a new PDF document
pdftron.PDF.Convert.ToPdf(pdfdoc, file.InputFile); //Use the Convert.ToPdf to generate the file from the image file
await pdfdoc.SaveAsync(file.OutputFile, SDFDocSaveOptions.e_linearized); //Save the PDF document generated from the conversion
pdfdoc.Destroy();
pdfdoc = null;
}
Any help or direction for assigning dimensions (letter size) to a PDF page and inserting the image file in that page would be more than welcome.
If ToPDF is given an image then PDFNet will query the DPI information of the image metadata and make page dimensions to match the DPI and resolution of the source.
If you like, you can always post-process the PDF generated by ToPDF.
Or, you can follow the AddImage sample code to do everything yourself.
https://www.pdftron.com/pdfnet/samplecode.html#AddImage

Take screenshot of selected area in pdf in windows application using 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.

.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);
}

Put .Gif Image from Clipboard to PictureBox

I want to copy gif image from Browser and paste to PictureBox
Image cImage = Clipboard.GetImage();
pictureBox1.Image = (Image)cImage;
This put image, but its not animated.
The image on the clipboard isn't a GIF, it's a Bitmap or DIB (or both). Those are the formats that you would see, if you used the old XP Clipboard Viewer (clipbrd.exe), or if you enumerated the available clipboard formats within your program.
See the list of standard clipboard formats: http://msdn.microsoft.com/en-us/library/windows/desktop/ff729168(v=vs.85).aspx
GIF is not one of them.
Also, there is no JPG or PNG. When you copy those images, they're placed onto the clipboard as CF_BITMAP and/or CF_DIB. Basically, Bitmap is the universal image format that everything converts to/from.
As an alternative, you MAY be able to get CF_HTML from the clipboard, figure out where the image is, and then fetch it from the server (or maybe the browser cache), preserving the original GIF information.

Bad Quality with Icon.ExtractAssociatedIcon function

I try to fetch the icon from an exe, where I have used the below snippet
C#
FileStream fs = new FileStream(icoFileNam, FileMode.OpenOrCreate);
Icon ico = Icon.ExtractAssociatedIcon(exefilePath);
ico.Save(fs);
The image saved lacks quality. I have saved the image as .ico file.
Can anyone knows how to retain the original quality of the icon present in the exefile?
In the most common cases you can use the extracted icon bitmap data via the Icon.ToBitmap() method. You can save this image to different formats. However it is pretty hard to save the icon as "true" .ico file.The problem is that there are no embedded encoders for icon images in .Net. So by default the result have been saved as low-color image. If this is unacceptable, the MS is recommended to save raw bitmap data as .ico manually. I suggest you use the IconLib library that already implement this task:
Icon icon = Icon.ExtractAssociatedIcon(#"C:\Windows\System32\notepad.exe");
MultiIcon mIcon = new MultiIcon();
SingleIcon sIcon = mIcon.Add("notepad");
sIcon.CreateFrom(icon.ToBitmap(), IconOutputFormat.Vista);
sIcon.Save(#"c:\notepad.ico");

Categories