-1

Hi everybody I want to convert all images to grayscales in my directory but code that I know only do that task one by one!!But I have a lot of images and I want If there is a code that convert all images in directory to grayscale or

Johana
  • 61
  • 1
  • 9
  • 1
    possible duplicate of [C#, convert image to grayscale](http://stackoverflow.com/questions/2265910/c-convert-image-to-grayscale) – Rivasa Feb 20 '14 at 16:47
  • This is a question and answer site to help with development questions. You could have googled this questions and found a lot of answers on it. Consider showing us some code you tried and we'll be happy to help you. – The Muffin Man Feb 20 '14 at 16:51
  • Search for "batch convert images" or "bulk convert images". There are many different programs that will do that for you. – Jim Mischel Feb 20 '14 at 17:24

1 Answers1

1

Assuming you have a variable called path which is the path of the folder with the images to process, and assuming you can use WPF APIs to convert the image:

DirectoryInfo dirInfo = new DirectoryInfo(path);

foreach ( FileInfo fileInfo in dirInfo.EnumerateFile() ) {
    ProcessImage(FileInfo.FullName);
}

void ProcessImage( string image ) {
    byte[] imageData = null;

    try {
        // Load the data in the file into a  byte array for processing.
        imageData = File.ReadAllBytes( image );

    } catch ( Exception) {
        // Your error handling code here
    }

    // We're going to put the image into this object
    BitmapImage src = new BitmapImage();

    try {
        // Load the image into a memory stream, then into src.
        using( MemoryStream stream = new MemoryStream( imageData ) ) {
            src.BeginInit();
            src.CacheOption = BitmapCacheOption.OnLoad; // Causes the bitmap data to be loaded now, not at a later time.
            src.StreamSource = stream;
            src.EndInit();
        }
    } catch ( NotSupportedException ) {
        // The bitmap format is not supported.  Your error handler here.
    }

    try {
        // Create a FormatConvertedBitmap object & set it up.
        FormatConvertedBitmap dst = new FormatConvertedBitmap();
        dst.BeginInit();
        dst.DestinationFormat = PixelFormats.Gray8;  // Use whatever gray scale format you need here
        dst.Source            = src;

        // Now convert the image to 8 bits per pixel grey scale.
        dst.EndInit();

        // Compute the dst Bitmap's stride (the length of one row of pixels in bytes).
        int stride = ( ( dst.PixelWidth * dst.Format.BitsPerPixel + 31 ) / 32 ) * 4;

        // Allocate space for the imageBytes array.
        imageBytes = new byte[ stride * dst.PixelHeight ];

        // Get the pixels out of the dst image and put them into imageBytes.
        dst.CopyPixels( Int32Rect.Empty, imageBytes, stride, 0 );

    } catch ( Exception ex ) {
        // Your error handler here.
    }

    // Any other code to save or do something else with the image.
}
Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123
  • Yes I wanted this part of code DirectoryInfo dirInfo = new DirectoryInfo(path); foreach ( FileInfo fileInfo in dirInfo.EnumerateFile() ) { ProcessImage(FileInfo.FullName); } But I want to use opencv to convert into grayscale so Do I read all byte to convert use Image into graysvale? – Johana Feb 20 '14 at 18:11
  • @Johana I'm afraid I have no experience with opencv, sorry. – Tony Vitabile Feb 21 '14 at 13:52
  • I doesn´t care!!Your info help me a lot!!Dont worry!! – Johana Feb 21 '14 at 14:13