diff --git a/AIStudio.Wpf.DiagramApp/ViewModels/PageViewModel.cs b/AIStudio.Wpf.DiagramApp/ViewModels/PageViewModel.cs index 738179b..8346818 100644 --- a/AIStudio.Wpf.DiagramApp/ViewModels/PageViewModel.cs +++ b/AIStudio.Wpf.DiagramApp/ViewModels/PageViewModel.cs @@ -426,15 +426,7 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels foreach (var viewModel in DiagramViewModels) { - DiagramItem diagramItem = new DiagramItem(viewModel); - - var selectedDesignerItems = viewModel.Items.OfType(); - var selectedConnections = viewModel.Items.OfType(); - - diagramItem.DesignerItems = selectedDesignerItems.Select(p => p.ToSerializableItem(ext)).Where(p => p != null).ToList(); - diagramItem.Connections = selectedConnections.Select(p => p.ToSerializableItem(ext)).Where(p => p != null).ToList(); - - + DiagramItem diagramItem = new DiagramItem(viewModel); diagramDocument.DiagramItems.Add(diagramItem); } @@ -485,12 +477,6 @@ namespace AIStudio.Wpf.DiagramApp.ViewModels { var viewModel = DiagramViewModel; DiagramItem diagramItem = new DiagramItem(viewModel); - - var selectedDesignerItems = viewModel.Items.OfType(); - var selectedConnections = viewModel.Items.OfType(); - - diagramItem.DesignerItems = selectedDesignerItems.Select(p => p.ToSerializableItem(".json")).Where(p => p != null).ToList(); - diagramItem.Connections = selectedConnections.Select(p => p.ToSerializableItem(".json")).Where(p => p != null).ToList(); diagramItem.Name = NewNameHelper.GetNewName(DiagramViewModels.Select(p => p.Name), "页-"); viewModel = GetDiagramViewModel(diagramItem, ".json"); diff --git a/AIStudio.Wpf.DiagramDesigner/Helpers/BitmapHelper.cs b/AIStudio.Wpf.DiagramDesigner/Helpers/BitmapHelper.cs index dcf6dc2..b33d7ed 100644 --- a/AIStudio.Wpf.DiagramDesigner/Helpers/BitmapHelper.cs +++ b/AIStudio.Wpf.DiagramDesigner/Helpers/BitmapHelper.cs @@ -143,7 +143,7 @@ namespace AIStudio.Wpf.DiagramDesigner { var bitmap = (imageBrush.ImageSource as BitmapImage); byte[] bytearray = null; - Stream smarket = bitmap.StreamSource; + Stream smarket = bitmap.StreamSource; if (smarket != null && smarket.Length > 0) { //设置当前位置 @@ -194,21 +194,27 @@ namespace AIStudio.Wpf.DiagramDesigner public static string ToBase64String(this BitmapImage bitmap) { - byte[] bytearray = null; - Stream smarket = bitmap.StreamSource; - if (smarket != null && smarket.Length > 0) - { - //设置当前位置 - smarket.Position = 0; - using (BinaryReader br = new BinaryReader(smarket)) - { - bytearray = br.ReadBytes((int)smarket.Length); - } - } + //byte[] bytearray = null; + //Stream smarket = bitmap.StreamSource; + //if (smarket != null && smarket.Length > 0) + //{ + // //设置当前位置 + // smarket.Position = 0; + // using (BinaryReader br = new BinaryReader(smarket)) + // { + // bytearray = br.ReadBytes((int)smarket.Length); + // } + //} + + Stream stream = bitmap.StreamSource; + byte[] bytearray = new byte[stream.Length]; + stream.Seek(0, SeekOrigin.Begin); + stream.Read(bytearray, 0, bytearray.Length); + return Convert.ToBase64String(bytearray); } - public static BitmapImage ToBitmapImage(this string base64String, int width = 0, int height =0) + public static BitmapImage ToBitmapImage(this string base64String, int width = 0, int height = 0) { if (string.IsNullOrEmpty(base64String)) return null; @@ -233,5 +239,34 @@ namespace AIStudio.Wpf.DiagramDesigner return null; } } + + public static BitmapImage ToBitmapImage(this System.Drawing.Bitmap ImageOriginal, int width = 0, int height = 0) + { + System.Drawing.Bitmap ImageOriginalBase = new System.Drawing.Bitmap(ImageOriginal); + BitmapImage bitmapImage = new BitmapImage() { DecodePixelWidth = width, DecodePixelHeight = height }; + System.IO.MemoryStream ms = new System.IO.MemoryStream(); + ImageOriginalBase.Save(ms, System.Drawing.Imaging.ImageFormat.Png); + bitmapImage.BeginInit(); + bitmapImage.StreamSource = ms; + bitmapImage.CacheOption = BitmapCacheOption.OnLoad; + bitmapImage.EndInit(); + bitmapImage.Freeze(); + + return bitmapImage; + } + + public static BitmapImage ToBitmapImage(this byte[] byteArray, int width = 0, int height = 0) + { + BitmapImage bitmapImage = new BitmapImage() { DecodePixelWidth = width, DecodePixelHeight = height }; + System.IO.MemoryStream ms = new System.IO.MemoryStream(); + bitmapImage.BeginInit(); + bitmapImage.StreamSource = ms; + bitmapImage.CacheOption = BitmapCacheOption.OnLoad; + bitmapImage.EndInit(); + bitmapImage.Freeze(); + + return bitmapImage; + + } } } diff --git a/AIStudio.Wpf.DiagramDesigner/Models/Serializables/Container/BlockItemsContainerInfoItem.cs b/AIStudio.Wpf.DiagramDesigner/Models/Serializables/Container/BlockItemsContainerInfoItem.cs index 1e624a7..2798633 100644 --- a/AIStudio.Wpf.DiagramDesigner/Models/Serializables/Container/BlockItemsContainerInfoItem.cs +++ b/AIStudio.Wpf.DiagramDesigner/Models/Serializables/Container/BlockItemsContainerInfoItem.cs @@ -23,6 +23,7 @@ namespace AIStudio.Wpf.DiagramDesigner this.ChildFlag = viewmodel.ChildFlag; this.PhysicalItemWidth = viewmodel.PhysicalItemWidth; this.PhysicalItemHeight = viewmodel.PhysicalItemHeight; + this.Parameter = new ConstParameterItem(viewmodel.Parameter); Children = new List(viewmodel.Children.Select(p => new BlockDesignerItem(p))); } @@ -52,10 +53,16 @@ namespace AIStudio.Wpf.DiagramDesigner get; set; } + [XmlElement] + public ConstParameterItem Parameter + { + get; set; + } + [XmlArray] public List Children { get; set; - } + } } } diff --git a/AIStudio.Wpf.DiagramDesigner/Models/Serializables/Container/ConstParameterItem.cs b/AIStudio.Wpf.DiagramDesigner/Models/Serializables/Container/ConstParameterItem.cs new file mode 100644 index 0000000..dcba14d --- /dev/null +++ b/AIStudio.Wpf.DiagramDesigner/Models/Serializables/Container/ConstParameterItem.cs @@ -0,0 +1,31 @@ +using AIStudio.Wpf.DiagramDesigner; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Serialization; + +namespace AIStudio.Wpf.DiagramDesigner +{ + [XmlInclude(typeof(ConstParameterItem))] + public class ConstParameterItem + { + [XmlAttribute] + public string Text { get; set; } + + [XmlAttribute] + public virtual string Value { get; set; } + + public ConstParameterItem() + { + + } + + public ConstParameterItem(ConstParameter viewmodel) + { + Text = viewmodel.Text; + Value = viewmodel.Value?.ToString(); + } + } +} diff --git a/AIStudio.Wpf.DiagramDesigner/Models/Serializables/DiagramItem.cs b/AIStudio.Wpf.DiagramDesigner/Models/Serializables/DiagramItem.cs index 74105a5..09b0b02 100644 --- a/AIStudio.Wpf.DiagramDesigner/Models/Serializables/DiagramItem.cs +++ b/AIStudio.Wpf.DiagramDesigner/Models/Serializables/DiagramItem.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Windows; using System.Windows.Media; using System.Xml; @@ -17,7 +18,7 @@ namespace AIStudio.Wpf.DiagramDesigner } - public DiagramItem(IDiagramViewModel diagramView) + public DiagramItem(IDiagramViewModel diagramView, string ext = ".json") { Name = diagramView.Name; DiagramType = diagramView.DiagramType; @@ -30,9 +31,16 @@ namespace AIStudio.Wpf.DiagramDesigner PageSizeType = diagramView.DiagramOption.LayoutOption.PageSizeType; PhysicalGridMarginSize = diagramView.DiagramOption.LayoutOption.PhysicalGridMarginSize; GridColor = diagramView.DiagramOption.LayoutOption.GridColor; + PageBackground = diagramView.DiagramOption.LayoutOption.PageBackground; AllowDrop = diagramView.DiagramOption.LayoutOption.AllowDrop; Thumbnail = diagramView.Thumbnail.ToBase64String(); + + var selectedDesignerItems = diagramView.Items.OfType(); + var selectedConnections = diagramView.Items.OfType(); + + DesignerItems = selectedDesignerItems.Select(p => p.ToSerializableItem(ext)).Where(p => p != null).ToList(); + Connections = selectedConnections.Select(p => p.ToSerializableItem(ext)).Where(p => p != null).ToList(); } [XmlAttribute] @@ -157,6 +165,26 @@ namespace AIStudio.Wpf.DiagramDesigner } } + [XmlIgnore] + public Color PageBackground + { + get; set; + } + + [JsonIgnore] + [XmlAttribute("PageBackground")] + public string XmlPageBackground + { + get + { + return SerializeHelper.SerializeColor(PageBackground); + } + set + { + PageBackground = SerializeHelper.DeserializeColor(value); + } + } + [XmlAttribute] public bool AllowDrop { diff --git a/AIStudio.Wpf.DiagramDesigner/Models/Values/ConstParameter.cs b/AIStudio.Wpf.DiagramDesigner/Models/Values/ConstParameter.cs index 4f375b8..5d9f111 100644 --- a/AIStudio.Wpf.DiagramDesigner/Models/Values/ConstParameter.cs +++ b/AIStudio.Wpf.DiagramDesigner/Models/Values/ConstParameter.cs @@ -4,7 +4,15 @@ namespace AIStudio.Wpf.DiagramDesigner { public class ConstParameter : BindableBase, IParameter { + public ConstParameter() + { + } + public ConstParameter(ConstParameterItem item) + { + Text = item.Text; + Value = item.Value.ToString(); + } private string _text; diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/Container/BlockItemsContainerInfo.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/Container/BlockItemsContainerInfo.cs index a704de7..c0e10dc 100644 --- a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/Container/BlockItemsContainerInfo.cs +++ b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/Container/BlockItemsContainerInfo.cs @@ -65,6 +65,7 @@ namespace AIStudio.Wpf.DiagramDesigner this.ChildFlag = designer.ChildFlag; this.PhysicalItemWidth = designer.PhysicalItemWidth; this.PhysicalItemHeight = designer.PhysicalItemHeight; + this.Parameter = new ConstParameter(designer.Parameter); if (designer.Children != null) { foreach (var child in designer.Children) @@ -178,8 +179,8 @@ namespace AIStudio.Wpf.DiagramDesigner } } - private IParameter _parameter; - public IParameter Parameter + private ConstParameter _parameter; + public ConstParameter Parameter { get { diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModel.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModel.cs index f09e824..de24da7 100644 --- a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModel.cs +++ b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModel.cs @@ -859,7 +859,8 @@ namespace AIStudio.Wpf.DiagramDesigner DiagramOption.LayoutOption.PageSizeType = diagramItem.PageSizeType; DiagramOption.LayoutOption.PhysicalGridMarginSize = diagramItem.PhysicalGridMarginSize; DiagramOption.LayoutOption.GridColor = diagramItem.GridColor; - DiagramOption.LayoutOption.AllowDrop = diagramItem.AllowDrop; + DiagramOption.LayoutOption.PageBackground = diagramItem.PageBackground; + DiagramOption.LayoutOption.AllowDrop = diagramItem.AllowDrop; Thumbnail = diagramItem.Thumbnail.ToBrush((int)DiagramOption.LayoutOption.PageSize.Width / 4, (int)DiagramOption.LayoutOption.PageSize.Height / 4); Init(true);