mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-03-20 00:16:36 +08:00
block
This commit is contained in:
@@ -192,5 +192,46 @@ 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);
|
||||
}
|
||||
}
|
||||
return Convert.ToBase64String(bytearray);
|
||||
}
|
||||
|
||||
public static BitmapImage ToBitmapImage(this string base64String, int width = 0, int height =0)
|
||||
{
|
||||
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 bitmapImage;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user