2016-06-16 18 views
7

Qual è l'equivalente WPF del seguente codice SWT Java? Voglio creare un'immagine da un elenco di valori RGBA e visualizzarli su una tela.Qual è l'equivalente WPF della visualizzazione di un'immagine su una tela utilizzando ImageData in Java SWT

private Image GetImage() 
{ 

    ImageData imageData = new ImageData(imageWidth, imageHeight,32,palette); 

    int pixelVecLoc=0; 
    for (int h = 0; h<imageHeight && (pixelVecLoc < currentImagePixelVec.size()); h++) 
    { 
     for (int w = 0; w<imageWidth && (pixelVecLoc < currentImagePixelVec.size()); w++) 
     { 
      int p = 0; 
      Pixel pixel = currentImagePixelVec.get(pixelVecLoc); 
      p = (pixel.Alpha<<24) | (pixel.Red<<16) | (pixel.Green<<8) | pixel.Blue;     
      imageData.setPixel(w, h, p);    
      pixelVecLoc++; 
     } 
    } 

    imageData = imageData.scaledTo(imageScaleWidth, imageScaleHeight); 
    Image image = ImageDescriptor.createFromImageData(imageData).createImage(); 
    return image; 
} 

Quindi disegnare su una tela di canapa:

gc.drawImage(image, 0, 0); 
+2

utilizzare un controllo immagine e assegnare un 'WriteableBitmap' alla sua Proprietà 'sorgente'. Oppure usa uno degli overload di 'BitmapSource.Create()' per creare una BitmapSource da dati di pixel grezzi. – Clemens

risposta

6

Questo è un breve frammento che mostra come è possibile creare un buffer RGBA personalizzato e scrivere i dati dei pixel ad esso (based on this example):

int width = 512; 
int height = 256; 
int stride = width * 4 + (width % 4); 
int pixelWidth = 4; // RGBA (BGRA) 
byte[] imageData = new byte[width * stride];  // raw byte buffer 

for (int y = 0; y < height; y++) 
{ 
    int yPos = y * stride; 

    for (int x = 0; x < width; x++) 
    { 
     int xPos = yPos + x * pixelWidth; 

     imageData[xPos + 2] = (byte) (RedValue); // replace *Value with source data 
     imageData[xPos + 1] = (byte) (GreenValue); 
     imageData[xPos ] = (byte) (BlueValue); 
     imageData[xPos + 3] = (byte) (AlphaValue); 
    } 
} 

Quindi utilizzare il metodo BitmapSource.Create Method (Int32, Int32, Double, Double, PixelFormat, BitmapPalette, IntPtr, Int32, Int32) insieme a PixelFormats:

BitmapSource bmp = 
    BitmapSource.Create(
    width, 
    height, 
    96,     // Horizontal DPI 
    96,     // Vertical DPI 
    PixelFormats.Bgra32, // 32-bit BGRA 
    null,     // no palette 
    imageData,    // byte buffer 
    imageData.Length,  // buffer size 
    stride);    // stride 

Si noti che l'ordine dei byte è inverso tranne il componente alfa (BGRA) come mostrato nel frammento.

Per trasferire il risultato su tela si può prima creare un Image, impostare la BitmapSource come Source e, infine, aggiungere che alla tela:

// create image and set image as source 
Image BmpImg = New Image(); 
BmpImg.Width = width; 
BmpImg.Height = height; 
BmpImg.Source = bmp; 

// add image to canvas 
canvas.Children.Add(BmpImg); 

Canvas.SetLeft(BmpImg, 0); // to set position (x,y) 
Canvas.SetTop(BmpImg, 0); 
+1

Grazie per quello - come lo visualizzeresti su una tela? –

+1

@Robben_Ford_Fan_boy Ho aggiunto un esempio. Disclaimer: il mio VS è giù al momento, quindi non ho potuto ricontrollare. Vado dalla memoria. (Qualcuno) fammi sapere se c'è qualcosa di sbagliato e aggiornerò. – K3N

Problemi correlati