Exemplo n.º 1
0
        protected override void SetAttributes(System.Xml.Linq.XElement ele, object propertyValue)
        {
            if (propertyValue == null)
            {
                return;
            }
            Bitmap bitmap = propertyValue as Bitmap;

            if (bitmap == null)
            {
                return;
            }
            BitmapData pdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);

            try
            {
                byte[]   buffer = new byte[bitmap.Height * pdata.Stride];
                GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                try
                {
                    MemoryCopy(handle.AddrOfPinnedObject(), pdata.Scan0, buffer.Length);
                    ele.SetAttributeValue("pixelformat", bitmap.PixelFormat);
                    ele.SetAttributeValue("width", bitmap.Width);
                    ele.SetAttributeValue("height", bitmap.Height);
                    ele.SetAttributeValue("stride", pdata.Stride);
                    ele.SetValue(Convert.ToBase64String(buffer));
                }
                finally
                {
                    handle.Free();
                }
            }
            finally
            {
                bitmap.UnlockBits(pdata);
            }
        }