简体   繁体   English

在 C# WPF 上将 ImageSource 转换为字符串

[英]Converting ImageSource to String on C# WPF

ESRI Symbology library is slow and sometimes take longer time than expected. ESRI 符号库很慢,有时需要比预期更长的时间。

I wish to serialize a selected range of ImageSource to a cache, string in the memory or file.我希望将选定的 ImageSource 范围序列化为缓存、memory 或文件中的字符串。

I have searched the web but not much on ImageSource.我已经搜索了 web 但在 ImageSource 上搜索的不多。

An interesting thing I have found is "ImageSourceValueSerializer".我发现一个有趣的事情是“ImageSourceValueSerializer”。

Being a 3 months old baby in WPF, I am not so sure how to go about this.作为 WPF 中一个 3 个月大的婴儿,我不太清楚如何 go 关于这个。

here's how I got the ImageSource:这是我获得 ImageSource 的方式:

MultilayerPointSymbol multiLayerSym = await result.GetSymbolAsync() as MultilayerPointSymbol;
RuntimeImage swatch = await multiLayerSym.CreateSwatchAsync();
ImageSource symbolImage = await swatch.ToImageSourceAsync();

Check if the ImageSource is a BitmapSource and encode the BitmapSource by one of the BitmapEncoders.检查 ImageSource 是否为 BitmapSource 并通过 BitmapEncoder 之一对 BitmapSource 进行编码。 Encode into a MemoryStream or a FileStream.编码成 MemoryStream 或 FileStream。

if (symbolImage is BitmapSource bitmapSource)
{
    var encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

    using (var stream = new MemoryStream())
    {
        encoder.Save(stream);

        var bytes = stream.ToArray();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM