ImageSource to Byte Array

public enum ImageFormat
    {
        Bmp,
        Ico,
        Gif,
        Jpeg,
        Png,
        Tiff,
        Wmp
    }


    public static class BitmapSourceExtensions
    {
        public static readonly Guid GUID_ContainerFormatBmp = new Guid(0xaf1d87e, 0xfcfe, 0x4188, 0xbd, 0xeb, 0xa7, 0x90, 100, 0x71, 0xcb, 0xe3);
        public static readonly Guid GUID_ContainerFormatIco = new Guid(0xa3a860c4, 0x338f, 0x4c17, 0x91, 0x9a, 0xfb, 0xa4, 0xb5, 0x62, 0x8f, 0x21);
        public static readonly Guid GUID_ContainerFormatGif = new Guid(0x1f8a5601, 0x7d4d, 0x4cbd, 0x9c, 130, 0x1b, 200, 0xd4, 0xee, 0xb9, 0xa5);
        public static readonly Guid GUID_ContainerFormatJpeg = new Guid(0x19e4a5aa, 0x5662, 0x4fc5, 160, 0xc0, 0x17, 0x58, 2, 0x8e, 0x10, 0x57);
        public static readonly Guid GUID_ContainerFormatPng = new Guid(0x1b7cfaf4, 0x713f, 0x473c, 0xbb, 0xcd, 0x61, 0x37, 0x42, 0x5f, 0xae, 0xaf);
        public static readonly Guid GUID_ContainerFormatTiff = new Guid(0x163bcc30, 0xe2e9, 0x4f0b, 150, 0x1d, 0xa3, 0xe9, 0xfd, 0xb7, 0x88, 0xa3);
        public static readonly Guid GUID_ContainerFormatWmp = new Guid(0x57a37caa, 0x367a, 0x4540, 0x91, 0x6b, 0xf1, 0x83, 0xc5, 9, 0x3a, 0x4b);

        public static void Save(this BitmapSource bitmap, Stream stream, ImageFormat imageFormat)
        {
            if (imageFormat > ImageFormat.Wmp || imageFormat < ImageFormat.Bmp)
            {
                throw new ArgumentOutOfRangeException("imageFormat");
            }

            BitmapEncoder encoder = BitmapEncoder.Create(GetContainerFormat(imageFormat));
            if (encoder != null)
            {
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                encoder.Save(stream);
            }
        }

        public static Guid GetContainerFormat(ImageFormat imageFormat)
        {
            switch (imageFormat)
            {
                case ImageFormat.Bmp:
                    return GUID_ContainerFormatBmp;
                case ImageFormat.Ico:
                    return GUID_ContainerFormatIco;
                case ImageFormat.Gif:
                    return GUID_ContainerFormatGif;
                case ImageFormat.Jpeg:
                    return GUID_ContainerFormatJpeg;
                case ImageFormat.Png:
                    return GUID_ContainerFormatPng;
                case ImageFormat.Tiff:
                    return GUID_ContainerFormatTiff;
                case ImageFormat.Wmp:
                    return GUID_ContainerFormatWmp;
                default:
                    return Guid.Empty;
            }
        }
    }

Your ConvertImageToByteArray method can be written as follows:

public byte[] ConvertImageToByteArray(BitmapSource imageToConvert, ImageFormat formatOfImage)
{
    byte[] buffer;
    try
    {
        using (MemoryStream ms = new MemoryStream())
        {
            imageToConvert.Save(ms, formatOfImage);
            buffer = ms.ToArray();
        }
    }
    catch (Exception) { throw; }

    return buffer;
}

Hope this helps

转载于:https://www.cnblogs.com/javak/archive/2009/02/04/1384134.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值