0

I'm trying to combine multiple images into one in c# using Bitmap ,my image width is 5000 and height is 1800 (which is constant) as per my code I'm able to combine upto 12 images (the total width will be 60000 and 1800 height).I have total 68 images to combine.I'm getting "Generic Error" when saving the image(error occurs when trying to combine more than 12 images). Final output should be combining the 68 images with (350000 width* 1800 height).

I have tried using Bitmap,Is there a way to use WIC(Windows imaging component) to combine multiple images.

       class Program
         {

        static void Main(string[] args)
           {
            int width = 0;
            int height = 0;
            List<Image> images = new List<Image>();
            var folderPath = "C:\\home\\imagepath";//contains all the images
            var file = Directory.GetFiles(folderPath, "*.png");
            String jpg3 = @"finalimage.png";
            var path = "C:\\home\\data\\FinalImage";
            foreach (var item in file)
            {
                Image image1 = Image.FromFile(item);
                images.Add(image1);
                width += image1.Width;  // adding all the images width
                height = image1.Height;
            }

       Bitmap finalImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            finalImage.MakeTransparent();
            Graphics g = Graphics.FromImage(finalImage);

            // g.Clear(SystemColors.AppWorkspace);        
            g.Clear(Color.White);
            var nextwidth = 0;
            foreach (var img in images)
            {
                g.DrawImage(img, new Point(nextwidth, 0));
                nextwidth += img.Width;

            }
            g.Dispose();
            foreach (var img in images)
            {
                img.Dispose();
            }
            finalImage.Save(Path.Combine(path, jpg3), ImageFormat.Png);// getting error when combining more than 12 images and saving in particular folder
            finalImage.Dispose();
        }
       }

"Error :System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.'"

Getting this error on finalImage.save(path.combine(path,jpg3),ImageFormat.Png);

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
anup.p
  • 39
  • 1
  • 4
  • 4
    That image will be getting close to 2GB in size (assuming 3 bytes/pixel), so it's probably just too big. You may need to use the [Windows Imaging Component](https://learn.microsoft.com/en-us/windows/win32/wic/-wic-about-windows-imaging-codec) to do this (via `System.Windows.Media.Imaging`) – Matthew Watson Aug 06 '19 at 08:15
  • can you share me an sample link for the same. – anup.p Aug 06 '19 at 10:27
  • I'm afraid I don't have a link to a sample application to combine images using WIC – Matthew Watson Aug 06 '19 at 11:57
  • Are you running as a 32bit process? This will never work (image > 2Gb). Try 64 bits. Otherwise try WPF: this `new WriteableBitmap(350000, 1800, 96, 96, PixelFormats.Bgra32, null);` works running as 64 bits. – Simon Mourier Aug 06 '19 at 17:01
  • @SimonMourier I'm running it in 64bit process ,even after using WriteableBitmap I'm getting the error ''The image dimensions are out of the range supported by this codec' when trying to save the image – anup.p Aug 09 '19 at 08:49
  • What codec (=> file type) are you using? This particularly codec seems not to support this file size – Simon Mourier Aug 09 '19 at 09:45

1 Answers1

-1

Original answer: This error can appear if the save path you provided is invalid or you don't have write permissions in that directory

EDIT1: I think you are using up nearly all your memory for your bitmap image and this is what is causing the error

Ionut Radu
  • 29
  • 1
  • 6