2013-02-02 10 views
5

Sto cercando un modo per unire alcune immagini di tile PNG in un'immagine grande. Così cerco e ho trovato alcuni collegamenti. This non viene risposto correttamente. This non è affiancato, è utile per sovrapporre le immagini e this non sta utilizzando WPF. Quindi sto facendo questa domanda.Unisci immagini png in un'unica immagine in WPF

Problema Definizione:

ho 4 immagini PNG. Voglio unirli una singola immagine PNG in, come questo

------------------- 
|  |  | 
| png1 | png2 | 
|  |  | 
------------------- 
|  |  | 
| png3 | png4 | 
|  |  | 
------------------- 

Domanda:

Qual è il modo migliore e più efficace di fare questo (L'immagine risultante deve essere PNG)?

+0

L'unione è un problema separato per il salvataggio. Una volta ottenuta la bitmap unita, è possibile salvarla in qualsiasi formato supportato. – ChrisF

risposta

13
// Loads the images to tile (no need to specify PngBitmapDecoder, the correct decoder is automatically selected) 
BitmapFrame frame1 = BitmapDecoder.Create(new Uri(path1), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First(); 
BitmapFrame frame2 = BitmapDecoder.Create(new Uri(path2), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First(); 
BitmapFrame frame3 = BitmapDecoder.Create(new Uri(path3), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First(); 
BitmapFrame frame4 = BitmapDecoder.Create(new Uri(path4), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First(); 

// Gets the size of the images (I assume each image has the same size) 
int imageWidth = frame1.PixelWidth; 
int imageHeight = frame1.PixelHeight; 

// Draws the images into a DrawingVisual component 
DrawingVisual drawingVisual = new DrawingVisual(); 
using (DrawingContext drawingContext = drawingVisual.RenderOpen()) 
{ 
    drawingContext.DrawImage(frame1, new Rect(0, 0, imageWidth, imageHeight)); 
    drawingContext.DrawImage(frame2, new Rect(imageWidth, 0, imageWidth, imageHeight)); 
    drawingContext.DrawImage(frame3, new Rect(0, imageHeight, imageWidth, imageHeight)); 
    drawingContext.DrawImage(frame4, new Rect(imageWidth, imageHeight, imageWidth, imageHeight)); 
} 

// Converts the Visual (DrawingVisual) into a BitmapSource 
RenderTargetBitmap bmp = new RenderTargetBitmap(imageWidth * 2, imageHeight * 2, 96, 96, PixelFormats.Pbgra32); 
bmp.Render(drawingVisual); 

// Creates a PngBitmapEncoder and adds the BitmapSource to the frames of the encoder 
PngBitmapEncoder encoder = new PngBitmapEncoder(); 
encoder.Frames.Add(BitmapFrame.Create(bmp)); 

// Saves the image into a file using the encoder 
using (Stream stream = File.Create(pathTileImage)) 
    encoder.Save(stream); 
+0

Perché non usare 'PngBitmapDecoder' al numero 1? –

+0

Puoi mostrare un esempio di codice per # 4? –

+0

@HosseinNarimaniRad Ho deciso di usare un altro metodo. Utilizzando WPF! –