How to convert Byte[] to BitmapImage

前端 未结 4 1930
傲寒
傲寒 2020-12-10 02:32

I need help, I have this method to get a BitmapImage from a Byte[]

public BitmapSource ByteToBitmapSource(byte[] image)
{
    BitmapImage imageSource = new B         


        
相关标签:
4条回答
  • 2020-12-10 02:57

    Set Image.Source to a byte array property in XAML.

    <Image x:Name="MyImage" Source="{Binding Path=MyByteArrayProperty}" />
    

    If you really want you can do this in code-behind:

    public void DecodePhoto(byte[] byteVal)
    {
      BitmapImage myBitmapImage = new BitmapImage();
      myBitmapImage.BeginInit();
      myBitmapImage.StreamSource = new MemoryStream(byteVal);
      myBitmapImage.DecodePixelWidth = 200;
      myBitmapImage.EndInit();
      MyImage.Source = myBitmapImage;
    }
    
    0 讨论(0)
  • 2020-12-10 03:13

    I've made something similar, but it's not with BitmapImage, hopes it can help...

    First, I get the image from a path, so I get a BMP and cast into a byte[]:

    private byte[] LoadImage(string szPathname)
      {
         try
         {
            Bitmap image = new Bitmap(szPathname, true);
    
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Bmp);
            return ms.ToArray();
         }catch (Exception){...}
    
         return null;
      }
    

    I call this in my code like this:

    byte[] bitmapData1 = LoadImage(@"C:\Users\toto\Desktop\test1.bmp");
    

    And when I want to cast the byte[] into System.Windows.Controls.Image I do this:

    protected virtual void OnByteArrayChanged(DependencyPropertyChangedEventArgs e)
      {
         try
         {
            // PropertyChanged method
            BitmapImage bmpi = new BitmapImage();
            bmpi.BeginInit();
            bmpi.StreamSource = new MemoryStream(ByteArray);
            bmpi.EndInit();
    
            System.Windows.Controls.Image image1 = (get my image in my xaml);
            image1.Source = bmpi;
         }catch (Exception){...}
      }
    
    0 讨论(0)
  • 2020-12-10 03:19

    You should provide us with more info on your image.
    I can assume it's unsupported by the system, which I would then advice you to use an external tool, such as imageMagik or any other converter specific to your image.

    0 讨论(0)
  • 2020-12-10 03:19

    This might also help:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.ComponentModel;
    
    
    public class MakeBitmapSource
    {
        [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
        public static extern void CopyMemory(IntPtr Destination, IntPtr Source, uint Length);
    
    
    
        public static BitmapSource FromNativePointer(IntPtr pData, int w, int h, int ch)
        {
            PixelFormat format = PixelFormats.Default;
    
            if (ch == 1) format = PixelFormats.Gray8; //grey scale image 0-255
            if (ch == 3) format = PixelFormats.Bgr24; //RGB
            if (ch == 4) format = PixelFormats.Bgr32; //RGB + alpha
    
    
            WriteableBitmap wbm = new WriteableBitmap(w, h, 96, 96, format, null);
            CopyMemory(wbm.BackBuffer, pData, (uint)(w * h * ch));
    
            wbm.Lock();
            wbm.AddDirtyRect(new Int32Rect(0, 0, wbm.PixelWidth, wbm.PixelHeight));
            wbm.Unlock();
    
            return wbm;
        }
    
        public static BitmapSource FromArray(byte[] data, int w, int h, int ch)
        {
            PixelFormat format = PixelFormats.Default;
    
            if (ch == 1) format = PixelFormats.Gray8; //grey scale image 0-255
            if (ch == 3) format = PixelFormats.Bgr24; //RGB
            if (ch == 4) format = PixelFormats.Bgr32; //RGB + alpha
    
    
            WriteableBitmap wbm = new WriteableBitmap(w, h, 96, 96, format, null);
            wbm.WritePixels(new Int32Rect(0, 0, w, h), data, ch * w, 0);
    
            return wbm;
        }
    }
    
    0 讨论(0)
提交回复
热议问题