Reference using system. Windows. Media. imaging;
Using system;
Using system. net;
Using system. IO;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. Ink;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. imaging;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Namespace commons
{
/// <Summary>
/// Type conversion set
/// </Summary>
Public class typeconverter
{
/// <Summary>
/// Convert the image to binary
/// </Summary>
/// <Param name = "sourceimage"> </param>
/// <Returns> </returns>
Public static byte [] imagetobytearray (image sourceimage)
{
Bitmapsource BS = sourceimage. source as bitmapsource;
Using (memorystream MS = new memorystream ())
{
System. Windows. Media. imaging. writeablebitmap = new writeablebitmap (BS );
System. Windows. Media. imaging. Extensions. savejpeg (writeablebitmap, MS, BS. pixelwidth, BS. pixelheight, 0,100 );
Return Ms. getbuffer ();
}
}
/// <Summary>
/// Convert a binary image to an image
/// </Summary>
/// <Param name = "bits"> </param>
/// <Returns> </returns>
Public static image bytearraytoimage (byte [] bits)
{
Bitmapimage = new bitmapimage ();
Using (memorystream MS = new memorystream (BITs ))
{
Bitmapimage. createoptions = bitmapcreateoptions. delaycreation;
Bitmapimage. setsource (MS );
Image image = new image ();
Image. Source = bitmapimage;
Return image;
}
}
}
}