2016-08-20 8 views
0

Das Ziel besteht darin, Bytes vieler gleich großer Bilder zu speichern und sie in WriteableBitmap zu zeichnen, um Hochleistungsvideos zu erstellen.Wie schreibe ich Pixel in WriteableBitmap by Bytes von BitmapImage?

ich nächsten Code versucht:

public MainWindow() 
    { 
     InitializeComponent(); 

     Method(); 
    } 

    private void Method() 
    { 
     BitmapImage bi = new BitmapImage(new Uri(@"Image.png", UriKind.Relative)); 
     int pw = bi.PixelWidth; 
     int ph = bi.PixelHeight; 

     WriteableBitmap wb = new WriteableBitmap(
      pw, 
      ph, 
      96, 
      96, 
      PixelFormats.Bgra32, 
      null); 

     byte[] data; 
     PngBitmapEncoder encoder = new PngBitmapEncoder(); 
     encoder.Frames.Add(BitmapFrame.Create(bi)); 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      encoder.Save(ms); 
      data = ms.ToArray(); 
     } 

     int stride = 4 * pw; 

     wb.Lock(); 
     wb.WritePixels(new Int32Rect(0, 0, pw, ph), data, 4 * pw, 0); 
     wb.Unlock(); 
    } 

Fehler:

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll Additional information: 'The invocation of the constructor on type 'WpfApplication2.MainWindow' that matches the specified binding constraints threw an exception.' Line number '6' and line position '9'. If there is a handler for this exception, the program may be safely continued.

Wenn ich den gleichen Code in Usercontrol zu platzieren, gibt es beim nächsten Fehler:

An unhandled exception of type 'System.ArgumentException' occurred in PresentationCore.dll Additional information: Buffer size is not sufficient.

+0

Sie können nicht ein codiertes Bildpuffer (ein PNG hier) in eine Writeablebitmap schreiben. Stattdessen müssen Sie rohe Pixeldaten schreiben. Holen Sie diese mit CopyPixels aus der Quell-Bitmap. – Clemens

+0

Außerdem scheint der ganze Ansatz nicht viel Sinn zu machen. Warum sollten Sie alle Pixel einer Quell-Bitmap in eine Ziel-WriteabelBitmap kopieren, wenn Sie stattdessen einfach die Quell-Bitmap * anzeigen * könnten? – Clemens

+0

@Clemens, Ich bin neu in WriteableBitmap und weiß nicht, wie es funktioniert. Ich möchte wissen, wie es geht, d. H. Bytes verschiedener Bilder speichern und sie dann mit WriteableBitmap schreiben, um ein Hochleistungsvideo zu erstellen. Ich brauche ein funktionierendes Beispiel ähnlich meinem Code. Danke, ich werde CopyPixels ausprobieren. Inzwischen habe ich ein Video aktualisiert von einem Array von zwischengespeicherten BitmapImage-Instanzen. –

Antwort

1

sollten Sie copypixels verwenden .

MainWindow.xaml:

<Grid> 
    <Image x:Name="image"></Image> 
</Grid> 

MainWindow.xaml.cs:

private void Method() 
    { 
     BitmapImage bi = new BitmapImage(new Uri(@"Image.png", UriKind.Relative)); 

     int stride = bi.PixelWidth * (bi.Format.BitsPerPixel + 7)/8; 
     byte[] data = new byte[stride * bi.PixelHeight]; 

     bi.CopyPixels(data, stride, 0); 

     WriteableBitmap wb = new WriteableBitmap(
      bi.PixelWidth, 
      bi.PixelHeight, 
      bi.DpiX, bi.DpiY, 
      bi.Format, null); 

     wb.WritePixels(
      new Int32Rect(0, 0, bi.PixelWidth, bi.PixelHeight), 
      data, stride, 0); 

     image.Source = wb; // an Image class instance from XAML. 
    } 
Verwandte Themen