zoukankan      html  css  js  c++  java
  • C# 读写16位tif图片灰度数据

                // Instantiate the dialog box
                var dlg = new Microsoft.Win32.OpenFileDialog
                {
                    FileName = "",
                    Filter = "All Files|*.*|PNG Files|*.png|JPG Files|*.jpg|TIF Files|*.tif"
                };
                var result = dlg.ShowDialog();
                // Process open file dialog box results
                if (result == true)
                {
                    // Open a Stream and decode a TIFF image.
                    Stream imageStreamSource = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                    var decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    BitmapSource bitmapSource = decoder.Frames[0];
                    Int32 PixelHeight = bitmapSource.PixelHeight; // 图像高度
                    Int32 PixelWidth = bitmapSource.PixelWidth;   // 图像宽度
                    Int32 Stride = (PixelWidth * bitmapSource.Format.BitsPerPixel + 7) / 8;  // 跨距宽度
                    if(bitmapSource.Format.BitsPerPixel ==8)
                    {
                        Byte[] gray8 = new Byte[PixelHeight * PixelWidth];//8bit 位深
                        bitmapSource.CopyPixels(gray8, Stride, 0);
                    }
                    else if (bitmapSource.Format.BitsPerPixel == 16)
                    {
                        ushort[] gray16 = new ushort[PixelHeight * PixelWidth];//16bit 位深
                        bitmapSource.CopyPixels(gray16, Stride, 0);
                    }
                }
    读16位tif图片灰度数据

    效果如下:

                unsafe
                {
                    int lenth = width * height;//相机buff照片的宽度和高度
                    UInt16* bufi = (UInt16*)buf.ToPointer();//从相机buff中读取16bit灰度数据
                    UInt16[] data = new UInt16[lenth];
                    byte[] bufBytes = new byte[lenth*2];
    
                    for (int i = 0; i < lenth; i++)
                    {
                        data[i] = bufi[i];
                    }
                    IntPtr intptr = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0);
                    Marshal.Copy(intptr,bufBytes,0,lenth*2);//将16bit数据转化成两个8bit数据
    
                    BitmapPalette myPalette = BitmapPalettes.Gray16;
                    int rawStride = (width * 16 + 7) / 8;//16bit 位深
    
                    BitmapSource image = BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray16, myPalette, bufBytes, rawStride);
    
                    FileStream f = new FileStream(name, FileMode.Create);
                    TiffBitmapEncoder encoder = new TiffBitmapEncoder();//tif编码格式
    
                    encoder.Compression = TiffCompressOption.Zip;
                    encoder.Frames.Add(BitmapFrame.Create(image));
                    encoder.Save(f);
                    f.Close();
                }
    从相机buff中将16bit灰度数据写入tif照片

    参考链接https://docs.microsoft.com/zh-cn/dotnet/desktop/wpf/graphics-multimedia/how-to-encode-and-decode-a-tiff-image?view=netframeworkdesktop-4.8

  • 相关阅读:
    【读书笔记】之《把时间当做朋友》
    软件工程——五大模型
    VB中的GetUserName函数
    VB中 vbp vbw frm frx log bas 等扩展名大全
    【机房收费系统】——基本数据设定的理解
    在64位WindowsServer2012R2中安装Oracle10g第二版(10.2.0.4.0)-20160106
    Linux——vi命令详解
    使用Postman测试WebService接口
    npm和yarn的淘宝镜像添加
    yarn配置私有registry
  • 原文地址:https://www.cnblogs.com/lizhiqiang0204/p/14945371.html
Copyright © 2011-2022 走看看