使用c#将WPF应用程序中的byte []转换为BitmapImage [英] Convert byte[] to BitmapImage in WPF application using c#

查看:102
本文介绍了使用c#将WPF应用程序中的byte []转换为BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我已经存储了从openfile对话框中获取的图像并以byte []的形式存储到sqlserver 2012数据库。但是当我将byte []转换为bitmapImage时它可以是绑定到wpf图像控件它会给出错误信息没有找到适合完成此操作的成像组件。

我编写的代码是:

Hello,
I have stored the image taken from openfile dialog box and stored in form of byte[] to sqlserver 2012 database.But when i convert byte[] to bitmapImage so that it can be binded to wpf image control it gives error msg "No imaging component suitable to complete this operation was found."
Code i have written is :

private void ViewImage_Click(object sender, RoutedEventArgs e)
       {
           db = myHelper.GetData();
          // byte[] rawData = new byte[0];
           var q = (from xx in db.VehicleGroups where xx.id == 1011 select xx).FirstOrDefault();
              byte[] rawData = q.Imagepath;
              image1.Source = LoadImage(rawData);


       }
       private static BitmapImage LoadImage(byte[] imageData)
       {
           if (imageData == null || imageData.Length == 0) return null;
           var image = new BitmapImage();
           using (var mem = new MemoryStream(imageData))
           {
               mem.Position = 0;
               image.BeginInit();
               image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
               image.CacheOption = BitmapCacheOption.OnLoad;
               image.UriSource = null;
               image.StreamSource = mem;
               image.EndInit();
           }
           image.Freeze();
           return image;
       }









将图像保存在数据库中编写的代码是:





to save the image in database code written was:

 private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            
            OpenFileDialog ofd = new OpenFileDialog();
            Nullable<bool> result = ofd.ShowDialog();
            if (result == true)
            {
                txtBrowseFile.Text = ofd.FileName;
            }
        }

in global area:
 BitmapImage bitmapImage;
VehicleGroup objtblVehicleGroup ;

then a method:
 private void fillbitmap()
        {
            #region load image with StreamSrouce
            bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = System.IO.File.OpenRead(txtBrowseFile.Text);
            bitmapImage.EndInit();
            //now, the Position of the StreamSource is not in the begin of the stream.

            image1.Source = bitmapImage;

            #endregion
        }

public Byte[] BufferFromImage(BitmapImage imageSource)
       {
           Stream stream = imageSource.StreamSource;
           Byte[] buffer = null;
           if (stream != null && stream.Length > 0)
           {
               using (BinaryReader br = new BinaryReader(stream))
               {
                   buffer = br.ReadBytes((Int32)stream.Length);
               }
           }

           return buffer;
       }







private void btnSave_Click(object sender, RoutedEventArgs e)
   {
         
          
               fillbitmap();
              
               objtblVehicleGroup = new VehicleGroup();

               db = myHelper.GetData();
               if (selObject.id == -1)
               {
                   objtblVehicleGroup = new VehicleGroup();
                   objtblVehicleGroup.CreatedDate = (DateTime?)System.DateTime.Now;
                   objtblVehicleGroup.IsDel = false;
               
               objtblVehicleGroup.VehicleGroupName = txtVehicleGroup.Text;
               objtblVehicleGroup.VehcileCost = Convert.ToDecimal( txtprice.Text);

               objtblVehicleGroup.Imagepath = BufferFromImage(bitmapImage);
                       db.AddToVehicleGroups(objtblVehicleGroup);

                       db.SaveChanges();
                       var userid = objtblVehicleGroup.id;
               }
               
   }

推荐答案

请尝试使用此链接



http://stackoverflow.com/ question / 15270844 / how-can-i-convert-byte-to-bitmapimage [ ^ ]
Please try to use this link

http://stackoverflow.com/questions/15270844/how-can-i-convert-byte-to-bitmapimage[^]


public static CachedBitmap ByteToImage(byte[] buffer, int width, int height, PixelFormat format)
        {
            var stride = ((width * format.BitsPerPixel + 31) / 32) * 4;
            var image = BitmapSource.Create(width, height, 96d, 96d, format, null, buffer, stride);

            return (CachedBitmap)image;
        }


这篇关于使用c#将WPF应用程序中的byte []转换为BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆