使用c#.net合并两个tiff图像

问题描述 投票:0回答:3

在我的场景中,我在不同位置有两个 tiff 图像

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

我需要以编程方式将这些图像合并为单个图像

提出一些想法/代码。

谢谢,

开发

c# image-processing tiff
3个回答
3
投票

要仅使用框架类来执行此操作,您基本上可以这样做:

  1. 将每个 TIFF 图像加载到 Bitmap 对象中,例如使用
    Image.FromFile
  2. 使用编码器参数保存第一页
    Encoder.SaveFlag
    =
    EncoderValue.MultiFrame
  3. 使用
    Encoder.SaveFlag
    ()
     将每个后续页面保存到编码器参数 
    EncoderValue.FrameDimensionPage
     = 
    Bitmap.SaveAdd
  4. 的同一文件中

它看起来像这样:

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

0
投票

可能有几种方法可以“合并”图像。这是伪代码中的几个:

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

0
投票
    static void Main()
    {
        string[] archivos;
        int largo = 0;
        string nombreArchivoNuevo = "";
        string ruta = ConfigurationSettings.AppSettings["carpetaVigilada"];
        string destino = ConfigurationSettings.AppSettings["carpetaDestino"];
        string temp = ConfigurationSettings.AppSettings["carpetaIntermedia"];
        archivos = Directory.GetFiles(ruta, "*.tif", SearchOption.AllDirectories);
        foreach (String archivo in archivos)
        {
            string nameFile = "";
            string strImageFilePath = archivo;
            nombreArchivoNuevo = archivo.Substring(archivo.LastIndexOf("\\") + 1);
            nombreArchivoNuevo = nombreArchivoNuevo.Replace(".tif", "");
            largo = nombreArchivoNuevo.Length;
            if (nombreArchivoNuevo.Substring(largo - 1, 1).Equals("C"))
            {
                var source = Image.FromFile(strImageFilePath);
                Rectangle crop = new Rectangle(0, 0, 2560, 3270);
                Rectangle crop2 = new Rectangle(2560, 0, 2560, 3270);
                var bmp = new Bitmap(2560, 3270);
                //var bmp = new Bitmap(3277, 8418);
                var bmp2 = new Bitmap(2560, 3270);
                using (var gr = Graphics.FromImage(bmp))
                {
                    gr.DrawImage(source, new Rectangle(0, 0, bmp.Width, bmp.Height), crop, GraphicsUnit.Pixel);
                    RotateFlipType rotation = RotateFlipType.Rotate270FlipXY;
                    bmp.RotateFlip(rotation);
                    bmp.Save(temp+ nombreArchivoNuevo+"_left.tif");
                }
                using (var gr2 = Graphics.FromImage(bmp2))
                {
                    gr2.DrawImage(source, new Rectangle(0, 0, bmp2.Width, bmp2.Height), crop2, GraphicsUnit.Pixel);
                    bmp2.Save(temp + nombreArchivoNuevo +"_right.tif");
                }

                List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
                System.Drawing.Bitmap finalImage = null;

                try
                {
                    int width = 0;
                    int height = 0;
                    string[] files = Directory.GetFiles(temp,"*.tif", SearchOption.AllDirectories);
                    foreach (string image in files)
                    {
                        //create a Bitmap from the file and add it to the list
                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);

                        //update the size of the final bitmap
                        width += bitmap.Width;
                        height = bitmap.Height > height ? bitmap.Height : height;

                        images.Add(bitmap);
                    }

                    //create a bitmap to hold the combined image
                    finalImage = new System.Drawing.Bitmap(width, height);

                    //get a graphics object from the image so we can draw on it
                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
                    {
                        //set background color
                        g.Clear(System.Drawing.Color.White);

                        //go through each image and draw it on the final image
                        int offset = 0;
                        foreach (System.Drawing.Bitmap image in images)
                        {
                            g.DrawImage(image, new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                            offset += image.Width;
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (finalImage != null)
                        finalImage.Dispose();

                    throw ex;
                }
                finally
                {
                    //clean up memory
                    foreach (System.Drawing.Bitmap image in images)
                    {
                        image.Dispose();
                    }
                }
                finalImage.Save(destino+nombreArchivoNuevo+"new.tif");
                File.Delete(temp + nombreArchivoNuevo + "_left.tif");
                File.Delete(temp + nombreArchivoNuevo + "_right.tif");
            }
        }
        //Application.EnableVisualStyles();
        //Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new Form1());
    }
© www.soinside.com 2019 - 2024. All rights reserved.