将System.Windows.Media.ImageSource转换为ByteArray

本杰明·马丁

有没有一种方法可以将ImageSource对象转换为字节数组?我有一个绑定到WPF窗口的ImageSource对象,我可以从数据库转换一个字节数组,然后将其转换为ImageSource,但是我不能做相反的事情。

提前谢谢。

编辑:我试图将ImageSource转换为BitmapImage,但是得到了一个空对象。

克莱门斯

即使您的ImageSource不是BitmapImage,您仍可以成功将其BitmapSource强制转换,这是所有WPF位图类的基类,例如BitmapImage,BitmapFrame,WriteableBitmap,RenderTargetBitmap等(请参见此处)。

因此,如果您的ImageSource实际上是BitmapSource(而不是DrawingImage或D3DImage),则以下方法通过使用指定的BitmapEncoder(例如PngBitmapEncoder)将其转换为字节数组:

public byte[] ImageSourceToBytes(BitmapEncoder encoder, ImageSource imageSource)
{
    byte[] bytes = null;
    var bitmapSource = imageSource as BitmapSource;

    if (bitmapSource != null)
    {
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

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

    return bytes;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将System.Drawing.Bitmap转换为WPF的System.Windows.Media.BitmapImage

来自分类Dev

C#如何将system.windows.media.brush转换为system.drawing.brush

来自分类Dev

如何将System.Windows.Media.DrawingImage转换为Stream?

来自分类Dev

无法将类型'string'隐式转换为'Windows.UI.Xaml.Media.Imaging.BitmapImage

来自分类Dev

将字节 [] 转换为 Windows.UI.Xaml.Media.Imaging.BitmapImage

来自分类Dev

将charArray转换为byteArray

来自分类Dev

将MessageBoxImage转换为ImageSource WPF

来自分类Dev

如何将图像转换为 ImageSource

来自分类Dev

难以使用ffmpeg将示例mkv转换为mp4,并无法在Windows Media Player中播放

来自分类Dev

UWP中的System.Windows.Media

来自分类Dev

UWP中的System.Windows.Media

来自分类Dev

在什么情况下System.Windows.Media.Transform.Inverse将返回null

来自分类Dev

将ByteArray转换为UUID Java

来自分类Dev

python:将bytearray转换为ctypes Struct

来自分类Dev

使用 javascript 将 byteArray 转换为 IntegerArray

来自分类Dev

将 api httpresponse bytearray 转换为文件

来自分类Dev

将System.Drawing.Icon转换为System.Windows.Controls.Image

来自分类Dev

'无法将类型为'System.String'的对象转换为类型'System.Windows.Controls.ComboBoxItem

来自分类Dev

如何将ImageSource转换为Byte数组?

来自分类Dev

如何将图标(位图)转换为ImageSource?

来自分类Dev

将位图转换为ImageSource使我的图像背景变黑

来自分类Dev

无法将类型为“ System.Windows.Controls.Grid”的对象转换为类型为“ System.Windows.Shapes.Ellipse”的对象?

来自分类Dev

无法将类型为“ System.Windows.Forms.FlowLayoutPanel”的对象转换为“ System.Windows.Forms.Button c#”

来自分类Dev

Xaml参考静态属性,例如System.Windows.Media.Colors

来自分类Dev

如何确定System.Windows.Media.DrawingContext的锚点?

来自分类Dev

WPF ComboBox作为System.Windows.Media.Colors

来自分类Dev

如何处置System.Windows.Media.MediaPlayer

来自分类Dev

C# - 获取所有 System.Windows.Media.Fonts

来自分类Dev

System.Windows.Media.Media3D.Matrix3D如何工作

Related 相关文章

  1. 1

    将System.Drawing.Bitmap转换为WPF的System.Windows.Media.BitmapImage

  2. 2

    C#如何将system.windows.media.brush转换为system.drawing.brush

  3. 3

    如何将System.Windows.Media.DrawingImage转换为Stream?

  4. 4

    无法将类型'string'隐式转换为'Windows.UI.Xaml.Media.Imaging.BitmapImage

  5. 5

    将字节 [] 转换为 Windows.UI.Xaml.Media.Imaging.BitmapImage

  6. 6

    将charArray转换为byteArray

  7. 7

    将MessageBoxImage转换为ImageSource WPF

  8. 8

    如何将图像转换为 ImageSource

  9. 9

    难以使用ffmpeg将示例mkv转换为mp4,并无法在Windows Media Player中播放

  10. 10

    UWP中的System.Windows.Media

  11. 11

    UWP中的System.Windows.Media

  12. 12

    在什么情况下System.Windows.Media.Transform.Inverse将返回null

  13. 13

    将ByteArray转换为UUID Java

  14. 14

    python:将bytearray转换为ctypes Struct

  15. 15

    使用 javascript 将 byteArray 转换为 IntegerArray

  16. 16

    将 api httpresponse bytearray 转换为文件

  17. 17

    将System.Drawing.Icon转换为System.Windows.Controls.Image

  18. 18

    '无法将类型为'System.String'的对象转换为类型'System.Windows.Controls.ComboBoxItem

  19. 19

    如何将ImageSource转换为Byte数组?

  20. 20

    如何将图标(位图)转换为ImageSource?

  21. 21

    将位图转换为ImageSource使我的图像背景变黑

  22. 22

    无法将类型为“ System.Windows.Controls.Grid”的对象转换为类型为“ System.Windows.Shapes.Ellipse”的对象?

  23. 23

    无法将类型为“ System.Windows.Forms.FlowLayoutPanel”的对象转换为“ System.Windows.Forms.Button c#”

  24. 24

    Xaml参考静态属性,例如System.Windows.Media.Colors

  25. 25

    如何确定System.Windows.Media.DrawingContext的锚点?

  26. 26

    WPF ComboBox作为System.Windows.Media.Colors

  27. 27

    如何处置System.Windows.Media.MediaPlayer

  28. 28

    C# - 获取所有 System.Windows.Media.Fonts

  29. 29

    System.Windows.Media.Media3D.Matrix3D如何工作

热门标签

归档