合并PNG图像到WPF单一的形象 [英] Merge png images into single image in WPF

查看:425
本文介绍了合并PNG图像到WPF单一的形象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来合并某些PNG图像拼接成一个大的图像。所以,我搜索,发现一些链接。 没有正确回答。 是不是平铺,这是很好的图像叠加和不使用WPF。所以我做了这个问题。



问题定义:



我有4 PNG图像。我想将它们合并成一个单一的PNG图像,这样

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

问:



什么是做这个(得到的图像必须是PNG)的最好的和有效的方式?


解决方案

  //加载图像瓦(无需指定PngBitmapDecoder,正确的解码器自动选择)
BitmapFrame帧1 = BitmapDecoder.Create(新的URI(PATH1),BitmapCreateOptions.None,BitmapCacheOption.OnLoad).Frames.First();
BitmapFrame式2 = BitmapDecoder.Create(新的URI(PATH2),BitmapCreateOptions.None,BitmapCacheOption.OnLoad).Frames.First();
BitmapFrame帧3 = BitmapDecoder.Create(新的URI(path3时),BitmapCreateOptions.None,BitmapCacheOption.OnLoad).Frames.First();
BitmapFrame窗口4 = BitmapDecoder.Create(新的URI(path4),BitmapCreateOptions.None,BitmapCacheOption.OnLoad).Frames.First();

//获取的图像的大小(I假设每个图像具有相同的尺寸)
INT imageWidth = frame1.PixelWidth;
INT imageHeight = frame1.PixelHeight;

//绘制图像转换成一个DrawingVisual组件
DrawingVisual drawingVisual =新DrawingVisual();
使用(的DrawingContext的DrawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawImage(帧1,新的Rect(0,0,imageWidth,imageHeight));
drawingContext.DrawImage(帧2,新的矩形(imageWidth,0,imageWidth,imageHeight));
drawingContext.DrawImage(帧3,新的矩形(0,imageHeight,imageWidth,imageHeight));
drawingContext.DrawImage(窗口4,新的矩形(imageWidth,imageHeight,imageWidth,imageHeight));
}

//视觉(DrawingVisual)转换成一个的BitmapSource
RenderTargetBitmap BMP =新RenderTargetBitmap(imageWidth * 2,imageHeight * 2,96,96,PixelFormats.Pbgra32) ;
bmp.Render(drawingVisual);

//创建一个PngBitmapEncoder,并增加了的BitmapSource编码器
PngBitmapEncoder编码器=新PngBitmapEncoder的帧();
encoder.Frames.Add(BitmapFrame.Create(BMP));

//使用编码器
使用(流流= File.Create(pathTileImage))
encoder.Save(流)保存图像到一个文件;


I'm looking for a way to Merge some PNG tile images into a big image. So I search and found some links. This is not answered properly. This is not tiling, it's good for overlaying images and this is not using WPF. So I'm making this question.

Problem Definition:

I have 4 PNG images. I want to merge them into a single PNG image, like this

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

Question:

What is the best and efficient way of doing this (The resulting image must be PNG)?

解决方案

// 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);

这篇关于合并PNG图像到WPF单一的形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆