如何保持图像的大小,位置和旋转,然后恢复它?

时间:2017-04-22 01:27:39

标签: wpf image xaml xamlwriter

在WPF中,我有一个图像被放到InkCanvas上并作为孩子添加:

    ImageInfo image_Info = e.Data.GetData(typeof(ImageInfo)) as ImageInfo;
        if (image_Info != null)
        {
            Image image = new Image();
            image.Width = image_Info.Width * 4;
            image.Stretch = Stretch.Uniform;
            image.Source = new BitmapImage(image_Info.Uri);
            Point position = e.GetPosition(ic);

            InkCanvas.SetLeft(image, position.X);
            InkCanvas.SetTop(image, position.Y);
            ic.Children.Add(image);
     }

然后通过装饰者移动图像并调整其大小。然后将其持久保存到数据库:

 public List<string> Children;

 var uiList = ic.Children.Cast<UIElement>().ToList();
            foreach (var p in uiList)
            {
                string uis = System.Windows.Markup.XamlWriter.Save(p);
                s.Add(uis);
            }
            Children = s;

然后儿童被送到数据库。数据库中生成的记录显示为:

"<Image Source="pack://application:,,,/Images/Female - Front.png" Stretch="Uniform" Width="Auto" InkCanvas.Top="296" InkCanvas.Left="695" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" /> "

没有引用图像的新位置,大小或旋转 - 仅指其初始下降点。使用xmlreader重新创建图像会将图像恢复为其初始放置点和大小。

foreach (string s in behavior.Children)
            {
                var stringReader = new StringReader(s);
                var xmlReader = System.Xml.XmlReader.Create(stringReader, new System.Xml.XmlReaderSettings());

                Image b = (Image)System.Windows.Markup.XamlReader.Load(xmlReader);
                ic.Children.Add(b);
            }

(图像源被打包为应用程序资源)。

如何保持图像的大小,位置和旋转,然后将其恢复?

TIA。

1 个答案:

答案 0 :(得分:1)

  1. 您可以在数据库表中为大小/位置/轮换添加其他字段,并在那里存储信息。

  2. 或者,您可以将这些字段一起添加为逗号(,)分隔并存储在Tag控件的Image字段中。 <Image Tag="(120,230);(50,50);(-30)" ... />

  3. 您还可以在DB中将整个受管理的图像保存为byte[]

    请告知这是否解决了您手头的问题。