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