将多字节数组图像数据合并为一个字节数组数据,并将WriteAllBytes合并为单个图像输出

问题描述 投票:0回答:1

注意使用.net3.5框架。不,我不能使用System.Windows.Media中的任何类

概观

我发现需要在屏幕上截取4个屏幕截图。

内容分布在比我的屏幕区域大的区域上,该区域宽1618像素,高696像素。

我自动截取4个区域的屏幕截图,然后我将从屏幕读取的像素编码为具有.png数据的字节数组。

然后我使用System.IO.File.WriteAllBytes将实际的png图像输出到“Path”的文件夹

问题

我确实将所有png图像输出到一个文件夹中,我可以成功查看所有4个图像。但是我需要将图像作为一个大图像。

即如图所示的here的3236 x x 1392px图像。

在图像中,您刚看到四个1618像素和696像素的正方形,标记为1到4.这代表截图和截取顺序。

它的这个完全相同的顺序,我希望图像组合并输出为单个3236 x x 1392px图像。

在这堂课上。假设图像1,2,3和4的字节数据已经分配给它们各自的字节数组。

class SimplePseudoExample
{
 private byte[] bytes1;
 private byte[] bytes2;
 private byte[] bytes3;
 private byte[] bytes4;

private byte FinalByes[];

void CreateTheSingleLargeImage()
{
 System.IO.File.WriteAllBytes("Path"+".png",FinalByes);
}

}

如何获得单个大图像输出?

c# .net unity3d
1个回答
1
投票

一种方法是将它们变成纹理,然后使用getPixelssetPixels进行合并。

 tex1 = new Texture2D(2, 2);
 ImageConversion.LoadImage(tex1, bytes1);
 tex2 = new Texture2D(2, 2);
 ImageConversion.LoadImage(tex2, bytes2);
 tex3 = new Texture2D(2, 2);
 ImageConversion.LoadImage(tex3, bytes3);
 tex4 = new Texture2D(2, 2);
 ImageConversion.LoadImage(tex4, bytes4);


 outTex = new Texture2D(tex1.width * 2, tex1.height * 2);

 // we could use tex1.width,tex1.height for everything but this is easier to read

 // setPixels bottom-left is 0,0
 // bottom-left
 outTex.setPixels(0,0,
                  tex3.width,tex3.height,
                  tex3.getPixels());
 // bottom-right
 outTex.setPixels(tex3.width,0,
                  tex4.width,tex4.height,
                  tex4.getPixels());
 // top-left
 outTex.setPixels(0,tex3.height,
                  tex1.width,tex1.height,
                  tex1.getPixels());
 // top-right
 outTex.setPixels(tex3.width, tex3.height,
                  tex2.width,tex2.height,
                  tex2.getPixels());

 byte[] outBytes = outTex.EncodeToPNG();
© www.soinside.com 2019 - 2024. All rights reserved.