This commit is contained in:
艾竹
2023-09-05 22:51:08 +08:00
parent 2b8fe03f25
commit 9e6000f278

View File

@@ -31,11 +31,6 @@ namespace AIStudio.Wpf.DiagramDesigner
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;
}
@@ -53,11 +48,6 @@ namespace AIStudio.Wpf.DiagramDesigner
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;
}
@@ -125,48 +115,7 @@ namespace AIStudio.Wpf.DiagramDesigner
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)
{
@@ -175,16 +124,7 @@ namespace AIStudio.Wpf.DiagramDesigner
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();
var bitmapImage = base64String.ToBitmapImage(width, height);
return new System.Windows.Media.ImageBrush(bitmapImage) { Stretch = System.Windows.Media.Stretch.Uniform };
}
@@ -194,28 +134,6 @@ 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);
// }
//}
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)
{
if (string.IsNullOrEmpty(base64String))
@@ -260,7 +178,7 @@ namespace AIStudio.Wpf.DiagramDesigner
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();
System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
@@ -271,6 +189,96 @@ namespace AIStudio.Wpf.DiagramDesigner
}
public static Bitmap ToBitmap(this string base64String, int width = 0, int height = 0)
{
if (string.IsNullOrEmpty(base64String))
return null;
try
{
var byteArray = Convert.FromBase64String(base64String);
return byteArray.ToBitmap();
}
catch
{
return null;
}
}
public static Bitmap ToBitmap(this byte[] byteArray)
{
//var stream = new MemoryStream(byteArray);
//return new Bitmap(stream);
MemoryStream ms = new MemoryStream(byteArray);
Bitmap bitmap = (Bitmap)Image.FromStream(ms);
ms.Close();
return bitmap;
}
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 string ToBase64String(this BitmapImage bitmap)
{
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 string ToBase64String(this Bitmap bitmap)
{
var ms = new MemoryStream();
bitmap.Save(ms, bitmap.RawFormat);
byte[] bytearray = new byte[ms.Length];
bytearray = ms.ToArray();
return Convert.ToBase64String(bytearray);
}
public static string ToBase64String(this Stream stream)
{
byte[] bytearray = new byte[stream.Length];