4

In my scenario i have two tiff image in different location say

c:/temp/img1.tiff and x:/temp/img2.tiff.

I need to merge these images as a single image programatically

suggest some ideas/codes.

Thanks,

Dev

Jalal Said
  • 15,906
  • 7
  • 45
  • 68
Dev
  • 101
  • 1
  • 4
  • 7

2 Answers2

3

To do this using just the Framework classes, you basically do this:

  1. Load each of your TIFF images into a Bitmap object, e.g. using Image.FromFile.
  2. Save the first page with an encoder parameter Encoder.SaveFlag = EncoderValue.MultiFrame
  3. Save each subsequent page to the same file with an encoder parameter of Encoder.SaveFlag = EncoderValue.FrameDimensionPage using Bitmap.SaveAdd()

It would look something like this:

ImageCodecInfo tiff = null;
foreach ( ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders() )
{
    if ( codec.MimeType == "image/tiff" )
    {
        tiff = codec;
        break;
    }
}

Encoder encoder = Encoder.SaveFlag;
EncoderParameters parameters = new EncoderParamters(1);
parameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame);

bitmap1.Save(newFileName, tiff, parameters);

parameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.FrameDimensionPage);
bitmap2.SaveAdd(newFileName, tiff, paramters);

parameters.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.Flush);
bitmap2.SaveAdd(parameters);
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • I will also add that my company long ago decided to purchase a toolkit (in our case, we use LEADTools) that uses unmanaged GDI for this type of thing, as GDI+ is not exactly the fastest-performing part of the Framework, and the code gets tedious. Depending on your budgetary limitations, you might want to look into that. – Michael Edenfield Jul 01 '11 at 13:54
  • the 'Image' class does not have a method 'SaveAdd' that takes a string. – Steffen Winkler Aug 06 '15 at 08:29
0

There could be a couple ways to "merge" images. Here's a couple in pseudocode:

var NewImage = new Image();

ForEach (curPixel in img1)
{
   var newColor = new Color();
   newColor.RGB = (Pixel.Color.RGB + img2.PixelAt(curPixel.Location).Color.RGB) / 2
   NewImage.PixelAt(curPixel.Location) = new Pixel(newColor);
}

///OR    

int objCounter = 0;
ForEach (curPixel in Image)
{
   if(objCounter % 2 == 0){
      NewImage.PixelAt(curPixel.Location) = img1.PixelAt(curPixel.Location);
   } else {
      NewImage.PixelAt(curPixel.Location) = img2.PixelAt(curPixel.Location);
   }
}
Nick Heidke
  • 2,787
  • 2
  • 34
  • 58