예제 #1
0
 async void SetBackgroundImageAsync(string imageId, Panel control, PositionLength horizontal, PositionLength vertical)
 {
     byte[] imgBytes = null;
     if (imageId.StartsWith("#"))
     {
         var img = CaptionRegion.TunneledData[imageId.Substring(1)];
         if (img != null && img.Encoding == "Base64")
         {
             imgBytes = System.Convert.FromBase64String(img.Data);
         }
     }
     else
     {
         using (var httpClient = new HttpClient())
         {
             imgBytes = await httpClient.GetByteArrayAsync(imageId);
         }
     }
     if (imgBytes != null)
     {
         await SetBackgroundImageBytesAsync(imgBytes, control, horizontal, vertical);
     }
 }
 static async Task SetBackgroundImageBytesAsync(byte[] imageBytes, Panel control, PositionLength horizontal, PositionLength vertical)
 {
     var bmpImage = await ByteArrayToImageAsync(imageBytes);
     var imageBrush = new ImageBrush() { ImageSource = bmpImage, Stretch = Stretch.None };
     if (horizontal != null)
     {
         switch (horizontal.Unit)
         {
             case PositionLengthUnit.CenterAlign:
                 imageBrush.AlignmentX = AlignmentX.Center;
                 break;
             case PositionLengthUnit.NearAlign:
                 imageBrush.AlignmentX = AlignmentX.Left;
                 break;
             case PositionLengthUnit.FarAlign:
                 imageBrush.AlignmentX = AlignmentX.Right;
                 break;
             case PositionLengthUnit.Absolute:
                 imageBrush.AlignmentX = AlignmentX.Left;
                 imageBrush.Transform = new TranslateTransform() { X = horizontal.Value };
                 break;
             case PositionLengthUnit.Percentage:
                 imageBrush.AlignmentX = AlignmentX.Left;
                 imageBrush.RelativeTransform = new TranslateTransform() { X = horizontal.Value };
                 break;
         }
     }
     if (vertical != null)
     {
         switch (vertical.Unit)
         {
             case PositionLengthUnit.CenterAlign:
                 imageBrush.AlignmentY = AlignmentY.Center;
                 break;
             case PositionLengthUnit.NearAlign:
                 imageBrush.AlignmentY = AlignmentY.Top;
                 break;
             case PositionLengthUnit.FarAlign:
                 imageBrush.AlignmentY = AlignmentY.Bottom;
                 break;
             case PositionLengthUnit.Absolute:
                 imageBrush.AlignmentY = AlignmentY.Top;
                 var transform = imageBrush.Transform as TranslateTransform;
                 if (transform == null)
                 {
                     transform = new TranslateTransform();
                     imageBrush.Transform = transform;
                 }
                 transform.Y = vertical.Value;
                 break;
             case PositionLengthUnit.Percentage:
                 imageBrush.AlignmentY = AlignmentY.Top;
                 var relativeTransform = imageBrush.RelativeTransform as TranslateTransform;
                 if (relativeTransform == null)
                 {
                     relativeTransform = new TranslateTransform();
                     imageBrush.RelativeTransform = relativeTransform;
                 }
                 relativeTransform.Y = vertical.Value;
                 break;
         }
     }
     control.Background = imageBrush;
 }
 async void SetBackgroundImageAsync(string imageId, Panel control, PositionLength horizontal, PositionLength vertical)
 {
     byte[] imgBytes = null;
     if (imageId.StartsWith("#"))
     {
         var img = CaptionRegion.TunneledData[imageId.Substring(1)];
         if (img != null && img.Encoding == "Base64")
         {
             imgBytes = System.Convert.FromBase64String(img.Data);
         }
     }
     else
     {
         using (var httpClient = new HttpClient())
         {
             imgBytes = await httpClient.GetByteArrayAsync(imageId);
         }
     }
     if (imgBytes != null)
     {
         await SetBackgroundImageBytesAsync(imgBytes, control, horizontal, vertical);
     }
 }
예제 #4
0
        static async Task SetBackgroundImageBytesAsync(byte[] imageBytes, Panel control, PositionLength horizontal, PositionLength vertical)
        {
            var bmpImage = await ByteArrayToImageAsync(imageBytes);

            var imageBrush = new ImageBrush()
            {
                ImageSource = bmpImage, Stretch = Stretch.None
            };

            if (horizontal != null)
            {
                switch (horizontal.Unit)
                {
                case PositionLengthUnit.CenterAlign:
                    imageBrush.AlignmentX = AlignmentX.Center;
                    break;

                case PositionLengthUnit.NearAlign:
                    imageBrush.AlignmentX = AlignmentX.Left;
                    break;

                case PositionLengthUnit.FarAlign:
                    imageBrush.AlignmentX = AlignmentX.Right;
                    break;

                case PositionLengthUnit.Absolute:
                    imageBrush.AlignmentX = AlignmentX.Left;
                    imageBrush.Transform  = new TranslateTransform()
                    {
                        X = horizontal.Value
                    };
                    break;

                case PositionLengthUnit.Percentage:
                    imageBrush.AlignmentX        = AlignmentX.Left;
                    imageBrush.RelativeTransform = new TranslateTransform()
                    {
                        X = horizontal.Value
                    };
                    break;
                }
            }
            if (vertical != null)
            {
                switch (vertical.Unit)
                {
                case PositionLengthUnit.CenterAlign:
                    imageBrush.AlignmentY = AlignmentY.Center;
                    break;

                case PositionLengthUnit.NearAlign:
                    imageBrush.AlignmentY = AlignmentY.Top;
                    break;

                case PositionLengthUnit.FarAlign:
                    imageBrush.AlignmentY = AlignmentY.Bottom;
                    break;

                case PositionLengthUnit.Absolute:
                    imageBrush.AlignmentY = AlignmentY.Top;
                    var transform = imageBrush.Transform as TranslateTransform;
                    if (transform == null)
                    {
                        transform            = new TranslateTransform();
                        imageBrush.Transform = transform;
                    }
                    transform.Y = vertical.Value;
                    break;

                case PositionLengthUnit.Percentage:
                    imageBrush.AlignmentY = AlignmentY.Top;
                    var relativeTransform = imageBrush.RelativeTransform as TranslateTransform;
                    if (relativeTransform == null)
                    {
                        relativeTransform            = new TranslateTransform();
                        imageBrush.RelativeTransform = relativeTransform;
                    }
                    relativeTransform.Y = vertical.Value;
                    break;
                }
            }
            control.Background = imageBrush;
        }