locked
How to convert a Color tiff file to Black and white tiff file using vb/C# code? RRS feed

  • Question

  • User-1167953582 posted

     

    Hi ,

       I have a lot of TIFF file in Color format. Now I want to convert into Black and White TIFF file because color is taking lot of memory.

     one color file is 5 MB.

    How to convert a Color tiff file to Black and white tiff file using vb.net code?

     Please any one reply.

    Sunday, February 22, 2009 3:19 AM

Answers

  • User2113775107 posted
    Here is the code you need !
    use the ConvertToBitonal function to conver your color tiff file to black & white one.
     
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;
    using System.Text;
    
    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);
                Graphics g = Graphics.FromImage(newImage);
                g.DrawImageUnscaled(original,  0, 0);
                g.Dispose();
                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.Format32bppArgb);
                //Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
    
                // Lock destination bitmap in memory
                //BitmapData destinationData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);
                BitmapData destinationData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    
                // 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)
                        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;
                    }
                }
    
                // 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;
            }
        }
    }
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 22, 2009 7:34 AM
  • User1817007508 posted

    Hi e_kreda,

     Thanks for your wonderful code. I am new to .Net. Can you please tell me Where to go

    I am using MS Visual Studio 2005.

    File --->New--> Project or File---> New ----> Website or File --> New ---> File.

     Where to go .ie.,Window application or console application or web form or web user

    Please give some ideas.

    Thanks

    Hi,

    This article may help

    Beginning Programming with the Visual Studio 2005 Environment

    http://blog.csdn.net/zhujianyun/archive/2006/07/02/866057.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 23, 2009 12:18 AM

All replies

  • User2113775107 posted
    Here is the code you need !
    use the ConvertToBitonal function to conver your color tiff file to black & white one.
     
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;
    using System.Text;
    
    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);
                Graphics g = Graphics.FromImage(newImage);
                g.DrawImageUnscaled(original,  0, 0);
                g.Dispose();
                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.Format32bppArgb);
                //Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
    
                // Lock destination bitmap in memory
                //BitmapData destinationData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);
                BitmapData destinationData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    
                // 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)
                        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;
                    }
                }
    
                // 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;
            }
        }
    }
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 22, 2009 7:34 AM
  • User-1167953582 posted

    Hi e_kreda,

     Thanks for your wonderful code. I am new to .Net. Can you please tell me Where to go

    I am using MS Visual Studio 2005.

    File --->New--> Project or File---> New ----> Website or File --> New ---> File.

     Where to go .ie.,Window application or console application or web form or web user

     

    Please give some ideas.

     

     

    Thanks

    Sunday, February 22, 2009 11:48 PM
  • User1817007508 posted

    Hi e_kreda,

     Thanks for your wonderful code. I am new to .Net. Can you please tell me Where to go

    I am using MS Visual Studio 2005.

    File --->New--> Project or File---> New ----> Website or File --> New ---> File.

     Where to go .ie.,Window application or console application or web form or web user

    Please give some ideas.

    Thanks

    Hi,

    This article may help

    Beginning Programming with the Visual Studio 2005 Environment

    http://blog.csdn.net/zhujianyun/archive/2006/07/02/866057.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 23, 2009 12:18 AM