页面视图新增缩略图模式

This commit is contained in:
艾竹
2023-05-27 12:35:44 +08:00
parent b11d39024a
commit 01131dde47
25 changed files with 2177 additions and 190 deletions

View File

@@ -208,7 +208,7 @@ namespace AIStudio.Wpf.DiagramDesigner
base.OnRender(dc);
// without a background the OnMouseMove event would not be fired !
// Alternative: implement a Canvas as a child of this adorner, like
// Alternative: implement a CanvasBrush as a child of this adorner, like
// the ConnectionAdorner does.
dc.DrawRectangle(Brushes.Transparent, null, new Rect(RenderSize));

View File

@@ -104,7 +104,7 @@ namespace AIStudio.Wpf.DiagramDesigner
base.OnRender(dc);
// without a background the OnMouseMove event would not be fired !
// Alternative: implement a Canvas as a child of this adorner, like
// Alternative: implement a CanvasBrush as a child of this adorner, like
// the ConnectionAdorner does.
dc.DrawRectangle(Brushes.Transparent, null, new Rect(RenderSize));

View File

@@ -234,6 +234,21 @@ namespace AIStudio.Wpf.DiagramDesigner
this.Loaded += DesignerCanvas_Loaded;
this.IsVisibleChanged += DesignerCanvas_IsVisibleChanged;
this.DataContextChanged += DesignerCanvas_DataContextChanged;
}
private void DesignerCanvas_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue is IDiagramViewModel oldvalue)
{
//var image = this.ToBitmap().ToBitmapSource();
//oldvalue.Thumbnail = new ImageBrush(image) { Stretch = Stretch.Uniform };
}
if (e.NewValue is IDiagramViewModel newvalue)
{
newvalue.Thumbnail = new VisualBrush(this) { Stretch = Stretch.Uniform };
}
}
private void DesignerCanvas_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
@@ -248,6 +263,7 @@ namespace AIStudio.Wpf.DiagramDesigner
{
Mediator.Instance.Register(this);
this.Focus();
int xx = this.GetHashCode();
_service.PropertyChanged += _service_PropertyChanged;
}
@@ -324,7 +340,7 @@ namespace AIStudio.Wpf.DiagramDesigner
private void EnterColorPicker()
{
// create rubberband adorner
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);
if (adornerLayer != null)
@@ -394,8 +410,8 @@ namespace AIStudio.Wpf.DiagramDesigner
_viewModel.ClearSelectedItems();
}
if (_service.DrawModeViewModel.SharpDrawModeSelected ||
if (_service.DrawModeViewModel.SharpDrawModeSelected ||
(_service.DrawModeViewModel.DrawingDrawModeSelected && _service.DrawModeViewModel.DrawingDrawMode != DrawMode.Select))
{
// create rubberband adorner
@@ -425,8 +441,8 @@ namespace AIStudio.Wpf.DiagramDesigner
protected override void OnMouseMove(MouseEventArgs e)
{
var focusedElement = Keyboard.FocusedElement;
Debug.WriteLine("focusedElement" + focusedElement?.ToString());
//var focusedElement = Keyboard.FocusedElement;
//Debug.WriteLine("focusedElement" + focusedElement?.ToString());
base.OnMouseMove(e);

View File

@@ -0,0 +1,196 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Windows;
using System.Windows.Media.Imaging;
namespace AIStudio.Wpf.DiagramDesigner
{
public static class BitmapHelper
{
/// <summary>
/// 截图转换成bitmap
/// </summary>
/// <param name="element"></param>
/// <param name="width">默认控件宽度</param>
/// <param name="height">默认控件高度</param>
/// <param name="x">默认0</param>
/// <param name="y">默认0</param>
/// <returns></returns>
public static Bitmap ToBitmap(this FrameworkElement element, int width = 0, int height = 0, int x = 0, int y = 0)
{
if (width == 0) width = (int)element.ActualWidth;
if (height == 0) height = (int)element.ActualHeight;
var rtb = new RenderTargetBitmap(width, height, x, y, System.Windows.Media.PixelFormats.Default);
rtb.Render(element);
var bit = BitmapSourceToBitmap(rtb);
////测试代码
//DirectoryInfo d = new DirectoryInfo(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Cache"));
//if (!d.Exists) d.Create();
//bit.Save(System.IO.Path.Combine(d.FullName, "控件截图.png"));
return bit;
}
public static Bitmap ToBitmap(this System.Windows.Media.VisualBrush brush, Rect rect)
{
var visual = new System.Windows.Media.DrawingVisual();
using (var dc = visual.RenderOpen())
{
dc.DrawRectangle(brush, null, rect);
}
var rtb = new RenderTargetBitmap((int)rect.Width, (int)rect.Height, 96, 96, System.Windows.Media.PixelFormats.Default);
rtb.Render(visual);
var bit = BitmapSourceToBitmap(rtb);
////测试代码
//DirectoryInfo d = new DirectoryInfo(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Cache"));
//if (!d.Exists) d.Create();
//bit.Save(System.IO.Path.Combine(d.FullName, "控件截图.png"));
return bit;
}
/// <summary>
/// BitmapSource转Bitmap
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Bitmap BitmapSourceToBitmap(this BitmapSource source)
{
return BitmapSourceToBitmap(source, source.PixelWidth, source.PixelHeight);
}
/// <summary>
/// Convert BitmapSource to Bitmap
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Bitmap BitmapSourceToBitmap(this BitmapSource source, int width, int height)
{
Bitmap bmp = null;
try
{
PixelFormat format = PixelFormat.Format24bppRgb;
/*set the translate type according to the in param(source)*/
switch (source.Format.ToString())
{
case "Rgb24":
case "Bgr24": format = PixelFormat.Format24bppRgb; break;
case "Bgra32": format = PixelFormat.Format32bppPArgb; break;
case "Bgr32": format = PixelFormat.Format32bppRgb; break;
case "Pbgra32": format = PixelFormat.Format32bppArgb; break;
}
bmp = new Bitmap(width, height, format);
BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size),
ImageLockMode.WriteOnly,
format);
source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bmp.UnlockBits(data);
}
catch
{
if (bmp != null)
{
bmp.Dispose();
bmp = null;
}
}
return bmp;
}
public static System.Windows.Media.ImageSource ToBitmapSource(this Bitmap bitmap, int width, int height)
{
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.DecodePixelWidth = width;//需要缩略图的解码宽度
bitmapImage.DecodePixelHeight = height;//缩略图的解码高度
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
return bitmapImage;
}
public static string ToBase64String(this System.Windows.Media.Brush brush)
{
try
{
if (brush is System.Windows.Media.VisualBrush visualBrush)
{
var size = ((UIElement)visualBrush.Visual).DesiredSize;
var image = visualBrush.ToBitmap(new Rect(size));
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
var bytearray = stream.ToArray();
return Convert.ToBase64String(bytearray);
}
else if (brush is System.Windows.Media.ImageBrush imageBrush)
{
var bitmap = (imageBrush.ImageSource as BitmapImage);
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);
}
}
return Convert.ToBase64String(bytearray);
}
else
{
return null;
}
}
catch (Exception ex)
{
return null;
}
}
public static System.Windows.Media.Brush ToBrush(this string base64String, int width, int height)
{
if (string.IsNullOrEmpty(base64String))
return null;
try
{
var byteArray = Convert.FromBase64String(base64String);
BitmapImage bitmapImage = null;
bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.DecodePixelWidth = width;//需要缩略图的解码宽度
bitmapImage.DecodePixelHeight = height;//缩略图的解码高度
bitmapImage.StreamSource = new MemoryStream(byteArray);
bitmapImage.EndInit();
return new System.Windows.Media.ImageBrush(bitmapImage) { Stretch = System.Windows.Media.Stretch.Uniform };
}
catch
{
return null;
}
}
}
}

View File

@@ -334,9 +334,18 @@ namespace AIStudio.Wpf.DiagramDesigner
return menuOptions;
}
}
public string Thumbnail
private Brush _thumbnail;
public Brush Thumbnail
{
get; set;
get
{
return _thumbnail;
}
set
{
SetProperty(ref _thumbnail, value);
}
}
public bool ShowMenuOptions
@@ -980,6 +989,16 @@ namespace AIStudio.Wpf.DiagramDesigner
}
#endregion
public void SaveThumbnail()
{
if (Thumbnail is VisualBrush visualBrush)
{
var size = ((UIElement)visualBrush.Visual).DesiredSize;
var image = visualBrush.ToBitmap(new Rect(size)).ToBitmapSource((int)size.Width / 4, (int)size.Height / 4);
Thumbnail = new ImageBrush(image) { Stretch = Stretch.Uniform };
}
}
[MediatorMessageSink("DoneDrawingMessage")]
public void OnDoneDrawingMessage(bool dummy)
{
@@ -3135,5 +3154,11 @@ namespace AIStudio.Wpf.DiagramDesigner
});
}
#endregion
public override string ToString()
{
return $"{Name}-{DiagramType}";
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
@@ -293,7 +294,7 @@ namespace AIStudio.Wpf.DiagramDesigner
{
get; set;
}
string Thumbnail
Brush Thumbnail
{
get; set;
}
@@ -341,6 +342,8 @@ namespace AIStudio.Wpf.DiagramDesigner
void ClearSelectedItems();
bool ExecuteShortcut(KeyEventArgs e);
void SaveThumbnail();
#endregion
#region