- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要将双色(黑白)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/
在我们用 ASP .NET 构建的 Web 应用程序中,需要允许用户查看 TIFF 图像文件,但不能打印、保存或复制这些文档。我们了解用户可能会打印屏幕,因此没有任何解决方案是 100% 万无一失的。
我有存储 TIFF 图像的旧 Eastman/eiStream/OpenText 文档库。 部分 TIFF 带有注释,注释存储在 TIFF 头中;这些我可以找到 TIFF 标题布局的规范和注释本身的布
并非所有图像阅读器都支持 BigTIFF,我想根据文件的 TIFF 格式识别文件。如何确定现有 tiff 文件是以标准 TIFF 格式编写的,还是使用 BigTIFF 格式编写的? 最佳答案 pyth
我使用 FreeImage 处理多页 TIFF 文件,有时我在 FIBITMAP 中有一个 TIFF 页面,我需要知道它的压缩情况。知道如何做到这一点吗? 最佳答案 FreeImage 没有内置函数来
我在目录 c:\temp 中有 600 个 TIFF 文件。 文件名如下: 001_1.tif, 001_2.tif, 001_3.tif 002_1.tif, 002_2.tif, 002_3.ti
我正在使用 netbeans 平台用 java 制作 DesktopApp。在我的应用程序中,我使用 16 位、tiff、灰度图像并对该图像进行处理。现在,我想使用 16 位、tiff、灰度图像(或
在我的场景中,我有 3 个或更多的多页 tiff 图像,我需要将它们合并成一个 tiff 图像。 下面是我试过的代码。它合并到单个 tiff 图像中,但仅与所有 tiff 图像的第一页合并。 priv
我想在选定的点添加图像的温度数据。它是否有任何预定义的属性,或者我们是否可以在 Tiff 文件中创建自定义/私钥来存储图像的温度数据。 回答: 使用 TIFF 库文档在 TIFF 文件中创建您自己的自
我已经编写了多个程序变体。该程序的目的是将“任何文件类型”转换为该文件的 TIFF 图像表示形式,就像使用打印机打印一样。 我目前正在使用我向其发送文件的第三方打印机驱动程序,它输出一个 TIFF 图
我在 Windows 7 32/64 位上使用 VS2008。 我正在尝试使用 IPP 处理 tiff 图像。我做了一些搜索,但没有发现英特尔 IPP 加载 tiff 图像的任何可能性。所以我使用 l
这是我用来从输入元素加载文件的 JavaScript 代码。图片格式包括jpeg、png、tiff。 $(document).ready(function() { FileDetails = fu
我正在使用 c++ JUCE 库学习 gui 编程。该库支持图像文件格式(png、jpg)。但我想了解如何使用其他文件格式,例如 tiff。 在谷歌之后我得到了 libtiff。 我的问题是显示它的准
我正在使用 urlmon.dll 中的 FindMimeFromData 来嗅探上传文件的 MIME 类型。根据MIME Type Detection in Internet Explorer , i
我发现,通过谷歌,很多人问同样的问题,但没有解决方案。 Python 图像库 (PIL) 具有用于单步执行现有多页 TIFF 的工具,但没有创建它们的工具。 库有望在 Windows 上可用,适用于
我正在尝试使用 iText 7.1.1 将 TIFF 图像转换为具有多页的 PDF 文件。感谢那些让我开始阅读这篇文章的人 Create PDF from TIFF image using iText
在本节中,我将头文件添加到 tiff 文件的顶部。 echo "/CourierLatin1 findfont 8 scalefont setfont" >>${PS} echo "40 2 move
对于 OCR 引擎,我需要为 OCR 引擎提供带有 CCITT4 压缩的 TIFF 文件。我们的扫描仪输出 JPEG 压缩的 TIFF 文件。我想用 C# 转换这些文件,使用 System.Drawi
我有 5 个单页 tiff 图像。我想将所有这 5 张 tiff 图像组合成一张多页 tiff 图像。我正在使用 Java 高级成像 API。看了SUN给的JAI API文档和教程。我是 JAI 的新
我需要一个 C# 函数,它将采用 8 位灰度 TIFF 的 Byte[],并返回 1 位(黑白)TIFF 的 Byte[]。 我对使用 TIFF 还很陌生,但一般的想法是我们需要将它们从灰度或彩色转换
我想在 TIFF 文件中操作 RGB 波段并在 matplotlib 上输出 灰度 贴图。到目前为止我有这段代码,但我无法在灰度上得到它: import scipy as N import gdal
我是一名优秀的程序员,十分优秀!