ESRI 符號系統庫速度較慢,有時需要比預期更長的時間。
我希望將選定范圍的 ImageSource 序列化為快取、記憶體或檔案中的字串。
我在網上搜索過,但在 ImageSource 上搜索的不多。
我發現一個有趣的事情是“ImageSourceValueSerializer”。
作為 WPF 中 3 個月大的嬰兒,我不太確定該怎么做。
這是我獲得 ImageSource 的方式:
MultilayerPointSymbol multiLayerSym = await result.GetSymbolAsync() as MultilayerPointSymbol;
RuntimeImage swatch = await multiLayerSym.CreateSwatchAsync();
ImageSource symbolImage = await swatch.ToImageSourceAsync();
測驗了 Clemen's 的例行程式:
MultilayerPointSymbol multiLayerSym = await result.GetSymbolAsync() as MultilayerPointSymbol;
RuntimeImage swatch = await multiLayerSym.CreateSwatchAsync();
ImageSource symbolImage = await swatch.ToImageSourceAsync();
byte[] b = ImageSourceBinary(symbolImage);
ImageSource test = BinaryImageSource(b);
在課堂里:
private byte[] ImageSourceBinary(ImageSource imageSrc)
{
if (imageSrc is BitmapSource bitmapSource)
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
using (MemoryStream stream = new MemoryStream())
{
encoder.Save(stream);
return stream.ToArray();
}
}
return null;
}
private ImageSource BinaryImageSource(byte[] bytes)
{
using (MemoryStream stream = new MemoryStream(bytes))
{
PngBitmapDecoder decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.Default);
BitmapFrame bf = decoder.Frames[0];
if (bf is ImageSource imagesource)
return imagesource;
return null;
}
}
結果,沒圖!:(
uj5u.com熱心網友回復:
檢查 ImageSource 是否為 BitmapSource 并通過 BitmapEncoders 之一對 BitmapSource 進行編碼。編碼為 MemoryStream 或 FileStream。
private byte[] ImageSourceToByteArray(ImageSource imageSrc)
{
if (symbolImage is BitmapSource bitmapSource)
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
using (var stream = new MemoryStream())
{
encoder.Save(stream);
return stream.ToArray();
}
}
return null;
}
為了從位元組陣列解碼影像,不要明確使用特定的 BitmapDecoder。更好地依賴自動解碼器選擇,如下所示。設定BitmapCacheOption.OnLoad流在解碼后立即關閉的時間也很重要。
private ImageSource ByteArrayToImageSource(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
return BitmapFrame.Create(
stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/388102.html
