locked
how to stream bitmap ?(C#) RRS feed

  • Question

  • how to stream bitmap ?(C#)
    Sunday, September 14, 2014 7:02 AM

Answers

  • var bitmap = new Bitmap(@"c:\Documente und Einstellungen\daniel.hilgarth\Desktop\Unbenannt.bmp");
    
    ImageCodecInfo jpgEncoder = ImageCodecInfo.GetImageEncoders().Single(x => x.FormatDescription == "JPEG");
    Encoder encoder2 = System.Drawing.Imaging.Encoder.Quality;
    EncoderParameters parameters = new System.Drawing.Imaging.EncoderParameters( 1 );
    EncoderParameter parameter = new EncoderParameter( encoder2, 50L );
    parameters.Param[0] = parameter;
    
    System.IO.Stream stream = new MemoryStream();
    bitmap.Save( stream, jpgEncoder, parameters );
    bitmap.Save(@"C:\Temp\TestJPEG.jpg", jpgEncoder, parameters);
    
    var bytes = ((MemoryStream)stream).ToArray();
    System.IO.Stream inputStream = new MemoryStream(bytes);
    Bitmap fromDisk = new Bitmap(@"C:\Temp\TestJPEG.jpg");
    Bitmap fromStream = new Bitmap(inputStream);

    • Marked as answer by Kristin Xie Thursday, September 25, 2014 9:47 AM
    Sunday, September 14, 2014 7:34 AM
  • Hi move ellipse,

    I think the following is what you want. Please take a look

     //byte[] to image 
            public static Bitmap BytesToBitmap(byte[] Bytes)
            {
                MemoryStream stream = null;
                try
                {
                    stream = new MemoryStream(Bytes);
                    return new Bitmap((Image)new Bitmap(stream));
                }
                catch (ArgumentNullException ex)
                {
                    throw ex;
                }
                catch (ArgumentException ex)
                {
                    throw ex;
                }
                finally
                {
                    stream.Close();
                }
            }
    
            //Image to byte[]   
            public static byte[] BitmapToBytes(Bitmap Bitmap)
            {
                MemoryStream ms = null;
                try
                {
                    ms = new MemoryStream();
                    Bitmap.Save(ms, Bitmap.RawFormat);
                    byte[] byteImage = new Byte[ms.Length];
                    byteImage = ms.ToArray();
                    return byteImage;
                }
                catch (ArgumentNullException ex)
                {
                    throw ex;
                }
                finally
                {
                    ms.Close();
                }
            }

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Kristin Xie Thursday, September 25, 2014 9:47 AM
    Monday, September 15, 2014 9:39 AM

All replies

  • var bitmap = new Bitmap(@"c:\Documente und Einstellungen\daniel.hilgarth\Desktop\Unbenannt.bmp");
    
    ImageCodecInfo jpgEncoder = ImageCodecInfo.GetImageEncoders().Single(x => x.FormatDescription == "JPEG");
    Encoder encoder2 = System.Drawing.Imaging.Encoder.Quality;
    EncoderParameters parameters = new System.Drawing.Imaging.EncoderParameters( 1 );
    EncoderParameter parameter = new EncoderParameter( encoder2, 50L );
    parameters.Param[0] = parameter;
    
    System.IO.Stream stream = new MemoryStream();
    bitmap.Save( stream, jpgEncoder, parameters );
    bitmap.Save(@"C:\Temp\TestJPEG.jpg", jpgEncoder, parameters);
    
    var bytes = ((MemoryStream)stream).ToArray();
    System.IO.Stream inputStream = new MemoryStream(bytes);
    Bitmap fromDisk = new Bitmap(@"C:\Temp\TestJPEG.jpg");
    Bitmap fromStream = new Bitmap(inputStream);

    • Marked as answer by Kristin Xie Thursday, September 25, 2014 9:47 AM
    Sunday, September 14, 2014 7:34 AM
  • Hi move ellipse,

    I think the following is what you want. Please take a look

     //byte[] to image 
            public static Bitmap BytesToBitmap(byte[] Bytes)
            {
                MemoryStream stream = null;
                try
                {
                    stream = new MemoryStream(Bytes);
                    return new Bitmap((Image)new Bitmap(stream));
                }
                catch (ArgumentNullException ex)
                {
                    throw ex;
                }
                catch (ArgumentException ex)
                {
                    throw ex;
                }
                finally
                {
                    stream.Close();
                }
            }
    
            //Image to byte[]   
            public static byte[] BitmapToBytes(Bitmap Bitmap)
            {
                MemoryStream ms = null;
                try
                {
                    ms = new MemoryStream();
                    Bitmap.Save(ms, Bitmap.RawFormat);
                    byte[] byteImage = new Byte[ms.Length];
                    byteImage = ms.ToArray();
                    return byteImage;
                }
                catch (ArgumentNullException ex)
                {
                    throw ex;
                }
                finally
                {
                    ms.Close();
                }
            }

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Kristin Xie Thursday, September 25, 2014 9:47 AM
    Monday, September 15, 2014 9:39 AM