c# 如何将16位/通道图转换为8位的 Bitmap16位tif内存不足 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 16位TIFF在转换为8位BitmapConvert 16-bit grayscale TIFF to a 8-bit System.Drawing.Bitmapusing 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); } } }} DLL - 回调函数? 请问下VS2008中单步调试的问题! 计算两个工作日之间的时间差 短信转码的问题 没有了目标 求sql语句 vs2003不能用,怎么回事? 参数设置的问题??、 方法就一定是函数,属性就一定是一个变量存储的值吗?有什么区别呀 请问C#中的[]是做什么用的? WinForm自动生成的Dispose函数没有被调用啊 为什么global名称空间在使用的时候是用"::"而不是"."?
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);
}
}
}
}