public static void DrawImage(this SKCanvas canvas, SKImage image, SKRect dest, ImageStretch stretch, ImageAlignment horizontal = ImageAlignment.Center, ImageAlignment vertical = ImageAlignment.Center, SKPaint paint = null) { if (stretch == ImageStretch.Fill) { canvas.DrawImage(image, dest, paint); } else { float scale = 1; switch (stretch) { case ImageStretch.None: break; case ImageStretch.Uniform: scale = Math.Min(dest.Width / image.Width, dest.Height / image.Height); break; case ImageStretch.UniformToFill: scale = Math.Max(dest.Width / image.Width, dest.Height / image.Height); break; } SKRect display = CalculateDisplayRect(dest, scale * image.Width, scale * image.Height, horizontal, vertical); canvas.DrawImage(image, display, paint); } }
public static System.Windows.Media.Stretch ConvertTo(this ImageStretch source) { switch (source) { case ImageStretch.Fill: return(System.Windows.Media.Stretch.Fill); case ImageStretch.None: return(System.Windows.Media.Stretch.None); case ImageStretch.Uniform: return(System.Windows.Media.Stretch.Uniform); case ImageStretch.UniformToFill: return(System.Windows.Media.Stretch.UniformToFill); } return(System.Windows.Media.Stretch.None); }
public override void Parse(string data) { base.Parse(data); XmlNode node = XmlUtils.CreateNodeFromData(data); string stretchValue = XmlUtils.GetAttributeValue(node, STRETCH); string rotationValue = XmlUtils.GetAttributeValue(node, ROTATION); this.Rotation = float.Parse(rotationValue); this.Stretch = ImageStretchUtils.FromString(stretchValue); }
public static async Task <BitmapImage> LoadImage(string url, ImageStretch imageStretch, int maxWidth, int maxHeight) { if (string.IsNullOrEmpty(url)) { return(null); } if (loading) { return(null); } if (lastDownloaded.Equals(url)) { return(_image); } using (var client = new WebClient()) { lastDownloaded = url; try { var data = await client.DownloadDataTaskAsync(url); if (data != null) { var memoryStream = new MemoryStream(data); _image = new BitmapImage(); _image.BeginInit(); _image.StreamSource = memoryStream; _image.StreamSource.Position = 0; _image.EndInit(); _image.Freeze(); if (imageStretch == ImageStretch.None) { if (maxHeight > 0 || maxWidth > 0) { var tmp = new BitmapImage(); tmp.BeginInit(); tmp.StreamSource = memoryStream; memoryStream.Position = 0; var modified = false; if (maxHeight > 0 && _image.PixelHeight > maxHeight) { if (_image.PixelHeight > _image.PixelWidth || maxWidth == 0) { tmp.DecodePixelHeight = maxHeight; } else { tmp.DecodePixelWidth = maxWidth; } modified = true; } if (maxWidth > 0 && _image.PixelWidth > maxHeight && !modified) { if (_image.PixelWidth > _image.PixelHeight || maxHeight == 0) { tmp.DecodePixelWidth = maxWidth; } else { tmp.DecodePixelHeight = maxHeight; } } tmp.EndInit(); tmp.Freeze(); _image = tmp; } } loading = false; return(_image); } return(null); } catch (Exception ex) { return(null); } } }
/// <summary> /// Копировать данные из элемента /// </summary> protected override void CopyFrom(Element srcElem) { base.CopyFrom(srcElem); StaticPicture staticPicture = (StaticPicture)srcElem; BorderColor = staticPicture.BorderColor; Image = staticPicture.Image == null ? null : staticPicture.Image.CloneWithoutData(); ImageStretch = staticPicture.ImageStretch; }