0

.netを使用して白黒のtiffファイルをpngに変換する必要があります。しかし、色深度をピクセルあたり8ビットに減らすことはできません。常に24ビット/ピクセルでファイルを出力します。.netでこれを行う方法はありますか?またはそうするためのオープンソースのマネージドライブラリ?

私が使用しているコード。

public bool convertTiffToJpg(string sourceFile, string targetFile)
        {

            bool response = false;

            try
            {

                // Get individual Images from the original image
                Image sourceImage = Bitmap.FromFile(sourceFile);

                var total = sourceImage.GetFrameCount(FrameDimension.Page);
                var pageNumbers = Enumerable.Range(0, total).ToArray();
                Image[] sourceImages = new Image[pageNumbers.Length];
                for (int i = 0; i < pageNumbers.Length; i++)
                {
                    sourceImage.SelectActiveFrame(FrameDimension.Page, pageNumbers[i]);
                    float width = sourceImage.Width;
                    float height = sourceImage.Height;
                    ResizeImage(1000, sourceImage.Height, ref width, ref height);

                    using (var returnImage = new Bitmap(sourceImage, (int)width, (int)height))
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            returnImage.Save(ms, ImageFormat.Jpeg);
                            sourceImages[i] = Image.FromStream(ms);
                        }
                    }
                }

                // Merge individual Images into one Image
                var totalHeight = sourceImages.FirstOrDefault().Height * total;
                var totalWidth = sourceImages.FirstOrDefault().Width;
                using (var finalImage = new Bitmap(totalWidth, totalHeight))
                {
                    using (var g = Graphics.FromImage(finalImage))
                    {
                        // All other pages
                        for (int i = 0; i < pageNumbers.Length; i++)
                        {
                            g.DrawImage(sourceImages[i], new Point(0, sourceImages[i].Height * i));
                        }
                    }
                        ImageCodecInfo Codec = ImageCodecInfo.GetImageEncoders().Where(codec => codec.FormatID.Equals(ImageFormat.Png.Guid)).FirstOrDefault();

                        finalImage.Save(targetFile, Codec, GetCodedParams(50));
                }

                response = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return response;
        }

private EncoderParameters GetCodedParams(int? quality)
        {
            var imageQuality = quality ?? 80;
            EncoderParameter ratio = new EncoderParameter(Encoder.Quality, imageQuality);
            EncoderParameter light = new EncoderParameter(Encoder.ColorDepth, 8L);
            EncoderParameters codecParameters = new EncoderParameters(2);
            codecParameters.Param[0] = ratio;
            codecParameters.Param[1] = light;
            return codecParameters;
        }

しかし、それでも私は8ビットの深さのpngを取得できません。

よろしく。

4

1 に答える 1

0

これが私が思いついたものです。画像深度を減らすために画像量子化を使用しました。

http://msdn.microsoft.com/en-us/library/aa479306.aspx これは量子化器を入手したものです

 public bool convertTiffToPng(string sourceFile, string targetFile)
        {

            bool response = false;

            try
            {

                // Get individual Images from the original image
                Image sourceImage = Bitmap.FromFile(sourceFile);

                var total = sourceImage.GetFrameCount(FrameDimension.Page);
                var pageNumbers = Enumerable.Range(0, total).ToArray();
                Image[] sourceImages = new Image[pageNumbers.Length];
                for (int i = 0; i < pageNumbers.Length; i++)
                {
                    sourceImage.SelectActiveFrame(FrameDimension.Page, pageNumbers[i]);
                    float width = sourceImage.Width;
                    float height = sourceImage.Height;
                    ResizeImage(1024, sourceImage.Height, ref width, ref height);

                    using (var returnImage = new Bitmap(sourceImage, (int)width, (int)height))
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            returnImage.Save(ms, ImageFormat.Jpeg);
                            sourceImages[i] = Image.FromStream(ms);
                        }
                    }
                }
                 var p = new System.Collections.ArrayList();
                    p.Add(Color.Black);
                    p.Add(Color.Gray);
                    p.Add(Color.LightGray);
                    p.Add(Color.DarkGray);
                    p.Add(Color.White);
                    PaletteQuantizer quantizer = new PaletteQuantizer(p);
                    //OctreeQuantizer quantizer = new OctreeQuantizer(2,2);

                // Merge individual Images into one Image
                var totalHeight = sourceImages.FirstOrDefault().Height * total;
                var totalWidth = sourceImages.FirstOrDefault().Width;
                using (var finalImage = new Bitmap(totalWidth, totalHeight))
                {

                    using (var g = Graphics.FromImage(finalImage))
                    {
                        g.InterpolationMode = InterpolationMode.Low;
                        g.PixelOffsetMode = PixelOffsetMode.HighSpeed;

                        // All other pages
                        for (int i = 0; i < pageNumbers.Length; i++)
                        {
                            g.DrawImage(sourceImages[i], new Point(0, sourceImages[i].Height * i));
                        }
                    }

                    //finalImage.MakeTransparent(Color.White);
                    using (var toout = quantizer.Quantize(finalImage))
                    {
                        ImageCodecInfo Codec = ImageCodecInfo.GetImageEncoders().Where(codec => codec.FormatID.Equals(ImageFormat.Png.Guid)).FirstOrDefault();
                        toout.Save(targetFile, Codec, GetCodedParams(null));
                    }

                }

                response = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return response;
        }
于 2012-04-13T09:30:41.740 に答える