title: Converting byte array to png
body: <p>I have a byte array obtained from an image using the following code.</p>
  
  <pre><code>String path = "/home/mypc/Desktop/Steganography/image.png";
  File file = new File(path);
  BufferedImage bfimage = ImageIO.read(file);
  
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ImageIO.write(bfimage, "png", baos);
  baos.flush();
  byte[] img_in_bytes = baos.toByteArray();
  baos.close();
  </code></pre>
  
  <p>Then I converted these bytes back to png image using the following code.</p>
  
  <pre><code>BufferedImage final_img = ImageIO.read(new ByteArrayInputStream(img_in_bytes));
  File output_file = new File("Stegano2.png");
  ImageIO.write(final_img, "png", output_file);
  </code></pre>
  
  <p>It is perfectly fine if i just execute this piece of code. But if i try to modify some of the bytes in between, say like this :</p>
  
  <pre><code>    Insert_number_to_image(image_in_bytes,  10);
  </code></pre>
  
  <p>and my method "Inset_number_to_image" goes like this :</p>
  
  <pre><code>static void Insert_number_to_image(byte[] image, int size){     
      byte[] size_in_byte = new byte[4];
      size_in_byte[0] = (byte)(size &gt;&gt;&gt; 0);
      size_in_byte[1] = (byte)(size &gt;&gt;&gt; 8);
      size_in_byte[2] = (byte)(size &gt;&gt;&gt; 16);
      size_in_byte[3] = (byte)(size &gt;&gt;&gt; 24);
  
      byte temp;
      int count = 0;
  
      for(int i=0; i&lt;4; i++)
      {
          for(int j=0; j&lt;8; j++)
          {
              temp = size_in_byte[i];
              temp = (byte)(temp &gt;&gt;&gt; j);
              temp = (byte)(temp &amp; 1);
              if(temp == 1)
              {
                  image[count] = (byte)(image[count] | 1);
              }
              else if(temp == 0)
              {
                  image[count] = (byte)(image[count] &amp; (byte)(~(1)));
              }
              count++;
          }
      }
  }
  </code></pre>
  
  <p>then after that, when i save the modified byte array as png image using same code mentioned above, i am getting this error :</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: image == null!
      at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
      at javax.imageio.ImageIO.getWriter(ImageIO.java:1591)
      at javax.imageio.ImageIO.write(ImageIO.java:1520)
      at Steganography.main(Steganography.java:211)
  </code></pre>