Edit tif files with C# - c#

I need to create a program that reads tif files from a directory and then trims the bottom inch of the file and resaves the file. I know how to open the files but how would I automate this process from c#?

If you need to handle TIFF images in C# then have a look at LibTIFF.Net
http://bitmiracle.com/libtiff/ - It is open source and Native .NET component and free for commercial use.
This library should also have the TIFF cropping functions you need. I am not sure if the native .NET libraries can handle all of the TIFF functions you may require whereas LibTIFF will.
The original LibTIFF for C/C++ can be found at http://www.remotesensing.org/libtiff/ which may help you with documentation and support if needed.
Included with libTiff is a program called tiffCrop which should also have source code. http://www.remotesensing.org/libtiff/man/tiffcrop.1.html which can be accessed via
http://www.remotesensing.org/libtiff/tools.html.

See here.

Related

Extracting the first page of multiple PDFs & saving them as Image

I have about 400 ebooks, all in PDF format, and my task is to extract the cover from every one of them (which is the first page of every PDF) and export them all as separate image (PNG or JPEG) files
So I will end up with 400 ebooks and 400 images of their covers.
I have Windows
Any advice greatly appreciated.
Use ghostscript to render tiff or jpg from the pdf. You have fine grained control over the result.
If this is a commercial application, you need a commercial license. If you use the application commercially, but inside your organisation, you are allowed to use the GPLed version of ghostscript.
Ghostscript can be found here. The PDF interpreter in many opensource packages relies on the gs PDF interpreter. Imagemagick for example, requires ghostscript libraries.
Download GS here: http://ghostscript.com/download/gsdnld.html
Use C# Process class to execute Ghostscript, there is a SO topic on this here How to run a C# console application with the console hidden
The commandline for tiff will be:
D:\gs\gs9.20>bin\gswin64c.exe -sOutputFile=d:\some%02d.tiff -dBATCH -dNOPAUSE -sDEVICE=tiff24nc -sCompression=lzw -r150 -sPageList=1 d:\PDFReference.pdf
This will create one some01.tiff file on d:\ in 150dpi resolution.
The following thread is suitable for your request. converting pdf file to an jpeg image
One solution is to use a third party library. ImageMagick is a very popular, freely available too. You can get a .NET wrapper for it here. The original ImageMagick download page is here.
http://www.codeproject.com/KB/library/pdftoimages.aspx Convert PDF pages to image files using the Solid Framework
http://www.print-driver.com/howto/convert_pdf_to_jpeg.html Universal Document Converter
http://www.makeuseof.com/tag/6-ways-to-convert-a-pdf-file-to-a-jpg-image/ 6 Ways To Convert A PDF To A JPG Image
And you also can take a look at this thread: how to open a page from a pdf file in pictureBox in C#
If you use this process to convert a PDF to tiff, you can use this class to retrieve the bitmap from tiff.

Error with ImageMagick and Ghostscript converting from PDF

We are using Magick.Net version 7.0 with Ghostscript 9.16. We are reading in a PDF and converting this to a tif or a jpg image. Everything is working fine when we run these through one at a time and our PDF gets converted.
This is an application that will be hit by many systems, so we put a small load test to ensure we could handle multiple requests. Everything runs great as long as we use different PDF files. If we try and run the same PDF file through multiple times (doing 5 requests at the same time with the same PDF), we encounter and error. The error we receive is PDFDelegateFailed. We are not sure why this error occurs and if we try other formats (such as tif to jpg), there are no issues.
ImageMagick.MagickDelegateErrorException:
ESBService.exe:
PDFDelegateFailed [ghostscript library 9.16] -q -dQUIET -dSAFER
-dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r96x96" "-sOutputFile=C:/Users/esbsvc/AppData/Local/Temp/magick-4668LPfdzdzRfLYF%d"
"-fC:/Users/esbsvc/AppData/Local/Temp/magick-4668wanF98SE_8PK"
"-fC:/Users/esbsvc/AppData/Local/Temp/magick-4668L3mJE6M2iUZV":
(null)' # error/pdf.c/ReadPDFImage/788 at
ImageMagick.Wrapper.MagickImageCollection.HandleException(MagickException
exception) at ImageMagick.Wrapper.MagickImageCollection.Read(Byte[]
data, MagickReadSettings readSettings) at
ImageMagick.MagickImageCollection.Read(Byte[] data, MagickReadSettings
readSettings) at
__DynamicCode.Typeaeb039071464a22ae6518eaa5ec46c.OnExecute(PipelineContext1
context) in c:\Users\esbsvc\AppData\Local\Temp\xp42eval.0.cs:line 112
Any help with this would be appreciated
Mike H.
There are two likely problems:
1) The C# code is using a single copy of the Ghostscript DLL and you haven't built it to be thread safe (I cannot recall what the default is currently on Windows). In effect you are running multiple threads rather than processes.
2) You have a collision on file access. In order to interpret a PDF file it is necessary to jump around the file a lot, I would guess that two processes tried to relocate the file pointer simultaneously and one failed.
ImageMagick can't handle PDF files directly, unlike image formats (PDF is not an image format, its much more complex), so it will not need to invoke Ghostscript. If you were to try the same with PostScript files you might encounter the same problem. However, since PostScript files are read linearly you may not have a problem with those.
If you capture the Ghostscript back channel output (and stop using -dQUIET) then you might get some more useful information.
Since you say this 'will be hit by many systems' please check the terms of the AGPL and ensure that your usage is consistent with the licence.
The API documentation of Ghostscript (http://www.ghostscript.com/doc/current/API.htm) states the following:
The Win32 DLL gsdll32.dll can be used by multiple programs simultaneously, but only once within each process.
The version of Magick.NET that you are using does not handle this properly. I just pushed a patch to GIT repository of ImageMagick to make sure the DLL can be used only once. The first thread will use the library in memory and the second/third/etc. thread will forced to use the command line. Magick.NET 7.0.0.0022 has just been published and includes this change.

Using libpng in C#

if you want to save the image in PNG format you can use ImageFormat class of .Net.
But this class doesn't implemented compression for PNG files.
And for some reasons i can't save my file in JPEG format.
After Some researched i know libpng is the official PNG reference library.
But i can't find anything for using it in .net framework.
Does anyone know about this library and using it in .NET?
About libpng
Thanks in Advanced.
Another alternative: PngCs
But this class doesn't implemented compression for PNG files
What on earth does that mean? PNG is compressed by design.
ImageMagick is a very nice library that allows you (among other things) create PNG files.
Magick.NET is the .Net wrapper for this library. Using this library, you can simply convert/create image formats. E.g. from their documentation:
using (MagickImage image = new MagickImage("Snakeware.gif"))
{
image.Write("Snakeware.jpg");
}
Since libpng it's written in C ( ANSI C, even better ) you can build a bridge between that library and your C# application -> Use a C library from C# code
There are also other options, such as freeimage and the port of ImageMagick to .net but in this case it's still unclear to me what kind of project is this one, I don't get if it's official or not, if it's already dead or alive, if you are interested you can search for that name.
In simple terms PNG it's a open standard where JPEG it's a world full of patents and royalties, for this reason some libraries do not offer jpeg features or they offer a limited set of jpeg related features/algorithms. PNG it's also a real imaging standard about a file format definition, jpeg it's more like a container.

bitmaps to avi file c# .Net

I have a list of Bitmaps, how can I convert it to avi file using c#.net. Or how can we convert a set of images to video file ?
I do not need AVI to "Set of images", but I need "Set of Images" to AVI.
i used avifile wrapper in the past, work nicely
I think you need to use third party libraries
There is AVI File Wrapper or you can use ffmpeg in .NET.
Old question, but I have two tips to achieve it. We're now 2013, but all answers are good from 2004 to 2013. These answers are useful as it seems programmers rarely take the time to work on new encoders solution, and we often rely on the same old DLL and wrappers.
Images to MPEG-1
Based on this code, http://www.codeproject.com/Articles/5834/A-C-MPEG1-Image-Compression-Class, you can write a C# Images to MPEG-1 class compatible on any platform using C#.
Images to AVI
Convert just an image with 24 bits color (try to use a gradient generator to have the maximum number of colors) to a full frame AVI using ffmpeg. Take an hexadecimal editor, check how the header of the AVI is, and how the single image has been placed in the AVI. Now do it with two images. Check the header. Refer to the specification to know which value use in the header. You'll see you can easily build a Images to AVI from scratch without any wrapper, and use it on any platform.
Both are codes from scratch.
You can check out a simple library for writing AVI files that I've coded to use in my projects.
https://sharpavi.codeplex.com/
The sources include a sample screencast app which can be easily adapted to get the bitmaps from files if you need it.

How to load EPS files and draw them using WinForms

Is it possible to load and display EPS file using plain WinForms GDI+? If not, is there a free library to help out?
I seem to remember that Windows GDI supported EPS files, but after Googling around a bit, I am starting to doubt that memory.
All I want to do is load the file and draw it using a Graphics context.
I am aware that I can just use any program to convert the file to PNG or something and render it that way, but because I am trying to render at multiple resolutions, I would prefer to keep the vector data in the EPS file.
Thanks!
All the free or open source libraries I know that can convert EPS to other vector or raster format are all based on Ghostscript. You can invoke ghostscript directly, with wrapper provided or alternatively look at imagemagick. It is a very popular library for manipulating image graphics and has been around for a long time. It also internally relies on Ghostscript for handling EPS format. There is a .NET wrapper for it that you can find at http://imagemagick.codeplex.com/. You can read a bit about its background here too http://www.codeproject.com/KB/dotnet/ImageMagick_in_VBNET.aspx. There is also pstoedit that is also based on ghostscript to read EPS and allow export to format like WMF. You will need to the call to pstoedit API using interop in .NET
Besides Ghostscript there are several commercial products that I known of like ImageGear and LeadTools which will let you take EPS to almost any other kind of graphic formats.
You can use GhostScript to produce images from an EPS. Once you have an image you can then display that within your application.
In your question you indicated you want the output in a vector format which would preclude bitmaps, jpeg etc. Here are a couple of ways of getting a XAML file which is a vector file with extensive support by Microsoft.
Microsoft Expression Blend 3 and Design 3 can both open .ai (eps) files and convert them to vector formats, design and XAML respectively, so it is definitely possible.
I know it is relatively easy to automate most Microsoft Office applications like Word and Excel, but I have not seen any documented com inter-op assemblies for these Expression products.
Perhaps you can use the converters that are part of Expression in an undocumented way?
If that doesn't work here is plan B:
Here is a free converter that will convert .ai (eps) files to XAML. To use it you need Adobe Illustrator however.

Categories

Resources