C# TIFF图像开发

NuGet安装控件:

Install-Package BitMiracle.LibTiff.NET -Version 2.4.560

实现代码:

复制代码
        private BitmapSource TiffToBitmap(string fileName)
        {
            Tiff tif = Tiff.Open(fileName, "r");
            if (tif == null)
            {
                return null;
            }
            // Find the width and height of the image
            FieldValue[] value = tif.GetField(TiffTag.IMAGEWIDTH);
            int width = value[0].ToInt();

            value = tif.GetField(TiffTag.IMAGELENGTH);
            int height = value[0].ToInt();

            // Read the image into the memory buffer
            int[] raster = new int[height * width];

            if (!tif.ReadRGBAImage(width, height, raster))
            {
                tif.Close();
                tif.Dispose();
                return null;
            }
            tif.Close();
            tif.Dispose();

            WriteableBitmap _wb = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, null);
            Int32Rect _rect = new Int32Rect(0, 0, _wb.PixelWidth, _wb.PixelHeight);
            int _bytesPerPixel = (_wb.Format.BitsPerPixel + 7) / 8;
            int _stride = _wb.PixelWidth * _bytesPerPixel;
            int _arraySize = _stride * _wb.PixelHeight;
            byte[] bits = new byte[_arraySize];
            for (int y = 0; y < _wb.PixelHeight; y++)
            {
                int rasterOffset = y * _wb.PixelWidth;
                int bitsOffset = (_wb.PixelHeight - y - 1) * _stride;

                for (int x = 0; x < _wb.PixelWidth; x++)
                {
                    int rgba = raster[rasterOffset++];
                    bits[bitsOffset++] = (byte)((rgba >> 16) & 0xff);
                    bits[bitsOffset++] = (byte)((rgba >> 8) & 0xff);
                    bits[bitsOffset++] = (byte)(rgba & 0xff);
                    bits[bitsOffset++] = (byte)((rgba >> 24) & 0xff);
                }
            }

            _wb.WritePixels(_rect, bits, _stride, 0);

            return _wb;
        }
复制代码

调用:

            MyImage.Source = TiffToBitmap("test.tif");

 

 

Bit Miracle - Software for the PDF, TIFF, and JPEG formats.
https://bitmiracle.com/

 

Tiff转换Bitmap处理方法 - CSDN博客
https://blog.csdn.net/kongwei521/article/details/8259461

posted @   wzwyc  阅读(5780)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示