2012-01-17 42 views
14

tôi cần sự giúp đỡ, tôi có phương pháp này để có được một BitmapImage từ một Byte []Làm thế nào để chuyển đổi Byte [] để BitmapImage

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

    using (MemoryStream stream = new MemoryStream(image)) 
    { 
     stream.Seek(0, SeekOrigin.Begin); 
     imageSource.BeginInit(); 
     imageSource.StreamSource = stream; 
     imageSource.CacheOption = BitmapCacheOption.OnLoad; 
     imageSource.EndInit(); 
    } 

    return imageSource; 
} 

imageSource.EndInit(); ném một lỗi "Chúng tôi không tìm thấy phần hình ảnh phù hợp để hoàn thành hoạt động này . "

+2

định dạng gì là hình ảnh mà bạn đang cố gắng để tải? Bạn có chắc chắn rằng nó được hỗ trợ? – Shimmy

Trả lời

0

Bạn nên cung cấp cho chúng tôi thêm thông tin về hình ảnh của bạn.
Tôi có thể cho rằng hệ thống không được hỗ trợ, sau đó tôi khuyên bạn nên sử dụng công cụ bên ngoài, chẳng hạn như imageMagik hoặc bất kỳ công cụ chuyển đổi nào khác cụ thể cho hình ảnh của bạn.

0

Tôi đã thực hiện một cái gì đó tương tự, nhưng nó không phải với BitmapImage, hy vọng nó có thể giúp ...

Đầu tiên, tôi nhận được hình ảnh từ một con đường, vì vậy tôi có được một BMP và đúc thành một 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; 
    } 

tôi gọi đây là trong mã của tôi như thế này:

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

Và khi tôi muốn bỏ byte [] vào System.Windows.Controls.Image tôi làm điều này:

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){...} 
    } 
24

Đặt Image.Source thành thuộc tính mảng byte trong XAML.

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

Nếu bạn thực sự muốn bạn có thể làm điều đó trong mã đằng sau như thế này

public void DecodePhoto(byte[] byteVal) 
     { 
      if (byteVal == null) 
{ 
return ; 
} 


      try 
      { 
       MemoryStream strmImg = new MemoryStream(byteVal); 
       BitmapImage myBitmapImage = new BitmapImage(); 
       myBitmapImage.BeginInit(); 
       myBitmapImage.StreamSource = strmImg; 
       myBitmapImage.DecodePixelWidth = 200; 
       myBitmapImage.EndInit(); 
       MyImage.Source = myBitmapImage; 
      } 
      catch (Exception) 
      { 
      } 
     } 
+0

Làm thế nào bạn có thể đặt Image.Source thành một byte [] khi nó thuộc loại ImageSource? – TimothyP

+3

@TimothyP Ý tôi là trong XAML. 0x4f3759df

0

này cũng có thể giúp:

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; 
    } 
} 
Các vấn đề liên quan