Convert different picture formats (jpg, gif, png, etc.) to TIFF format Convert different picture formats (jpg, gif, png, etc.) to TIFF format asp.net asp.net

Convert different picture formats (jpg, gif, png, etc.) to TIFF format


If you create an Image object in .NET, you can save it as a TIFF. It is one of the many ImageFormat choices at your disposal.

Example:

var png = Image.FromFile("some.png");png.Save("a.tiff", ImageFormat.Tiff);

You'll need to include the System.Drawing assembly in your project. That assembly will give you a lot of image manipulation capabilities. Hope that helps.


Intro Note:

  1. This answer cover the Bounty Question; which is: How do we convert multiple files into 1 tiff? For example, let's say have pdfs, jpegs, pngs, and I'd like to create 1 tiff out of them?
  2. In this answer I use .net implementation of https://imagemagick.org/index.php for image manipulation and and Ghostscript for helping read an AI/EPS/PDF/PS file so we can translate it to image files both are credible and official source.
  3. After I answered this question I got some extra question in my email asking other merging options, I have therefore extended my answer.

IMO there are 2 steps to your goal:

  1. Install required tools for pdf conversion
  2. Take all images including pdf formatted files from source and merge them together in one tiff file.

1. Install tools that helps Pdf to Image conversion:

Step 1 is only required if you intend to convert AI/EPS/PDF/PS file formats. Otherwise just jump to step2.

To make it possible converting pdf to any image format, we need a library that can read pdf files and we need a tool to convert it to image type. For this purpose, we will need to install Ghostscript (GNU Affero General Public License).

Here after, we need to install ImageMagick.net for .net in Visual Studio, nuget link.

So far so good.

2. Code part

Second and Last step is we need to read files (png, jpg, bmp, pdf etc) from folder location and add each file to MagickImageCollection, then we have several options to merge use AppendHorizontally, AppendVertically, Montage or Multiple page Tiff. ImageMagick has tons of features, like resizing, resolution etc, this is just example to demonstrate merging features:

public static void MergeImage(string src, string dest, MergeType type = MergeType.MultiplePage){    var files = new DirectoryInfo(src).GetFiles();    using (var images = new MagickImageCollection())    {        foreach (var file in files)        {            var image = new MagickImage(file)            {                Format = MagickFormat.Tif,                Depth = 8,            };            images.Add(image);        }        switch (type)        {            case MergeType.Vertical:                using (var result = images.AppendVertically())                {                    result.AdaptiveResize(new MagickGeometry(){Height = 600, Width = 800});                    result.Write(dest);                }                break;            case MergeType.Horizontal:                using (var result = images.AppendHorizontally())                {                    result.AdaptiveResize(new MagickGeometry(){Height = 600, Width = 800});                    result.Write(dest);                }                break;            case MergeType.Montage:                var settings = new MontageSettings                {                    BackgroundColor = new MagickColor("#FFF"),                    Geometry = new MagickGeometry("1x1<")                };                using (var result = images.Montage(settings))                {                    result.Write(dest);                }                break;            case MergeType.MultiplePage:                images.Write(dest);                break;            default:                throw new ArgumentOutOfRangeException(nameof(type), type, "Un-support choice");        }        images.Dispose();    }}public enum MergeType{    MultiplePage,    Vertical,    Horizontal,    Montage}

To run the code

public static void Main(string[] args){    var src = @"C:\temp\Images";    var dest1 = @"C:\temp\Output\MultiplePage.tiff";    var dest2 = @"C:\temp\Output\Vertical.tiff";    var dest3 = @"C:\temp\Output\Horizontal.tiff";    var dest4 = @"C:\temp\Output\Montage.tiff";    MergeImage(src, dest1);    MergeImage(src, dest2, MergeType.Vertical);    MergeImage(src, dest3, MergeType.Horizontal);    MergeImage(src, dest4, MergeType.Montage);}

Here is 4 input files in C:\temp\Images:

enter image description hereenter image description hereenter image description hereenter image description here

After running the code, we get 4 new files under C:\temp\Output looks like this:

enter image description here4 page Multiple Page Tiff

enter image description here4 image Vertical Merge

enter image description here4 image Horizontal Merge

enter image description here4 image Montage Merge

Final note:

  1. it is possible to merge multiple images to tiff using System.Drawing; and using System.Drawing.Imaging; with out using ImageMagick, but pdf does require a third party conversion library or tool, therefore I use Ghostscript and ImageMagick for C#.
  2. ImageMagick has many features, so you can change the resolution, size of output file etc. it is well recognized library.

Disclaimer: A part of this answer is taken from my my personal web site https://itbackyard.com/how-to-convert-ai-eps-pdf-ps-to-image-file/ with source code to github.


To be covert the image in tif format.In the below example to be convert the image and set to a text box.to be see the image in text box is (.tif formate).This sources code is working.

private void btn_Convert(object sender, EventArgs e)    {        string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);        newName = newName + ".tif";        try        {            img.Save(newName, ImageFormat.Tiff);        }        catch (Exception ex)        {            string error = ee.Message.ToString();            MessageBox.Show(MessageBoxIcon.Error);        }        textBox2.Text = System.IO.Path.GetFullPath(newName.ToString());    }