Conversion between byte [], image, and bitmap

Source: Internet
Author: User

/// <Summary>
  /// Convert the image to byte []
  /// </Summary>
  /// <Param name = "image"> image object </param>
  /// <Param name = "imageformat"> suffix </param>
  /// <Returns> </returns>
  Public static byte [] imagetobytes (image, system. Drawing. imaging. imageformat)
  {

 If (image = NULL) {return NULL ;}

 Byte [] DATA = NULL;

 Using (memorystream MS = new memorystream ())
 {

 Using (Bitmap bitmap = new Bitmap (image ))
 {

Bitmap. Save (MS, imageformat );

 Ms. Position = 0;

Data = new byte [Ms. Length];

 Ms. Read (data, 0, convert. toint32 (Ms. Length ));

 Ms. Flush ();

 }

 }

 Return data;

 }

 

 

/// <Summary>
  /// Byte [] to convert to image
  /// </Summary>
  /// <Param name = "bytearrayin"> binary image stream </param>
  /// <Returns> image </returns>
  Public static system. Drawing. Image bytearraytoimage (byte [] bytearrayin)
  {
  If (bytearrayin = NULL)
  Return NULL;
  Using (system. Io. memorystream MS = new system. Io. memorystream (bytearrayin ))
  {
  System. Drawing. Image returnimage = system. Drawing. image. fromstream (MS );
  Ms. Flush ();
  Return returnimage;
  }
  }

 

// Convert an image to a bitmap

1. Bitmap IMG = new Bitmap (imgselect. Image );

2. bitmap BMP = (Bitmap) picturebox1.image;

 

// Convert bitmap to image

Using system. IO;

Private Static system. Windows. Controls. Image bitmap2image (system. Drawing. Bitmap Bi)
  {
  Memorystream MS = new memorystream ();
  Bi. Save (MS, system. Drawing. imaging. imageformat. PNG );
  Bitmapimage bimage = new bitmapimage ();
  Bimage. begininit ();
  Bimage. streamsource = new memorystream (Ms. toarray ());
  Bimage. endinit ();
  Ms. Dispose ();
  Bi. Dispose ();
  System. Windows. Controls. image I = new system. Windows. Controls. Image ();
  I. Source = bimage;
  Return I;
  }

 

// Byte [] converts bitmap
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 ();
  }
  }

// Convert bitmap 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 ();
  }
  }
  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.