Wpf 如何保持图像的大小、位置和旋转,然后恢复它?
在WPF中,我有一个图像,它被放到InkCanvas上并作为子对象添加:Wpf 如何保持图像的大小、位置和旋转,然后恢复它?,wpf,image,xaml,xamlwriter,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;
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;
(图像源打包为应用程序资源)
如何保持图像的大小、位置和旋转,然后恢复图像
蒂亚
图像
控件的标记
字段中<代码>byte[]
请告诉我这是否解决了您手头的问题。事实证明,移除装饰器后,上述代码确实可以正常工作。但是,我非常喜欢您将附加信息作为字符串存储在标记中的想法。我相信xamlwriter将正确地编写用于持久化的字符串数据,这将允许在前端和数据库之间进行图像识别。好主意。谢谢。你根本不应该序列化和反序列化XAML。最好使用描述当前视图的所有必要数据创建适当的模型类。然后加载并保存该模型类的实例。
"<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" /> "
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);
}