gpt4 book ai didi

c# - 在 C# 中将双色 TIFF 转换为双色 PNG

转载 作者:太空狗 更新时间:2023-10-29 20:22:20 25 4
gpt4 key购买 nike

我需要将双色(黑白)TIFF 文件转换为另一种格式以便通过网络浏览器显示,目前我们使用的是 JPG,但格式并不重要。从周围的阅读来看,.NET 似乎并不容易支持编写双色图像,因此我们最终得到 ~1MB 文件而不是 ~100K 文件。我正在考虑使用 ImageMagick 来执行此操作,但理想情况下我想要一个尽可能不需要此功能的解决方案。

当前代码片段(也对图像进行了一些调整):

using (Image img = Image.FromFile(imageName))
{
using (Bitmap resized = new Bitmap(resizedWidth, resizedHeight)
{
using (Graphics g = Graphics.FromImage(resized))
{
g.DrawImage(img, new Rectangle(0, 0, resized.Width, resized.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
}

resized.Save(outputFilename, System.Drawing.Imaging.ImageFormat.Jpeg);

}
}

有什么办法可以实现吗?

谢谢。

最佳答案

我相信通过检查 resized 位图是否为 PixelFormat.Format1bppIndexed 可以解决问题。如果不是,您应该将其转换为 1bpp 位图,然后您可以毫无问题地将其另存为黑白 png。

换句话说,您应该使用以下代码而不是 resized.Save(outputFilename, System.Drawing.Imaging.ImageFormat.Jpeg);

if (resized.PixelFormat != PixelFormat.Format1bppIndexed)
{
using (Bitmap bmp = convertToBitonal(resized))
bmp.Save(outputFilename, System.Drawing.Imaging.ImageFormat.Png);
}
else
{
resized.Save(outputFilename, System.Drawing.Imaging.ImageFormat.Png);
}

我为 convertToBitonal 使用以下代码:

private static Bitmap convertToBitonal(Bitmap original)
{
int sourceStride;
byte[] sourceBuffer = extractBytes(original, out sourceStride);

// Create destination bitmap
Bitmap destination = new Bitmap(original.Width, original.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 buffer for destination bitmap bits
int 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 = destination.Height;
int width = destination.Width;
int threshold = 500;

for (int y = 0; y < height; y++)
{
sourceIndex = y * sourceStride;
destinationIndex = y * destinationData.Stride;
destinationValue = 0;
pixelValue = 128;

for (int x = 0; x < width; x++)
{
// Compute pixel brightness (i.e. total of Red, Green, and Blue values)
pixelTotal = sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2] +
sourceBuffer[sourceIndex + 3];

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;
}

Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize);
destination.UnlockBits(destinationData);
return destination;
}

private static byte[] extractBytes(Bitmap original, out int stride)
{
Bitmap source = null;

try
{
// 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);

stride = sourceData.Stride;
return sourceBuffer;
}
finally
{
if (source != original)
source.Dispose();
}
}

关于c# - 在 C# 中将双色 TIFF 转换为双色 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3414072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com