2013-02-02 6 views
5

큰 이미지로 일부 PNG 타일 이미지를 병합하는 방법을 찾고 있습니다. 그래서 나는 몇 개의 링크를 찾고 찾았다. This이 제대로 응답하지 않습니다. This은 바둑판 식 배열이 아니므로 이미지를 오버레이하는 데 적합하며 this은 WPF를 사용하지 않습니다. 그래서 나는이 질문을하고있다.WPF에서 단일 이미지로 png 이미지 병합

문제 정의 :

내가 4 개 PNG 이미지를 가지고있다. 나는이

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

질문처럼, 하나의 PNG 이미지로 병합하려면 :

이 (결과 이미지 PNG해야합니다)를 수행하는 최고의 효율적인 방법은 무엇입니까?

+0

절약과 관련된 별도의 문제입니다. 결합 된 비트 맵이 있으면 지원되는 모든 형식으로 저장할 수 있습니다. – ChrisF

답변

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

# 1에서'PngBitmapDecoder'를 사용하지 않으시겠습니까? –

+0

4 번 코드 샘플을 표시 할 수 있습니까? –

+0

@HosseinNarimaniRad 다른 방법을 사용하기로 결정했습니다. WPF 사용! –

관련 문제