c# 如何将16位/通道图转换为8位的

矿泉水 2014-05-24 06:10:26
Bitmap b1 = new Bitmap(“filename“);//执行这句话的时候报内存不足,此是处理的是16位的tif文件
当我用ps把16的tif转为8位的时候,再运行,正常!

这是什么原理呢?难道是16位的不能用用吗?那么使用c#方法如何实现这种转换?请各位支招!
...全文
643 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiejinquanli99 2015-09-10
  • 打赏
  • 举报
回复
请问,BitMiracle.LibTiff.Classic这个引用怎么添加啊?
save4me 2014-05-25
  • 打赏
  • 举报
回复
16位TIFF在转换为8位Bitmap Convert 16-bit grayscale TIFF to a 8-bit System.Drawing.Bitmap

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

using BitMiracle.LibTiff.Classic;

namespace BitMiracle.LibTiff.Samples
{
    public static class Convert16BitTo8Bit
    {
        public static void Main()
        {
            using (Bitmap tiff8bit = getBitmap8Bit(@"Sample Data\16bit.tif"))
            {
                if (tiff8bit == null)
                {
                    Console.WriteLine("Failed to convert image. Maybe input image does not exist or is not 16 bit.");
                    return;
                }

                tiff8bit.Save("Convert16BitTo8Bit.bmp");
                Process.Start("Convert16BitTo8Bit.bmp");
            }

        }

        private static Bitmap getBitmap8Bit(string inputName)
        {
            Bitmap result;

            using (Tiff tif = Tiff.Open(inputName, "r"))
            {
                FieldValue[] res = tif.GetField(TiffTag.IMAGELENGTH);
                int height = res[0].ToInt();

                res = tif.GetField(TiffTag.IMAGEWIDTH);
                int width = res[0].ToInt();

                res = tif.GetField(TiffTag.BITSPERSAMPLE);
                short bpp = res[0].ToShort();
                if (bpp != 16)
                    return null;

                res = tif.GetField(TiffTag.SAMPLESPERPIXEL);
                short spp = res[0].ToShort();
                if (spp != 1)
                    return null;

                res = tif.GetField(TiffTag.PHOTOMETRIC);
                Photometric photo = (Photometric)res[0].ToInt();
                if (photo != Photometric.MINISBLACK && photo != Photometric.MINISWHITE)
                    return null;

                int stride = tif.ScanlineSize();
                byte[] buffer = new byte[stride];

                result = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
                byte[] buffer8Bit = null;

                for (int i = 0; i < height; i++)
                {
                    Rectangle imgRect = new Rectangle(0, i, width, 1);
                    BitmapData imgData = result.LockBits(imgRect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

                    if (buffer8Bit == null)
                        buffer8Bit = new byte[imgData.Stride];
                    else
                        Array.Clear(buffer8Bit, 0, buffer8Bit.Length);

                    tif.ReadScanline(buffer, i);
                    convertBuffer(buffer, buffer8Bit);

                    Marshal.Copy(buffer8Bit, 0, imgData.Scan0, buffer8Bit.Length);
                    result.UnlockBits(imgData);
                }
            }

            return result;
        }

        private static void convertBuffer(byte[] buffer, byte[] buffer8Bit)
        {
            for (int src = 0, dst = 0; src < buffer.Length; dst++)
            {
                int value16 = buffer[src++];
                value16 = value16 + (buffer[src++] << 8);
                buffer8Bit[dst] = (byte)(value16 / 257.0 + 0.5);
            }
        }
    }
}

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧