After applying CCITT compression on my Tiff File, it produces an image with a solid black background

StackOverflow https://stackoverflow.com/questions/21192691

  •  29-09-2022
  •  | 
  •  

Question

Creating tiff file, with LZW compression (default):-

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); 
Graphics g = Graphics.FromImage(bitmap);
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);

So I am trying to change its compression to 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;
}

The process works and I can see that the compression type of my tiff file has correctly been modified to CCITT (by right clicking on the output file and checking properties). However when I try to open it, the whole image in black.. nothing is on it.

When I change the compression type to LZW or None or RLE then the tiff file is once again a viewable image.

Unfortunately I need the CCITT format only. Can anyone help me figure this out?

Update:

Even though the method now works on my windows 7 32 bit OS using Visual studio 2010.. Once published to the Windows 2008 server, the solution doesn't work (only for CCITT compression techniques). The following link explains why.

http://social.msdn.microsoft.com/Forums/vstudio/en-US/1585c562-f7a9-4cfd-9674-6855ffaa8653/parameter-is-not-valid-for-compressionccitt4-on-windows-server-2003-and-2008?forum=netfxbcl

As per this article, this is what I am trying but can't get the compression to apply to the original image:-

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

However, my end result is a file with the correct compression, but a solid black background. Deja Vu.

Was it helpful?

Solution

Edit your code as such, and you will be able to use your desired compression without the black background fill:

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top