Example #1
0
    /// <summary>
    /// ay 2018-6-28 09:51:49
    /// 支持jpg jpeg png gif bmp
    /// </summary>
    /// <param name="by">图片文件二进制</param>
    /// <param name="fileName">文件名,要被存储到的地方 绝对路径</param>
    public static void SaveBitmapImageIntoFile(byte[] by, string fileName)
    {
        BitmapImage bitmapImage = null;

        bitmapImage = ImageResources.ByteArrayToBitmapImage(by, null, null);
        BitmapEncoder encoder = null;
        int           index   = fileName.LastIndexOf('.');
        string        extion  = fileName.Substring(
            index + 1, fileName.Length - index - 1);

        extion = extion.ToLower();

        switch (extion)
        {
        case "jpg":
        case "jpeg":
            encoder = new JpegBitmapEncoder();
            break;

        case "png":
            encoder = new PngBitmapEncoder();
            break;

        case "gif":
            encoder = new GifBitmapEncoder();
            break;

        case "bmp":
            encoder = new BmpBitmapEncoder();
            break;
        }
        encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
        using (var fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
        {
            encoder.Save(fileStream);
        }
    }
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     return(ImageResources.ByteArrayToBitmapImage((byte[])value, DecodePixelWidth, DecodePixelHeight));
 }