C# 如何将TIF转换为黑白单色TIF
我想用Magick.NET 为了将TIF转换为黑白单色TIF,但手册并未对此进行详细说明 我尝试过这段代码,但我不确定这种方法是否正确C# 如何将TIF转换为黑白单色TIF,c#,image,tiff,image-conversion,magick.net,C#,Image,Tiff,Image Conversion,Magick.net,我想用Magick.NET 为了将TIF转换为黑白单色TIF,但手册并未对此进行详细说明 我尝试过这段代码,但我不确定这种方法是否正确 using (MagickImage image = new MagickImage("input.tif")) { image.CompressionMethod = CompressionMethod.Group4; image.Write("output.tif"); } 需要帮助。谢谢大家! 我发现.NET代码可以毫无问题地做同样的事情 p
using (MagickImage image = new MagickImage("input.tif"))
{
image.CompressionMethod = CompressionMethod.Group4;
image.Write("output.tif");
}
需要帮助。谢谢大家! 我发现.NET代码可以毫无问题地做同样的事情
private void convert\u单击(对象发送方,事件参数e)
{
//从磁盘加载位图
位图originalBitmap=新位图(@.\..\Bitonal In.tif”);
//显示图像
originalImage.Image=originalBitmap;
//将位图转换为图形的RGB格式
位图rgbBitmap=Converter.convertorgb(originalBitmap);
//将图像转换为二进制以保存到文件
位图bitonalBitmap=Converter.ConvertToBitonal(rgbBitmap);
//显示转换后的图像
convertedImage.Image=位位图;
//获取表示TIFF编解码器的ImageCodeInfo对象。
ImageCodecInfo ImageCodecInfo=GetEncoderInfo(“图像/tiff”);
System.Drawing.Imaging.Encoder编码器=System.Drawing.Imaging.Encoder.Compression;
EncoderParameters EncoderParameters=新的EncoderParameters(1);
//使用组IV压缩将位图另存为TIFF文件。
编码器参数编码器参数=新编码器参数(编码器,(长)编码器值压缩CCITT4);
encoderParameters.Param[0]=encoderParameter;
bitonalBitmap.Save(@.\..\bitonalout.tif),imageCodecInfo,encoderParameters);
}
使用系统图;
使用系统、绘图、成像;
使用System.Runtime.InteropServices;
命名空间位转换器
{
公共静态类转换器
{
公共静态位图转换器GB(位图原件)
{
位图newImage=新位图(original.Width、original.Height、PixelFormat.Format32bppArgb);
newImage.SetResolution(原始.水平分辨率,原始.垂直分辨率);
使用(Graphics g=Graphics.FromImage(newImage))
{
g、 DrawImageUnscaled(原始,0,0);
}
返回新图像;
}
公共静态位图转换文件(位图原件)
{
位图源=空;
//如果原始位图不是32 BPP、ARGB格式,则转换
if(original.PixelFormat!=PixelFormat.Format32bppArgb)
{
source=新位图(original.Width、original.Height、PixelFormat.Format32bppArgb);
source.SetResolution(原始水平分辨率、原始垂直分辨率);
使用(Graphics g=Graphics.FromImage(源))
{
g、 DrawImageUnscaled(原始,0,0);
}
}
其他的
{
来源=原件;
}
//在内存中锁定源位图
BitmapData sourceData=source.LockBits(新矩形(0,0,source.Width,source.Height),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
//将图像数据复制到二进制数组
int imageSize=sourceData.Stride*sourceData.Height;
byte[]sourceBuffer=新字节[imageSize];
Marshal.Copy(sourceData.Scan0,sourceBuffer,0,imageSize);
//解锁源位图
source.UnlockBits(sourceData);
//创建目标位图
位图目标=新位图(source.Width、source.Height、PixelFormat.format1bpindexed);
destination.SetResolution(原始水平分辨率、原始垂直分辨率);
//在内存中锁定目标位图
BitmapData destinationData=destination.LockBits(新矩形(0,0,destination.Width,destination.Height),ImageLockMode.WriteOnly,PixelFormat.Format1BPindexed);
//创建目标缓冲区
imageSize=destinationData.Stride*destinationData.Height;
byte[]destinationBuffer=新字节[imageSize];
int sourceIndex=0;
int destinationIndex=0;
整数像素总数=0;
字节destinationValue=0;
int像素值=128;
int height=源高度;
int-width=source.width;
int阈值=500;
//迭代行
对于(int y=0;y阈值)
{
destinationValue+=(字节)像素值;
}
如果(像素值==1)
{
destinationBuffer[destinationIndex]=destinationValue;
destinationIndex++;
destinationValue=0;
像素值=128;
}
其他的
{
像素值>>=1;
}
sourceIndex+=4;
}
如果(像素值!=128)
{
destinationBuffer[destinationIndex]=destinationValue;
}
}
private void convert_Click(object sender, EventArgs e)
{
// Load a bitmap from disk
Bitmap originalBitmap = new Bitmap(@"..\..\Bitonal-In.tif");
// Display image
originalImage.Image = originalBitmap;
// Convert bitmap to RGB format for drawing
Bitmap rgbBitmap = Converter.ConvertToRGB(originalBitmap);
// Convert image to bitonal for saving to file
Bitmap bitonalBitmap = Converter.ConvertToBitonal(rgbBitmap);
// Display converted image
convertedImage.Image = bitonalBitmap;
// Get an ImageCodecInfo object that represents the TIFF codec.
ImageCodecInfo imageCodecInfo = GetEncoderInfo("image/tiff");
System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Compression;
EncoderParameters encoderParameters = new EncoderParameters(1);
// Save the bitmap as a TIFF file with group IV compression.
EncoderParameter encoderParameter = new EncoderParameter(encoder, (long)EncoderValue.CompressionCCITT4);
encoderParameters.Param[0] = encoderParameter;
bitonalBitmap.Save(@"..\..\Bitonal-Out.tif", imageCodecInfo, encoderParameters);
}
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace BitonalConverter
{
public static class Converter
{
public static Bitmap ConvertToRGB(Bitmap original)
{
Bitmap newImage = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
newImage.SetResolution(original.HorizontalResolution, original.VerticalResolution);
using (Graphics g = Graphics.FromImage(newImage))
{
g.DrawImageUnscaled(original, 0, 0);
}
return newImage;
}
public static Bitmap ConvertToBitonal(Bitmap original)
{
Bitmap source = null;
// If original bitmap is not already in 32 BPP, ARGB format, then convert
if (original.PixelFormat != PixelFormat.Format32bppArgb)
{
source = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
source.SetResolution(original.HorizontalResolution, original.VerticalResolution);
using (Graphics g = Graphics.FromImage(source))
{
g.DrawImageUnscaled(original, 0, 0);
}
}
else
{
source = original;
}
// Lock source bitmap in memory
BitmapData sourceData = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// Copy image data to binary array
int imageSize = sourceData.Stride * sourceData.Height;
byte[] sourceBuffer = new byte[imageSize];
Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, imageSize);
// Unlock source bitmap
source.UnlockBits(sourceData);
// Create destination bitmap
Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
destination.SetResolution(original.HorizontalResolution, original.VerticalResolution);
// Lock destination bitmap in memory
BitmapData destinationData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);
// Create destination buffer
imageSize = destinationData.Stride * destinationData.Height;
byte[] destinationBuffer = new byte[imageSize];
int sourceIndex = 0;
int destinationIndex = 0;
int pixelTotal = 0;
byte destinationValue = 0;
int pixelValue = 128;
int height = source.Height;
int width = source.Width;
int threshold = 500;
// Iterate lines
for (int y = 0; y < height; y++)
{
sourceIndex = y * sourceData.Stride;
destinationIndex = y * destinationData.Stride;
destinationValue = 0;
pixelValue = 128;
// Iterate pixels
for (int x = 0; x < width; x++)
{
// Compute pixel brightness (i.e. total of Red, Green, and Blue values) - Thanks murx
// B G R
pixelTotal = sourceBuffer[sourceIndex] + sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2];
if (pixelTotal > threshold)
{
destinationValue += (byte)pixelValue;
}
if (pixelValue == 1)
{
destinationBuffer[destinationIndex] = destinationValue;
destinationIndex++;
destinationValue = 0;
pixelValue = 128;
}
else
{
pixelValue >>= 1;
}
sourceIndex += 4;
}
if (pixelValue != 128)
{
destinationBuffer[destinationIndex] = destinationValue;
}
}
// Copy binary image data to destination bitmap
Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize);
// Unlock destination bitmap
destination.UnlockBits(destinationData);
// Dispose of source if not originally supplied bitmap
if (source != original)
{
source.Dispose();
}
// Return
return destination;
}
}
}