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



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

概述

我发现需要在屏幕上截取 4 张内容屏幕截图。

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

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

然后我使用System.IO.File.WriteAllBytes将实际的 png 图像输出到"路径">中的文件夹

问题所在

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

即 3236 x 1392 像素的图像,如下所示。

在图像中,您刚刚看到四个 1618px by 和 696px 正方形,标记为 1 到 4。 这表示屏幕截图和拍摄顺序。

它与我希望图像组合并输出为单个 3236 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);
}
}

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

一种方法是将它们转换为纹理,然后使用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();

最新更新