Bitmap with nullexception argument save to memystream

I am trying to save my Bitmap to a MemoryStream - what is wrong with this code? Why does this give me reason?

private void insertBarCodesToPDF(Bitmap barcode)
    {

            .......
            MemoryStream ms = new MemoryStream();
            barcode.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp); //<----
            byte [] qwe = ms.ToArray();
            .......

    }

      

UPD: StackTrace System.Drawing.Image.Save (Stream Stream, ImageCodecInfo Encoder, EncoderParameters encoderParams) to WordTest.FormTestWord.insertBarCodesToPDF (Bitmap Barcode)

+3


source to share


1 answer


I believe your problem is related to the type of image you are trying to store in the MemoryStream. According to this code project article: Dynamically generating icons (safe) , some ImageFormat types do not have the required encoder for the save function to be saved as that type.

I defined the following types and didn't work:

System.Drawing.Bitmap b = new Bitmap(10, 10);
foreach (ImageFormat format in new ImageFormat[]{
          ImageFormat.Bmp, 
          ImageFormat.Emf, 
          ImageFormat.Exif, 
          ImageFormat.Gif, 
          ImageFormat.Icon, 
          ImageFormat.Jpeg, 
          ImageFormat.MemoryBmp,
          ImageFormat.Png,
          ImageFormat.Tiff, 
          ImageFormat.Wmf}) 
{
  Console.Write("Trying {0}:", format);
  MemoryStream ms = new MemoryStream();
  bool success = true;
  try 
  {
    b.Save(ms, format);
  }
  catch (Exception) 
  {
    success = false;
  }
  Console.WriteLine("\t{0}", (success ? "works" : "fails"));
}

      



This gave results:

Trying Bmp:       works
Trying Emf:       fails
Trying Exif:      fails
Trying Gif:       works
Trying Icon:      fails
Trying Jpeg:      works
Trying MemoryBMP: fails
Trying Png:       works
Trying Tiff:      works
Trying Wmf:       fails

      

There is a Microsoft KB article that says that some ImageFormat types are read-only.

+8


source







All Articles