WPF Image以byte[]为数据源,结果报错

夏天的枫 2016-03-08 10:09:49

public ImageWindow(byte[] imageByte)
{
InitializeComponent();
MemoryStream ms = new MemoryStream(imageByte);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.EndInit();
image.Source = bitmapImage;
}

错误描述是 未找到适用于完成此操作的图像处理组件。
Byte[]

...全文
626 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
夏天的枫 2016-03-10
  • 打赏
  • 举报
回复
引用 5 楼 duanzi_peng 的回复:
Try......

 public static Bitmap ConvertByteToImg(byte[] bytes)
        {
            Bitmap img = null;
            try
            {
                if (bytes != null && bytes.Length != 0)
                {
                    MemoryStream ms = new MemoryStream(bytes);
                    img = new Bitmap(ms);
                }
            }
            catch (Exception ex)
            {
                LogerHelper.WriteException("Unity:55" + ex.Message);
            }
            return img;
        }

/// <summary>
        /// bitmap转成BitmapImage 
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
public static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bitmap.Save(ms, bitmap.RawFormat);
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = ms;
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapImage.EndInit();
            bitmapImage.Freeze();
            return bitmapImage;
        }
求指导一下
夏天的枫 2016-03-09
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
你先将 byte[] 转换为 System.Drawing.Bitmap 类型的bitmap ,再转为bitMapImage

public partial class ImageWindow : Window
    {
        byte[] iByte;
        public ImageWindow(byte[] imageByte)
        {
            InitializeComponent();
            iByte = imageByte;
        }

        private void image_Loaded(object sender, RoutedEventArgs e)
        {
            
                MemoryStream ms = new MemoryStream(iByte);
                //ms.Seek(0, SeekOrigin.Begin);
                //BitmapImage bitmapImage = new BitmapImage();
                //bitmapImage.BeginInit();
                //bitmapImage.StreamSource = new MemoryStream()
                //bitmapImage.EndInit();
                //image.Source = bitmapImage;
                Bitmap bm = (Bitmap)System.Drawing.Image.FromStream(ms);
                ms.Close();
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage = BitmapToBitmapImage(bm);
                image.Source = bitmapImage;
        }
        public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
        {
            Bitmap bitmapSource = new Bitmap(bitmap.Width, bitmap.Height);
            int i, j;
            for (i = 0; i < bitmap.Width; i++)
                for (j = 0; j < bitmap.Height; j++)
                {
                    System.Drawing.Color pixelColor = bitmap.GetPixel(i, j);
                    System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B);
                    bitmapSource.SetPixel(i, j, newColor);
                }
            MemoryStream ms = new MemoryStream();
            bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = new MemoryStream(ms.ToArray());
            bitmapImage.EndInit();

            return bitmapImage;
        }
    }
还是出错,byte[]装bitmap那儿,参数无效。。。
exception92 2016-03-09
  • 打赏
  • 举报
回复
你先将 byte[] 转换为 System.Drawing.Bitmap 类型的bitmap ,再转为bitMapImage
夏天的枫 2016-03-09
  • 打赏
  • 举报
回复
引用 1 楼 muzizongheng 的回复:
在ms实例化之后,ms.Seek(0, SeekOrigin.Begin))。 然后再试试
还是一样的错误。
夏天的枫 2016-03-09
  • 打赏
  • 举报
回复
引用 5 楼 duanzi_peng 的回复:
Try......

 public static Bitmap ConvertByteToImg(byte[] bytes)
        {
            Bitmap img = null;
            try
            {
                if (bytes != null && bytes.Length != 0)
                {
                    MemoryStream ms = new MemoryStream(bytes);
                    img = new Bitmap(ms);
                }
            }
            catch (Exception ex)
            {
                LogerHelper.WriteException("Unity:55" + ex.Message);
            }
            return img;
        }

/// <summary>
        /// bitmap转成BitmapImage 
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
public static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bitmap.Save(ms, bitmap.RawFormat);
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = ms;
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapImage.EndInit();
            bitmapImage.Freeze();
            return bitmapImage;
        }
还是有问题,第一步byte[]转bitmap的时候转过去bitmap为空。是不是我图片存数据库,然后取出来放在datatable里面,然后又转为了string,然后又转的byte[],这中间出的问题。。。导致byte[]不对头呢?
exception92 2016-03-09
  • 打赏
  • 举报
回复
Try......

 public static Bitmap ConvertByteToImg(byte[] bytes)
        {
            Bitmap img = null;
            try
            {
                if (bytes != null && bytes.Length != 0)
                {
                    MemoryStream ms = new MemoryStream(bytes);
                    img = new Bitmap(ms);
                }
            }
            catch (Exception ex)
            {
                LogerHelper.WriteException("Unity:55" + ex.Message);
            }
            return img;
        }

/// <summary>
        /// bitmap转成BitmapImage 
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
public static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bitmap.Save(ms, bitmap.RawFormat);
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = ms;
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapImage.EndInit();
            bitmapImage.Freeze();
            return bitmapImage;
        }
muzizongheng 2016-03-08
  • 打赏
  • 举报
回复
在ms实例化之后,ms.Seek(0, SeekOrigin.Begin))。 然后再试试

8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧