投稿者 snowmansnow  (社会人) 投稿日時 2021/8/29 17:16:47
こんばんは、
 魔界の仮面弁士様の紹介していた
  https://dobon.net/vb/dotnet/graphics/createmultitiff.htmlに載っていた
  https://www.codeproject.com/Articles/16904/Save-images-into-a-multi-page-TIFF-file-or-add-imaをフォームアプリケーションのクラスライブラリにしてみました
  文字数が多いので分けます
  
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.IO;

namespace WindowsFormsAppTiffTo1L_CL
{
    [Guid(TiffTo1L.ClassId)]
    public class  TiffTo1L
    {
        //  COM用のGUID値
        public const string ClassId = "14BC0BB5-22E6-45C8-8656-45D84E25F60D";

        //https://www.codeproject.com/Articles/16904/Save-images-into-a-multi-page-TIFF-file-or-add-ima
        //This function can save newly scanned images on existing single page or multipage file
        public String OriginDest(string fileName, string type,long col,string orig,float dpi)
        {
            Image origionalFile = null;
            FileStream fr = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite);
            origionalFile = Image.FromStream(fr);
            int PageNumber = getPageNumber(origionalFile);

            if (PageNumber > 1)//Existing File is multi page tiff file
            {
                saveImageExistingMultiplePage(origionalFile, type, PageNumber, orig, col,dpi);
            }
            else if (PageNumber == 1)//Existing file is single page file
            {
                saveImageExistingSinglePage(origionalFile, type, orig, col,dpi);
            }
            fr.Flush();
            fr.Close();
           // System.IO.File.Replace("shreeTemp.tif", fileName, "Backup.tif", true);
            return "ok";
        }

        public ImageCodecInfo getCodecForstring(string type)
        {
            ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();

            for (int i = 0; i < info.Length; i++)
            {
                string EnumName = type.ToString();
                if (info[i].FormatDescription.Equals(EnumName))
                {
                    return info[i];
                }
            }
            return null;

        }
    
        public void saveImageExistingSinglePage(Image origionalFile, string type, string location,long col1,float dpi)
        {
            //Now load the Codecs 
            ImageCodecInfo codecInfo = getCodecForstring(type);

            EncoderParameter SaveEncodeParam;
            EncoderParameter CompressionEncodeParam;
            EncoderParameters EncoderParams = new EncoderParameters(3);
            EncoderParameter ColorEncodeParam;

            // Save the first page (frame).
            SaveEncodeParam = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
            CompressionEncodeParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionNone);
            // Save the image with a color depth of 24 bits per pixel.
            ColorEncodeParam = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth,col1);
            EncoderParams.Param[0] = CompressionEncodeParam;
            EncoderParams.Param[1] = SaveEncodeParam;
            EncoderParams.Param[2] = ColorEncodeParam;

            origionalFile = ConvertToBitonal((Bitmap)origionalFile,dpi);
            origionalFile.Save(location, codecInfo, EncoderParams);

            SaveEncodeParam = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
            EncoderParams.Param[0] = SaveEncodeParam;
            origionalFile.SaveAdd(EncoderParams);
        }