2013-02-02 39 views
5

Tôi đang tìm cách kết hợp một số hình ảnh lát PNG vào một hình ảnh lớn. Vì vậy, tôi tìm kiếm và tìm thấy một số liên kết. This không được trả lời đúng cách. This không phải là ốp lát, tốt cho việc che phủ hình ảnh và this không sử dụng WPF. Vì vậy, tôi đang đưa ra câu hỏi này.Kết hợp các hình ảnh png vào một hình ảnh trong WPF

Vấn đề Định nghĩa:

Tôi có 4 hình ảnh PNG. Tôi muốn kết hợp chúng thành một hình ảnh PNG duy nhất, như

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

Câu hỏi này:

cách tốt nhất và hiệu quả để làm điều này (Hình ảnh thu được phải PNG) là gì?

+0

Tham gia là một vấn đề riêng biệt đối với việc lưu. Một khi bạn có bitmap tham gia, bạn có thể lưu nó ở bất kỳ định dạng được hỗ trợ nào. – ChrisF

Trả lời

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

Tại sao không sử dụng 'PngBitmapDecoder' ở # 1? –

+0

Bạn có thể hiển thị mẫu mã cho # 4 không? –

+0

@HosseinNarimaniRad Tôi đã quyết định sử dụng một phương pháp khác. Sử dụng WPF! –

Các vấn đề liên quan