微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 将png图像合并到WPF中的单个图像中

我正在寻找一种方法将一些PNG平铺图像合并为一个大图像.所以我搜索并找到了一些链接. This未得到正确回答. This没有平铺,它有利于覆盖图像,this没有使用WPF.所以我正在提出这个问题.

问题定义:

我有4个PNG图像.我想将它们合并为一个PNG图像,就像这样

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

题:

这样做的最佳和有效方法是什么(结果图像必须是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),BitmapCacheOption.OnLoad).Frames.First();
BitmapFrame frame3 = BitmapDecoder.Create(new Uri(path3),BitmapCacheOption.OnLoad).Frames.First();
BitmapFrame frame4 = BitmapDecoder.Create(new Uri(path4),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,imageWidth,imageHeight));
    drawingContext.DrawImage(frame2,new Rect(imageWidth,imageHeight));
    drawingContext.DrawImage(frame3,imageHeight,imageHeight));
    drawingContext.DrawImage(frame4,imageHeight));
}

// Converts the Visual (DrawingVisual) into a BitmapSource
rendertargetBitmap bmp = new rendertargetBitmap(imageWidth * 2,imageHeight * 2,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);

原文地址:https://www.jb51.cc/csharp/98690.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐