Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在我的Tiff文件上应用CCITT压缩后,它会生成一个带有纯黑背景的图像_C#_Asp.net_Tiff - Fatal编程技术网

C# 在我的Tiff文件上应用CCITT压缩后,它会生成一个带有纯黑背景的图像

C# 在我的Tiff文件上应用CCITT压缩后,它会生成一个带有纯黑背景的图像,c#,asp.net,tiff,C#,Asp.net,Tiff,使用LZW压缩创建tiff文件(默认值):- 因此,我尝试将其压缩更改为CCITT:- Bitmap myBitmap; myBitmap = new Bitmap(fileName); ImageCodecInfo myImageCodecInfo; myImageCodecInfo = GetEncoderInfo("image/tiff"); System.Drawing.Imaging.Encoder myEncoder; myEncoder = System.Drawing.Imagi

使用LZW压缩创建tiff文件(默认值):-

因此,我尝试将其压缩更改为CCITT:-

Bitmap myBitmap;
myBitmap = new Bitmap(fileName);
ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/tiff");
System.Drawing.Imaging.Encoder myEncoder;
myEncoder = System.Drawing.Imaging.Encoder.Compression;
EncoderParameters myEncoderParameters;
myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter;      
myEncoderParameter = new EncoderParameter(myEncoder,(long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save(new_fileName, myImageCodecInfo, myEncoderParameters);

private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
  int j;
  ImageCodecInfo[] encoders;
  encoders = ImageCodecInfo.GetImageEncoders();
  for (j = 0; j < encoders.Length; ++j)
    {
        if (encoders[j].MimeType == mimeType)
        return encoders[j];
    }
  return null;
}
Bitmap-myBitmap;
myBitmap=新位图(文件名);
ImageCodecInfo myImageCodecInfo;
myImageCodecInfo=GetEncoderInfo(“图像/tiff”);
System.Drawing.Imaging.Encoder编码器;
myEncoder=System.Drawing.Imaging.Encoder.Compression;
编码器参数myEncoderParameters;
myEncoderParameters=新编码器参数(1);
编码器参数myEncoderParameter;
myEncoderParameter=新编码器参数(myEncoder,(long)EncoderValue.compressionCCIT4);
myEncoderParameters.Param[0]=myEncoderParameter;
保存(新文件名、MyImageCodeInfo、myEncoderParameters);
私有静态ImageCodeInfo GetEncoderInfo(字符串mimeType)
{
int j;
ImageCodeInfo[]编码器;
编码器=ImageCodecInfo.GetImageEncoders();
对于(j=0;j
该过程正常,我可以看到我的tiff文件的压缩类型已正确修改为CCITT(通过右键单击输出文件并检查属性)。 然而,当我试图打开它时,整个图像都是黑色的。。上面什么也没有

当我将压缩类型更改为LZW或None或RLE时,tiff文件再次成为一个可视图像

不幸的是,我只需要CCITT格式。有人能帮我弄清楚吗

更新:

尽管该方法现在可以在使用Visual studio 2010的windows 7 32位操作系统上使用。。一旦发布到Windows 2008服务器,该解决方案就不起作用(仅适用于CCITT压缩技术)。下面的链接解释了原因

根据本文,这是我正在尝试的,但无法将压缩应用于原始图像:-

        int width = 800;
        int height = 1000;
        int stride = width/8;
        byte[] pixels = new byte[height*stride];

        // Try creating a new image with a custom palette.
        List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
        colors.Add(System.Windows.Media.Colors.Red);
        colors.Add(System.Windows.Media.Colors.Blue);
        colors.Add(System.Windows.Media.Colors.Green);
        BitmapPalette myPalette = new BitmapPalette(colors);

        // Creates a new empty image with the pre-defined palette

        BitmapSource image = BitmapSource.Create(
            width,
            height,
            96,
            96,
            System.Windows.Media.PixelFormats.BlackWhite,
            myPalette, 
            pixels, 
            stride);


FileStream stream = new FileStream(Original_File, FileMode.Create);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Compression = TiffCompressOption.Ccitt4;
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
int-width=800;
整数高度=1000;
int步长=宽度/8;
字节[]像素=新字节[高度*步幅];
//尝试使用自定义调色板创建新图像。
列表颜色=新列表();
添加(System.Windows.Media.colors.Red);
添加(System.Windows.Media.colors.Blue);
添加(System.Windows.Media.colors.Green);
BitmapPalette myPalette=新的BitmapPalette(颜色);
//使用预定义的调色板创建新的空图像
BitmapSource图像=BitmapSource.Create(
宽度,
高度,
96,
96,
System.Windows.Media.PixelFormats.BlackWhite,
我的调色板,
像素,
步幅);
FileStream stream=新FileStream(原始文件,FileMode.Create);
TiffBitmapEncoder编码器=新的TiffBitmapEncoder();
encoder.Compression=TiffCompressOption.Ccitt4;
encoder.Frames.Add(BitmapFrame.Create(image));
编码器。保存(流);

然而,我的最终结果是一个具有正确压缩的文件,但是有一个坚实的黑色背景。似曾相识。

按此编辑代码,您将能够使用所需的压缩,而无需黑色背景填充:

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); 
Graphics g = Graphics.FromImage(bitmap);
/* Add this */ g.FillRectangle(Brushes.White, new Rectangle(0,0, 800, 1000));
g.DrawString("Name: " + Name.Text, outputFont, Brushes.Black, new PointF(0, 20));
g.DrawString("Date Of Birth: " + Date_Of_Birth.Text, outputFont, Brushes.Black, new PointF(0, 40));
g.DrawString("Address: " + Address.Text, outputFont, Brushes.Black, new PointF(0, 60));
g.DrawString("City: " + City.Text, outputFont, Brushes.Black, new PointF(0, 80));
g.DrawString("State: " + State.Text, outputFont, Brushes.Black, new PointF(0, 100));
g.DrawString("Zip Code: " + Zip_Code.Text, outputFont, Brushes.Black, new PointF(0, 120));
g.DrawString("Phone: " + Phone.Text, outputFont, Brushes.Black, new PointF(0, 140));
g.DrawString(" ID: " + ID.Text, outputFont, Brushes.Black, new PointF(0, 160));
fileName = saveDirectory + id + ".tif";
bitmap.Save(fileName, ImageFormat.Tiff);

我相信CCITT仅用于双层图像,不是吗?所以像素不是纯黑色就是纯空。有关更多信息,请参阅。你的图像内容是什么样的?我已经展示了我的图像内容的一些示例代码(在代码的创建部分)。基本上,用户在web表单上的输入被翻译成文本(黑色文本颜色,白色背景)到tiff文件上。是的,CCITT仅用于双层成像。在我的文件中没有颜色,也没有任何三维奇特的图形。只是一个tiff文件上的纯文本。只是在这里抛开一些东西来帮助你,但是你的tiff并不是多页或分层的,是吗?没有任何类型的透明度?事实上,看起来你没有明确设置任何背景填充。您是否确定它将默认为白色且不透明?如何更改输出tiff文件的字体?字体看起来有点浑浊。除非API内置了一个diterhing算法(老实说,这可能不会让它最终看起来更好),否则最好的选择是从一个更像素化的字体开始并使用它。平滑的字体笔划是通过使用字体指定颜色的较浅阴影来实现的,这显然在双层图像中不起作用,因为所有非白色的东西都变成纯黑色。很好!!我用的是“技术”tahoma…有什么建议吗?有一些选择,但要小心链接。Proggy Square的链接充其量是垃圾邮件,充其量是恶意的。这是Proggy的安全链接。实际上是一种相当好看的字体。
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); 
Graphics g = Graphics.FromImage(bitmap);
/* Add this */ g.FillRectangle(Brushes.White, new Rectangle(0,0, 800, 1000));
g.DrawString("Name: " + Name.Text, outputFont, Brushes.Black, new PointF(0, 20));
g.DrawString("Date Of Birth: " + Date_Of_Birth.Text, outputFont, Brushes.Black, new PointF(0, 40));
g.DrawString("Address: " + Address.Text, outputFont, Brushes.Black, new PointF(0, 60));
g.DrawString("City: " + City.Text, outputFont, Brushes.Black, new PointF(0, 80));
g.DrawString("State: " + State.Text, outputFont, Brushes.Black, new PointF(0, 100));
g.DrawString("Zip Code: " + Zip_Code.Text, outputFont, Brushes.Black, new PointF(0, 120));
g.DrawString("Phone: " + Phone.Text, outputFont, Brushes.Black, new PointF(0, 140));
g.DrawString(" ID: " + ID.Text, outputFont, Brushes.Black, new PointF(0, 160));
fileName = saveDirectory + id + ".tif";
bitmap.Save(fileName, ImageFormat.Tiff);