Es común en el desarrollo de Web Services utilizar el concepto denominado serialización, el cual nos permite intercambiar objetos entre sistemas heterogéneos. Ver listado de código.
using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Xml; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; namespace F.WebServices.Utils { /// <summary> /// /// </summary> public sealed class Serialization { #region String convertion methods /// <summary> /// /// </summary> /// <param name="str"></param> /// <returns></returns> public static byte[] StringToBytes(string str) { return StringToBytes(str, ASCIIEncoding.ASCII ); } /// <summary> /// /// </summary> /// <param name="str"></param> /// <param name="Enc"></param> /// <returns></returns> public static byte[] StringToBytes(string str, System.Text.Encoding Enc) { return Enc.GetBytes(str); } /// <summary> /// /// </summary> /// <param name="bytes"></param> /// <param name="Enc"></param> /// <returns></returns> public static string BytesToString(byte[] bytes, System.Text.Encoding Enc) { return Enc.GetString(bytes); } /// <summary> /// /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static string BytesToString(byte[] bytes) { return BytesToString(bytes, ASCIIEncoding.ASCII); } #endregion #region Bitmap convertion methods /// <summary> /// /// </summary> /// <param name="img"></param> /// <returns></returns> public static byte[] BitmapToBytes(Bitmap img) { return BitmapToBytes(img, System.Drawing.Imaging.ImageFormat.Bmp ); } /// <summary> /// /// </summary> /// <param name="img"></param> /// <param name="fmt"></param> /// <returns></returns> public static byte[] BitmapToBytes(Bitmap bmp, System.Drawing.Imaging.ImageFormat fmt) { if (bmp!=null) { BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb ); // number of bytes in the bitmap int byteCount = bData.Stride * bmp.Height; byte[] bmpBytes = new byte[byteCount]; // Copy the locked bytes from memory Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount); // don't forget to unlock the bitmap!! bmp.UnlockBits(bData); return bmpBytes; } return null; } /// <summary> /// /// </summary> /// <param name="bytes"></param> /// <param name="colorcorrection"></param> /// <returns></returns> public static Bitmap BytesToBitmap(byte[] bytes, Size imageSize) { if (bytes != null) { Bitmap bmp = new Bitmap(imageSize.Width, imageSize.Height); BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb ); // Copy the bytes to the bitmap object Marshal.Copy(bytes, 0, bData.Scan0, bytes.Length); bmp.UnlockBits(bData); return bmp; } return null; } #endregion #region DataSet convertion methods /// <summary> /// /// </summary> /// <param name="ds"></param> /// <returns></returns> public static byte[] DataSetToByteArray(DataSet ds) { return DataSetToByteArray(ds, ASCIIEncoding.ASCII); } /// <summary> /// /// </summary> /// <param name="ds"></param> /// <param name="Enc"></param> /// <returns></returns> public static byte[] DataSetToByteArray(DataSet ds, System.Text.Encoding Enc) { if (ds != null) { XmlDataDocument xdoc = new XmlDataDocument(ds); return Enc.GetBytes(xdoc.InnerXml); } return null; } /// <summary> /// /// </summary> /// <param name="array"></param> /// <returns></returns> public static DataSet ByteArrayToDataSet(byte[] array) { return ByteArrayToDataSet(array, ASCIIEncoding.ASCII); } /// <summary> /// /// </summary> /// <param name="array"></param> /// <param name="Enc"></param> /// <returns></returns> public static DataSet ByteArrayToDataSet(byte[] array, System.Text.Encoding Enc) { if (array != null) { if (array.Length > 0) { DataSet ds=new DataSet (); ds.ReadXml(new System.IO.StringReader(Enc.GetString (array))); return ds; } } return null; } #endregion #region Object convertion methods /// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> public static byte[] ObjectToBytes(object obj) { return ObjectToBytes(obj, ASCIIEncoding.ASCII ); } /// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="Enc"></param> /// <returns></returns> public static byte[] ObjectToBytes<T>(T obj, System.Text.Encoding Enc) { return Enc.GetBytes (F.Xml.Serializer.Serialize<T>(obj)); } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="bytes"></param> /// <returns></returns> public static T BytesToObject<T>(byte[] bytes) { return BytesToObject<T>(bytes, ASCIIEncoding.ASCII); } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="bytes"></param> /// <param name="Enc"></param> /// <returns></returns> public static T BytesToObject<T>(byte[] bytes, System.Text.Encoding Enc) { return F.Xml.Serializer.Deserialize<T>(Enc.GetString(bytes)); } #endregion } }