string ConvertObjectToString(object obj) { if (obj == null) { return(null); } if (!Context.DeserializeResources) { return(obj.ToString()); } return(SerializationUtilities.ConvertObjectToString(obj)); }
/// <summary> /// Serializes the image /// </summary> /// <param name="resElem">Resource element</param> /// <returns></returns> public static ResourceElement Serialize(ResourceElement resElem) { var data = (byte[])((BuiltInResourceData)resElem.ResourceData).Data; bool isIcon = BitConverter.ToUInt32(data, 0) == 0x00010000; object obj; if (isIcon) { obj = new System.Drawing.Icon(new MemoryStream(data)); } else { obj = new System.Drawing.Bitmap(new MemoryStream(data)); } return(new ResourceElement { Name = resElem.Name, ResourceData = new BinaryResourceData(new UserResourceType(obj.GetType().AssemblyQualifiedName, ResourceTypeCode.UserTypes), SerializationUtilities.Serialize(obj)), }); }
/// <summary> /// Serialize an image list /// </summary> /// <param name="opts">Options</param> /// <returns></returns> public static ResourceElement Serialize(ImageListOptions opts) { var imgList = new ImageList(); imgList.ColorDepth = opts.ColorDepth; imgList.ImageSize = opts.ImageSize; imgList.TransparentColor = opts.TransparentColor; foreach (var imageSource in opts.ImageSources) { var bitmapSource = imageSource as BitmapSource; if (bitmapSource is null) { throw new InvalidOperationException("Only BitmapSources can be used"); } var encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); var outStream = new MemoryStream(); encoder.Save(outStream); outStream.Position = 0; var wfBmp = new System.Drawing.Bitmap(outStream); imgList.Images.Add(wfBmp); } var obj = imgList.ImageStream; return(new ResourceElement { Name = opts.Name, ResourceData = new BinaryResourceData(new UserResourceType(obj.GetType().AssemblyQualifiedName, ResourceTypeCode.UserTypes), SerializationUtilities.Serialize(obj)), }); }